Skip to content

Commit

Permalink
iOS: RCTImageLoader: use static request counter for request IDs (#42720)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fkgozali authored and facebook-github-bot committed Jan 31, 2024
1 parent 25c5d1c commit 841e00e
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions packages/react-native/Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,10 @@ static NSInteger RCTImageBytesForImage(UIImage *image)
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
}

static uint64_t monotonicTimeGetCurrentNanoseconds(void)
static uint64_t getNextImageRequestCount(void)
{
static struct mach_timebase_info tb_info = {0};
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__unused int ret = mach_timebase_info(&tb_info);
assert(0 == ret);
});

return (mach_absolute_time() * tb_info.numer) / tb_info.denom;
static uint64_t requestCounter = 0;
return requestCounter++;
}

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

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

0 comments on commit 841e00e

Please sign in to comment.