diff --git a/Source/ASExperimentalFeatures.h b/Source/ASExperimentalFeatures.h index a8b655ce9..be947c0b4 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 6113dc405..3a96d9ce3 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 8df442667..865fddf4f 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 {