diff --git a/Source/ASExperimentalFeatures.h b/Source/ASExperimentalFeatures.h index f6a6be4ee..8b54a4ad1 100644 --- a/Source/ASExperimentalFeatures.h +++ b/Source/ASExperimentalFeatures.h @@ -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 }; diff --git a/Source/ASExperimentalFeatures.mm b/Source/ASExperimentalFeatures.mm index 59467c89b..a5a10ebd6 100644 --- a/Source/ASExperimentalFeatures.mm +++ b/Source/ASExperimentalFeatures.mm @@ -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; } diff --git a/Source/ASTextNode.mm b/Source/ASTextNode.mm index 29ff9b844..dfe649612 100644 --- a/Source/ASTextNode.mm +++ b/Source/ASTextNode.mm @@ -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(); @@ -130,6 +130,23 @@ - (BOOL)isEqual:(ASTextNodeRendererKey *)object return renderer; } +static AS::RecursiveMutex __sharedRendererCacheInstanceLock__; + +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 {