Skip to content

Commit 558767d

Browse files
perf(ios): bound markdown cache and pending renders
1 parent 8d2f84a commit 558767d

6 files changed

Lines changed: 330 additions & 15 deletions

File tree

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"clean": "yarn workspace react-native-enriched-markdown clean && del-cli apps/react-native-example/android/build apps/react-native-example/android/app/build apps/react-native-example/ios/build apps/react-native-macos-example/macos/build",
5656
"sync-md4c": "yarn workspace react-native-enriched-markdown sync-md4c",
5757
"prepare": "yarn workspace react-native-enriched-markdown prepare",
58-
"release": "release-it --only-version"
58+
"release": "release-it --only-version",
59+
"test:ios-streaming": "node scripts/test-enriched-markdown-native.mjs"
5960
},
6061
"devDependencies": {
6162
"@commitlint/config-conventional": "^20.5.0",

‎packages/react-native-enriched-markdown/ios/internals/MeasurementCache.h‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ class MeasurementCache {
299299

300300
void set(const MeasurementCacheKey &key, CachedSize size)
301301
{
302+
const size_t keyBytes = keyStorageBytes(key);
303+
if (keyBytes > kMaxKeyBytes)
304+
return;
302305
std::lock_guard<std::mutex> lock(mutex_);
303306

304307
auto it = map_.find(key);
@@ -308,14 +311,16 @@ class MeasurementCache {
308311
return;
309312
}
310313

311-
list_.push_front({key, size});
312-
map_[key] = list_.begin();
313-
314-
if (map_.size() > kMaxEntries) {
314+
while (map_.size() >= kMaxEntries || keyBytes_ + keyBytes > kMaxKeyBytes) {
315315
auto &lastEntry = list_.back();
316+
keyBytes_ -= keyStorageBytes(lastEntry.key);
316317
map_.erase(lastEntry.key);
317318
list_.pop_back();
318319
}
320+
321+
list_.push_front({key, size});
322+
map_[key] = list_.begin();
323+
keyBytes_ += keyBytes;
319324
}
320325

321326
void removeMatchingMarkdown(const std::string &markdown)
@@ -324,6 +329,7 @@ class MeasurementCache {
324329

325330
for (auto it = list_.begin(); it != list_.end();) {
326331
if (it->key.markdown == markdown) {
332+
keyBytes_ -= keyStorageBytes(it->key);
327333
map_.erase(it->key);
328334
it = list_.erase(it);
329335
} else {
@@ -336,6 +342,15 @@ class MeasurementCache {
336342
MeasurementCache() = default;
337343

338344
static constexpr size_t kMaxEntries = 512;
345+
// The list and map each own a key. Count both copies; allocator/node overhead is additional.
346+
static constexpr size_t kMaxKeyBytes = 8 * 1024 * 1024;
347+
static size_t keyStorageBytes(const MeasurementCacheKey &key)
348+
{
349+
return sizeof(CacheEntry) + sizeof(MeasurementCacheKey) +
350+
2 * (key.markdown.size() + key.lineBreakStrategyIOS.size() + key.writingDirection.size() +
351+
key.ellipsizeMode.size() + key.linkRegexKey.size() + key.inlineCodeLinkRegexKey.size());
352+
}
353+
size_t keyBytes_ = 0;
339354
mutable std::mutex mutex_;
340355

341356
std::list<CacheEntry> list_;

‎packages/react-native-enriched-markdown/ios/utils/ENRMAsyncRenderCoordinator.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
- (instancetype)initWithQueueLabel:(const char *)label NS_DESIGNATED_INITIALIZER;
1212
- (instancetype)init NS_UNAVAILABLE;
1313

14-
/// Dispatches renderBlock on the serial queue.
14+
/// Keeps only the newest pending renderBlock on the serial queue.
1515
/// If renderBlock returns YES, applyBlock is dispatched on the main queue,
1616
/// provided the render ID has not been superseded by a newer call.
1717
- (void)scheduleRender:(BOOL (^)(void))renderBlock apply:(dispatch_block_t)applyBlock;
1818

19-
/// Advances the render ID so any in-flight render's apply block is discarded
19+
/// Drops pending work and advances the render ID so any in-flight render's apply block is discarded
2020
/// by the renderId == currentRenderId check when it lands on the main queue.
2121
/// Use before recycling or otherwise resetting a host that owns this coordinator.
2222
- (void)invalidate;

‎packages/react-native-enriched-markdown/ios/utils/ENRMAsyncRenderCoordinator.m‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
@implementation ENRMAsyncRenderCoordinator {
44
dispatch_queue_t _queue;
55
NSUInteger _currentRenderId;
6+
BOOL _workerScheduled;
7+
BOOL (^_pendingRender)(void);
8+
dispatch_block_t _pendingApply;
69
}
710

811
- (instancetype)initWithQueueLabel:(const char *)label
@@ -17,21 +20,53 @@ - (void)scheduleRender:(BOOL (^)(void))renderBlock apply:(dispatch_block_t)apply
1720
{
1821
if (_blockAsyncRender)
1922
return;
20-
NSUInteger renderId = ++_currentRenderId;
21-
dispatch_async(_queue, ^{
22-
if (!renderBlock())
23+
@synchronized(self) {
24+
++_currentRenderId;
25+
_pendingRender = [renderBlock copy];
26+
_pendingApply = [applyBlock copy];
27+
if (_workerScheduled)
2328
return;
24-
dispatch_async(dispatch_get_main_queue(), ^{
25-
if (renderId == self->_currentRenderId) {
26-
applyBlock();
29+
_workerScheduled = YES;
30+
}
31+
dispatch_async(_queue, ^{
32+
while (YES) {
33+
@autoreleasepool {
34+
BOOL (^nextRender)(void);
35+
dispatch_block_t nextApply;
36+
NSUInteger renderId;
37+
@synchronized(self) {
38+
if (!self->_pendingRender) {
39+
self->_workerScheduled = NO;
40+
return;
41+
}
42+
nextRender = self->_pendingRender;
43+
nextApply = self->_pendingApply;
44+
renderId = self->_currentRenderId;
45+
self->_pendingRender = nil;
46+
self->_pendingApply = nil;
47+
}
48+
// Keep at most one in-flight render and one replaceable pending revision.
49+
if (!nextRender())
50+
continue;
51+
dispatch_async(dispatch_get_main_queue(), ^{
52+
@synchronized(self) {
53+
if (renderId != self->_currentRenderId)
54+
return;
55+
}
56+
nextApply();
57+
});
2758
}
28-
});
59+
}
2960
});
3061
}
3162

3263
- (void)invalidate
3364
{
34-
++_currentRenderId;
65+
@synchronized(self) {
66+
++_currentRenderId;
67+
_pendingRender = nil;
68+
_pendingApply = nil;
69+
}
3570
}
3671

3772
@end
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#include "MeasurementCacheUnderTest.h"
2+
#import <Foundation/Foundation.h>
3+
#include <cstring>
4+
5+
static NSMutableArray<dispatch_block_t> *background;
6+
static NSMutableArray<dispatch_block_t> *mainQueue;
7+
8+
static void enqueue(dispatch_queue_t queue, dispatch_block_t block)
9+
{
10+
[(queue == dispatch_get_main_queue() ? mainQueue : background) addObject:[block copy]];
11+
}
12+
13+
// Control queue progress deterministically while executing the installed coordinator itself.
14+
#define dispatch_async enqueue
15+
#include "ENRMAsyncRenderCoordinator.m"
16+
#undef dispatch_async
17+
18+
static void check(BOOL condition, const char *message)
19+
{
20+
if (!condition) {
21+
fprintf(stderr, "FAIL: %s\n", message);
22+
exit(1);
23+
}
24+
}
25+
26+
static void drain(NSMutableArray<dispatch_block_t> *queue)
27+
{
28+
while (queue.count) {
29+
dispatch_block_t block = queue.firstObject;
30+
[queue removeObjectAtIndex:0];
31+
block();
32+
}
33+
}
34+
35+
static void testCoordinator(void)
36+
{
37+
background = [NSMutableArray new];
38+
mainQueue = [NSMutableArray new];
39+
ENRMAsyncRenderCoordinator *coordinator = [[ENRMAsyncRenderCoordinator alloc] initWithQueueLabel:"test"];
40+
__block NSUInteger rendered = 0;
41+
__block NSUInteger applied = 0;
42+
__weak NSObject *obsolete;
43+
@autoreleasepool {
44+
NSObject *document = [NSObject new];
45+
obsolete = document;
46+
[coordinator
47+
scheduleRender:^BOOL {
48+
(void)[document description];
49+
return YES;
50+
}
51+
apply:^{}];
52+
}
53+
for (NSUInteger revision = 1; revision <= 3; revision++) {
54+
[coordinator
55+
scheduleRender:^BOOL {
56+
rendered++;
57+
return YES;
58+
}
59+
apply:^{ applied = revision; }];
60+
}
61+
printf("coordinator pending workers: %lu; obsolete document retained: %s\n", (unsigned long)background.count,
62+
obsolete ? "yes" : "no");
63+
check(obsolete == nil, "superseded pending documents must be released before the worker runs");
64+
check(background.count == 1, "a burst must enqueue one worker, not one closure per revision");
65+
drain(background);
66+
drain(mainQueue);
67+
check(rendered == 1 && applied == 3, "only the latest pending revision may parse and apply");
68+
69+
NSMutableArray<NSString *> *renders = [NSMutableArray new];
70+
NSMutableArray<NSString *> *applies = [NSMutableArray new];
71+
[coordinator
72+
scheduleRender:^BOOL {
73+
[renders addObject:@"in-flight"];
74+
[coordinator
75+
scheduleRender:^BOOL {
76+
[renders addObject:@"obsolete"];
77+
return YES;
78+
}
79+
apply:^{ [applies addObject:@"obsolete"]; }];
80+
[coordinator
81+
scheduleRender:^BOOL {
82+
[renders addObject:@"latest"];
83+
return YES;
84+
}
85+
apply:^{ [applies addObject:@"latest"]; }];
86+
return YES;
87+
}
88+
apply:^{ [applies addObject:@"in-flight"]; }];
89+
drain(background);
90+
drain(mainQueue);
91+
check([renders isEqual:@[ @"in-flight", @"latest" ]],
92+
"work arriving during a render must replace the pending revision");
93+
check([applies isEqual:@[ @"latest" ]], "a completed superseded render must never apply");
94+
95+
[coordinator
96+
scheduleRender:^BOOL {
97+
rendered++;
98+
return YES;
99+
}
100+
apply:^{ applied++; }];
101+
[coordinator invalidate];
102+
drain(background);
103+
drain(mainQueue);
104+
check(rendered == 1 && applied == 3, "recycling must discard pending parsing and application");
105+
106+
printf("PASS coordinator: latest pending revision, capture release, in-flight replacement, invalidation\n");
107+
}
108+
109+
static void testCache(void)
110+
{
111+
using namespace facebook::react;
112+
auto &cache = MeasurementCache::shared();
113+
CachedSize size;
114+
// Four duplicated 1 MiB strings exceed 8 MiB once key structs are included.
115+
MeasurementCacheKey keys[4]{};
116+
for (int index = 0; index < 4; index++)
117+
keys[index].markdown = std::string(1024 * 1024, char('a' + index));
118+
for (int index = 0; index < 3; index++)
119+
cache.set(keys[index], {double(index), 20});
120+
check(cache.get(keys[0], size), "a hit must remain available before budget eviction");
121+
cache.set(keys[3], {3, 20});
122+
check(!cache.get(keys[1], size), "byte overflow must evict the least recently used key");
123+
for (int index : {0, 2, 3})
124+
check(cache.get(keys[index], size) && size.width == index, "byte eviction must retain the other measurements");
125+
126+
// An oversized key must bypass storage without flushing useful entries.
127+
for (auto field : {&MeasurementCacheKey::markdown, &MeasurementCacheKey::lineBreakStrategyIOS,
128+
&MeasurementCacheKey::writingDirection, &MeasurementCacheKey::ellipsizeMode,
129+
&MeasurementCacheKey::linkRegexKey, &MeasurementCacheKey::inlineCodeLinkRegexKey}) {
130+
MeasurementCacheKey huge{};
131+
huge.*field = std::string(4 * 1024 * 1024, 'z');
132+
cache.set(huge, {99, 99});
133+
check(!cache.get(huge, size), "8 MiB includes both copies of every string and key structs");
134+
}
135+
for (int index : {0, 2, 3})
136+
check(cache.get(keys[index], size), "oversized keys must preserve useful measurements");
137+
138+
cache.set(keys[0], {123, 456});
139+
cache.removeMatchingMarkdown(keys[2].markdown);
140+
cache.set(keys[1], {1, 20});
141+
check(cache.get(keys[0], size) && size.width == 123 && size.height == 456,
142+
"updates and removals must release or reuse capacity without spurious eviction");
143+
check(cache.get(keys[3], size) && cache.get(keys[1], size), "removed capacity must be reusable");
144+
check(!cache.get(keys[2], size), "removed measurements must stay absent");
145+
for (const auto &key : keys)
146+
cache.removeMatchingMarkdown(key.markdown);
147+
148+
MeasurementCacheKey boundary{};
149+
const size_t budget = 8 * 1024 * 1024;
150+
const size_t structs = sizeof(MeasurementCache::CacheEntry) + sizeof(MeasurementCacheKey);
151+
boundary.markdown = std::string((budget - structs) / 2, 'b');
152+
cache.set(boundary, {8, 8});
153+
check(cache.get(boundary, size), "a key at the 8 MiB budget must fit");
154+
auto oversized = boundary;
155+
oversized.markdown += "b";
156+
cache.set(oversized, {9, 9});
157+
check(!cache.get(oversized, size), "a key above the 8 MiB budget must bypass storage");
158+
check(cache.get(boundary, size), "a rejected boundary key must not evict the cached key");
159+
cache.removeMatchingMarkdown(boundary.markdown);
160+
161+
MeasurementCacheKey key{};
162+
for (int entry = 0; entry < 512; entry++) {
163+
key.markdown = std::to_string(entry);
164+
cache.set(key, {double(entry), 1});
165+
}
166+
key.markdown = "0";
167+
check(cache.get(key, size), "the entry limit must still allow 512 small documents");
168+
key.markdown = "new";
169+
cache.set(key, {1000, 1});
170+
key.markdown = "1";
171+
check(!cache.get(key, size), "entry overflow must evict the least recently used document");
172+
key.markdown = "0";
173+
check(cache.get(key, size), "reading a cache hit must refresh its LRU position");
174+
printf("PASS cache: 8 MiB byte eviction, oversized keys, updates/removal, 512-entry LRU\n");
175+
}
176+
177+
int main(int argc, const char *argv[])
178+
{
179+
@autoreleasepool {
180+
if (argc == 1 || strcmp(argv[1], "cache") == 0)
181+
testCache();
182+
if (argc == 1 || strcmp(argv[1], "coordinator") == 0)
183+
testCoordinator();
184+
}
185+
}

0 commit comments

Comments
 (0)