Severe lag adding PKStroke to PKDrawing

We've been using PencilKit in our app for a couple of years now, but have run into a significant slowdown recently. It coincided with a shift to iPadOS 17, but possibly that's a coincidence.

It seems to occur when we add PKStroke elements to a PKDrawing (programatically). Previously this has refreshed on-screen instantly, but now it's so slow we can sometime see the new stroke progressively drawing on-screen. I profiled the running app and the lag seems to entirely occur within the following call sequence:

2563	-[PKDrawingConcrete addNewStroke:]	
2494	-[PKDrawing setNeedsRecognitionUpdate]	
2434	-[NSUserDefaults(NSUserDefaults) boolForKey:]	
2419	_CFPreferencesGetAppBooleanValueWithContainer	
2417	_CFPreferencesCopyAppValueWithContainerAndConfiguration	
2399	-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]	
2395	-[_CFXPreferences withSearchListForIdentifier:container:cloudConfigurationURL:perform:]	
2394	normalizeQuintuplet	
2367	__108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke	
2338	__76-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]_block_invoke	
2336	-[CFPrefsSource copyValueForKey:]	
2332	-[CFPrefsSearchListSource alreadylocked_copyValueForKey:]	
1924	-[CFPrefsSearchListSource alreadylocked_copyValueForKey:].cold.1	
1921	_os_log_debug_impl	
1918	_os_log	
1914	_os_log_impl_flatten_and_send	
1829	_os_log_impl_stream	
1811	_os_activity_stream_reflect	
1205	dispatch_block_perform	
1199	_dispatch_block_invoke_direct	
1195	_dispatch_client_callout	
1194	___os_activity_stream_reflect_block_invoke	
1188	_xpc_pipe_simpleroutine	
882	_xpc_send_serializer	
867	_xpc_pipe_mach_msg	
859	mach_msg	
859	mach_msg_overwrite	
855	mach_msg2_internal	
853	0x1cf7701d8	

Up to 'addNewStroke' this makes sense. But where is it going with 'PKDrawing setNeedsRecognitionUpdate'? This ultimately seems to be hitting some kind of lock, which I assume is the cause of the lag.

Any suggestions for why this is happening or a means of avoiding it would be much appreciated.

Post not yet marked as solved Up vote post of rwessel Down vote post of rwessel
359 views