Skip to content

Commit 841e00e

Browse files
fkgozalifacebook-github-bot
authored andcommittedJan 31, 2024·
iOS: RCTImageLoader: use static request counter for request IDs (#42720)
Summary: Pull Request resolved: #42720 The request ID was used for in-memory request caching purpose only. There wasn't much reason to use the sophisticated monotonic number, so let's just use a static 64-bit unsigned int counter. With the more-or-less unique UUID prefix, this should have an extremely low chance of collision. Changelog: [Internal] Reviewed By: philIip, sammy-SC Differential Revision: D53205641 fbshipit-source-id: e6da12029624058dc877e9cbe2000af4df938870
1 parent 25c5d1c commit 841e00e

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed
 

‎packages/react-native/Libraries/Image/RCTImageLoader.mm

+4-11
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,10 @@ static NSInteger RCTImageBytesForImage(UIImage *image)
4444
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
4545
}
4646

47-
static uint64_t monotonicTimeGetCurrentNanoseconds(void)
47+
static uint64_t getNextImageRequestCount(void)
4848
{
49-
static struct mach_timebase_info tb_info = {0};
50-
static dispatch_once_t onceToken;
51-
dispatch_once(&onceToken, ^{
52-
__unused int ret = mach_timebase_info(&tb_info);
53-
assert(0 == ret);
54-
});
55-
56-
return (mach_absolute_time() * tb_info.numer) / tb_info.denom;
49+
static uint64_t requestCounter = 0;
50+
return requestCounter++;
5751
}
5852

5953
static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLResponse *response)
@@ -528,8 +522,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
528522
auto cancelled = std::make_shared<std::atomic<int>>(0);
529523
__block dispatch_block_t cancelLoad = nil;
530524
__block NSLock *cancelLoadLock = [NSLock new];
531-
NSString *requestId =
532-
[NSString stringWithFormat:@"%@-%llu", [[NSUUID UUID] UUIDString], monotonicTimeGetCurrentNanoseconds()];
525+
NSString *requestId = [NSString stringWithFormat:@"%@-%llu", [[NSUUID UUID] UUIDString], getNextImageRequestCount()];
533526

534527
void (^completionHandler)(NSError *, id, id, NSURLResponse *) =
535528
^(NSError *error, id imageOrData, id imageMetadata, NSURLResponse *response) {

0 commit comments

Comments
 (0)
Please sign in to comment.