Skip to content
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

[ASTextKitRenderer] Adding locking when accessing the text renderer cache #2075

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/ASExperimentalFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ typedef NS_OPTIONS(NSUInteger, ASExperimentalFeatures) {
ASExperimentalDisableGlobalTextkitLock = 1 << 10, // exp_disable_global_textkit_lock
ASExperimentalMainThreadOnlyDataController = 1 << 11, // exp_main_thread_only_data_controller
ASExperimentalRangeUpdateOnChangesetUpdate = 1 << 12, // exp_range_update_on_changeset_update
ASExperimentalNoTextRendererCache = 1 << 13, // exp_no_text_renderer_cache
ASExperimentalLockTextRendererCache = 1 << 14, // exp_lock_text_renderer_cache
ASExperimentalFeatureAll = 0xFFFFFFFF
};

Expand Down
5 changes: 4 additions & 1 deletion Source/ASExperimentalFeatures.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
@"exp_optimize_data_controller_pipeline",
@"exp_disable_global_textkit_lock",
@"exp_main_thread_only_data_controller",
@"exp_range_update_on_changeset_update"]));
@"exp_range_update_on_changeset_update",
@"exp_no_text_renderer_cache",
@"exp_lock_text_renderer_cache"]));

if (flags == ASExperimentalFeatureAll) {
return allNames;
}
Expand Down
19 changes: 18 additions & 1 deletion Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ - (BOOL)isEqual:(ASTextNodeRendererKey *)object
we maintain a LRU renderer cache that is queried via a unique key based on text kit attributes and constrained size.
*/

static ASTextKitRenderer *rendererForAttributes(ASTextKitAttributes attributes, CGSize constrainedSize)
static ASTextKitRenderer *_rendererForAttributes(ASTextKitAttributes attributes, CGSize constrainedSize)
{
NSCache *cache = sharedRendererCache();

Expand All @@ -130,6 +130,23 @@ - (BOOL)isEqual:(ASTextNodeRendererKey *)object
return renderer;
}

static AS::RecursiveMutex __sharedRendererCacheInstanceLock__;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should this go at the top with the other locks? Or is the thinking to clean this up if it works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which other locks? Since these are static C functions I put the lock right next to them. Is there a better place to put them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me, I was looking at a different file 😅


static ASTextKitRenderer *rendererForAttributes(ASTextKitAttributes attributes, CGSize constrainedSize)
{
BOOL neverCache = ASActivateExperimentalFeature(ASExperimentalNoTextRendererCache);
if (neverCache) {
return [[ASTextKitRenderer alloc] initWithTextKitAttributes:attributes constrainedSize:constrainedSize];
}

BOOL lockCache = ASActivateExperimentalFeature(ASExperimentalLockTextRendererCache);
if (lockCache) {
AS::MutexLocker l(__sharedRendererCacheInstanceLock__);
return _rendererForAttributes(attributes, constrainedSize);
}
return _rendererForAttributes(attributes, constrainedSize);
}

#pragma mark - ASTextNodeDrawParameter

@interface ASTextNodeDrawParameter : NSObject {
Expand Down