onnxruntime/js/react_native/ios/OnnxruntimeModuleTest/FakeRCTBlobManager.m
Jhen-Jie Hong ea1a5cf920
[js/rn] Implement blob exchange by JSI instead of use base64 (#16094)
### Description
<!-- Describe your changes. -->

- Create `OnnxruntimeJSIHelper` native module to provide two JSI
functions
- `jsiOnnxruntimeStoreArrayBuffer`: Store buffer in Blob Manager &
return blob object (iOS: RCTBlobManager, Android: BlobModule)
  - `jsiOnnxruntimeResolveArrayBuffer`: Use blob object to get buffer
- The part of implementation is reference to
[react-native-blob-jsi-helper](https://github.com/mrousavy/react-native-blob-jsi-helper)
- Replace base64 encode/decode
  - `loadModelFromBlob`: Rename from `loadModelFromBase64EncodedBuffer`
  - `run`: Use blob object to replace input.data & results[].data

For [this
context](https://github.com/microsoft/onnxruntime/issues/16031#issuecomment-1556527812),
it saved a lot of time and avoid JS thread blocking in decode return
type, it is 3700ms -> 5~20ms for the case. (resolve function only takes
0.x ms)

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

It’s related to #16031, but not a full implementation for migrate to
JSI.

It just uses JSI through BlobManager to replace the slow part (base64
encode / decode).

Rewriting it entirely in JSI could be complicated, like type convertion
and threading. This PR might be considered a minor change.

/cc @skottmckay
2023-06-16 19:37:02 +10:00

47 lines
1.1 KiB
Objective-C

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import <Foundation/Foundation.h>
#import "FakeRCTBlobManager.h"
@implementation FakeRCTBlobManager
- (instancetype)init {
if (self = [super init]) {
_blobs = [NSMutableDictionary new];
}
return self;
}
- (NSString *)store:(NSData *)data {
NSString *blobId = [[NSUUID UUID] UUIDString];
_blobs[blobId] = data;
return blobId;
}
- (NSData *)resolve:(NSString *)blobId offset:(long)offset size:(long)size {
NSData *data = _blobs[blobId];
if (data == nil) {
return nil;
}
return [data subdataWithRange:NSMakeRange(offset, size)];
}
- (NSDictionary *)testCreateData:(NSData *)buffer {
NSString* blobId = [self store:buffer];
return @{
@"blobId": blobId,
@"offset": @0,
@"size": @(buffer.length),
};
}
- (NSString *)testGetData:(NSDictionary *)data {
NSString *blobId = [data objectForKey:@"blobId"];
long size = [[data objectForKey:@"size"] longValue];
long offset = [[data objectForKey:@"offset"] longValue];
[self resolve:blobId offset:offset size:size];
return blobId;
}
@end