Repository navigation
perf: keep document work off the UI thread and cap the iOS brush map #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
|
|
||
| using namespace facebook::react; | ||
|
|
||
| // Brushes outlive the strokes they belong to, because undo can bring those strokes back. | ||
| static const NSUInteger INKBrushLimit = 1000; | ||
|
|
||
| @interface INKPadView () <RCTInkpadViewViewProtocol, PKCanvasViewDelegate> | ||
| @end | ||
|
|
||
|
|
@@ -39,6 +42,7 @@ @implementation INKPadView { | |
| CGFloat _brushSize; | ||
| BOOL _erasing; | ||
| NSMutableDictionary<NSDate *, INKBrushInfo *> *_brushes; | ||
| NSMutableArray<NSDate *> *_brushOrder; | ||
| } | ||
|
|
||
| + (ComponentDescriptorProvider)componentDescriptorProvider | ||
|
|
@@ -62,6 +66,7 @@ - (instancetype)initWithFrame:(CGRect)frame | |
| _brushColor = UIColor.blackColor; | ||
| _brushSize = 3; | ||
| _brushes = [NSMutableDictionary new]; | ||
| _brushOrder = [NSMutableArray new]; | ||
|
|
||
| _canvas = [[INKCanvasView alloc] initWithFrame:frame]; | ||
| _canvas.delegate = self; | ||
|
|
@@ -181,22 +186,40 @@ - (void)replaceDrawing:(PKDrawing *)drawing | |
|
|
||
| - (void)getStrokes:(NSInteger)requestId | ||
| { | ||
| NSString *json = [INKStrokeCodec encodeDrawing:_canvas.drawing size:self.bounds.size brushes:_brushes]; | ||
| [self emitResult:requestId ok:YES payload:json errorCode:@"" message:@""]; | ||
| PKDrawing *drawing = _canvas.drawing; | ||
| NSDictionary<NSDate *, INKBrushInfo *> *brushes = [_brushes copy]; | ||
| CGSize size = self.bounds.size; | ||
| __weak INKPadView *weakSelf = self; | ||
| // A long drawing serializes to a large JSON string, and exporting already renders off the main thread. | ||
| dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ | ||
| NSString *json = [INKStrokeCodec encodeDrawing:drawing size:size brushes:brushes]; | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| [weakSelf emitResult:requestId ok:YES payload:json errorCode:@"" message:@""]; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| - (void)setStrokes:(NSInteger)requestId json:(NSString *)json | ||
| { | ||
| NSString *errorCode = nil; | ||
| NSString *message = nil; | ||
| INKDecodedDrawing *decoded = [INKStrokeCodec decode:json errorCode:&errorCode message:&message]; | ||
| if (decoded == nil) { | ||
| [self emitResult:requestId ok:NO payload:@"" errorCode:errorCode message:message]; | ||
| return; | ||
| } | ||
| [_brushes addEntriesFromDictionary:decoded.brushes]; | ||
| [self replaceDrawing:decoded.drawing]; | ||
| [self emitResult:requestId ok:YES payload:@"" errorCode:@"" message:@""]; | ||
| __weak INKPadView *weakSelf = self; | ||
| // Loading a saved drawing builds a stroke path per point, which is as heavy as encoding one. | ||
| dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ | ||
| NSString *errorCode = nil; | ||
| NSString *message = nil; | ||
| INKDecodedDrawing *decoded = [INKStrokeCodec decode:json errorCode:&errorCode message:&message]; | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| INKPadView *view = weakSelf; | ||
| if (decoded == nil) { | ||
| [view emitResult:requestId ok:NO payload:@"" errorCode:errorCode message:message]; | ||
| return; | ||
| } | ||
| [decoded.brushes enumerateKeysAndObjectsUsingBlock:^(NSDate *created, INKBrushInfo *info, BOOL *stop) { | ||
| [view rememberBrush:info forDate:created]; | ||
| }]; | ||
| [view replaceDrawing:decoded.drawing]; | ||
| [view emitResult:requestId ok:YES payload:@"" errorCode:@"" message:@""]; | ||
| }); | ||
| }); | ||
|
Comment on lines
+204
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: set -eu
printf '%s\n' '--- changed hunks ---'
git diff --unified=80 717a9ad2d77f362ca06689be65571dc457979dc0 1a62ac92756182a55e9492baf7d23e8eca2bccb8 -- ios/INKPadView.mm
printf '%s\n' '--- command references ---'
rg -n -F --glob 'ios/*' -- 'getStrokes' ios || test "$?" -eq 1
rg -n -F --glob 'ios/*' -- 'setStrokes' ios || test "$?" -eq 1Repository: SectionTN/react-native-inkpad Length of output: 12539 🏁 Script executed: set -eu
nl -ba ios/INKPadView.mm | sed -n '140,250p'
printf '%s\n' '--- command declarations and callers ---'
rg -n -F --glob 'ios/*' -- 'getStrokes' ios || test "$?" -eq 1
rg -n -F --glob 'ios/*' -- 'setStrokes' ios || test "$?" -eq 1
printf '%s\n' '--- relevant diff ---'
git diff --unified=30 717a9ad2d77f362ca06689be65571dc457979dc0 1a62ac92756182a55e9492baf7d23e8eca2bccb8 -- ios/INKPadView.mmRepository: SectionTN/react-native-inkpad Length of output: 14698 🏁 Script executed: grep -n -A45 -B15 'getStrokes\|setStrokes' ios/INKPadView.mmRepository: SectionTN/react-native-inkpad Length of output: 3718 Preserve command ordering between
Serialize the complete command sequence, including the main-thread replacement and snapshot. A serial codec queue alone does not fix this race. Awaiting 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| - (void)exportImage:(NSInteger)requestId optionsJson:(NSString *)optionsJson | ||
|
|
@@ -276,7 +299,21 @@ - (void)rememberNewStrokes | |
| info.color = INKHexFromColor(_brushColor); | ||
| info.size = _brushSize; | ||
| info.input = INKInputName(_canvas.lastTouchType); | ||
| _brushes[created] = info; | ||
| [self rememberBrush:info forDate:created]; | ||
| } | ||
| } | ||
|
|
||
| // Undo can bring back any stroke that is still in the drawing, so the brushes stay in a bounded window | ||
| // instead of growing for the whole session. | ||
| - (void)rememberBrush:(INKBrushInfo *)info forDate:(NSDate *)date | ||
| { | ||
| if (![_brushOrder.lastObject isEqualToDate:date]) { | ||
| [_brushOrder addObject:date]; | ||
| } | ||
| _brushes[date] = info; | ||
| while (_brushOrder.count > INKBrushLimit) { | ||
| [_brushes removeObjectForKey:_brushOrder.firstObject]; | ||
| [_brushOrder removeObjectAtIndex:0]; | ||
| } | ||
| } | ||
|
Comment on lines
+308
to
318
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Brush eviction can drop metadata for strokes that are still in the drawing.
Also, Sort the decoded entries by date before registering them. Proposed fix for the decode ordering (lines 216-218)- [decoded.brushes enumerateKeysAndObjectsUsingBlock:^(NSDate *created, INKBrushInfo *info, BOOL *stop) {
- [view rememberBrush:info forDate:created];
- }];
+ for (NSDate *created in [decoded.brushes.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
+ [view rememberBrush:decoded.brushes[created] forDate:created];
+ }🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Stale
setStrokesresult can erase newer user edits.setStrokesnow parses onWORKERand later callsreplaceAll(next)on the UI thread. This is a behavior change. The old code replaced the strokes synchronously.While the worker runs, the user can still draw, erase, undo, or call
clear.replaceAllthen overwrites those edits without a check. It also records aReplaceop whose "before" list comes from the new state. A stroke drawn in that window is lost, and the caller already received no error.Two
setStrokescalls can also overlap with other commands. The single-threadWORKERkeeps them in order. The posted results can still interleave with user edits.Add a guard. For example, keep a
documentGenerationcounter on the UI thread. Bump it on each user edit and on eachsetStrokescall. Capture it beforeWORKER.execute. Apply the result only if it is unchanged. Otherwise callemitErrorwith a clear "document changed" message. Alternatively, accept last-writer-wins and document that behavior.🤖 Prompt for AI Agents