From 92c18508ccc7a24849a85fabf629596e36f66f3b Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 18 Oct 2024 11:47:28 -0700 Subject: [PATCH] iOS: Eliminate needless profiler metrics ivar iOS profiling is implemented in ProfileMetricsIOS, which has a default constructor, non member fields, and no superclass. i.e. it's roughly zero cost to create and holds no state. There's therefore no reason to make it an ivar on FlutterEngine. Further, making it an ivar on FlutterEngine means that the profiling sampler lambda needs to capture self which, in the case where all other FlutterEngine references go out of scope during sampling, means that the last FlutterEngine reference exists on a profiling thread, and when the sampling lambda block completes, the engine is dealloc'ed. The engine *must* be deallocated on the platform (main in Apple terminology) thread because FlutterEngine holds a reference to a PlatformViewsController, which owns a WeakPtrFactory. WeakPtrFactory's dtor asserts that it executes on the thread on which it was created, in this case, the platform thread. Since there's no need for ProfileMetricsIOS to be tied to the engine, we now create it on the fly in the sampler. No test changes because no semantic changes. Uncovered by ARC migration. Issue: https://github.com/flutter/flutter/issues/137801 Issue: https://github.com/flutter/flutter/issues/156177 --- .../darwin/ios/framework/Source/FlutterEngine.mm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 713d4fd0e288f..b87195f59f6ad 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -125,7 +125,6 @@ @implementation FlutterEngine { std::shared_ptr _platformViewsController; flutter::IOSRenderingAPI _renderingApi; - std::shared_ptr _profiler_metrics; std::shared_ptr _profiler; // Channels @@ -570,10 +569,13 @@ - (void)resetChannels { - (void)startProfiler { FML_DCHECK(!_threadHost->name_prefix.empty()); - _profiler_metrics = std::make_shared(); _profiler = std::make_shared( _threadHost->name_prefix.c_str(), _threadHost->profiler_thread->GetTaskRunner(), - [self]() { return self->_profiler_metrics->GenerateSample(); }, kNumProfilerSamplesPerSec); + []() { + flutter::ProfilerMetricsIOS profiler_metrics; + return profiler_metrics.GenerateSample(); + }, + kNumProfilerSamplesPerSec); _profiler->Start(); } @@ -1486,7 +1488,6 @@ - (FlutterEngine*)spawnWithEntrypoint:(/*nullable*/ NSString*)entrypoint result->_threadHost = _threadHost; result->_profiler = _profiler; - result->_profiler_metrics = _profiler_metrics; result->_isGpuDisabled = _isGpuDisabled; [result setUpShell:std::move(shell) withVMServicePublication:NO]; return [result autorelease];