From d41e5c15d83bd1c1427064ff5da840af3b50d3c1 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 18 Oct 2024 12:51:04 -0700 Subject: [PATCH] iOS: Eliminate needless profiler metrics ivar (flutter/engine#55957) iOS profiling is implemented in ProfileMetricsIOS, which has a default constructor, no 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. Further, the engine holds direct and indirect references to other UIKit objects such as `UITextInput` in FlutterTextInputPlugin, which must be dealloc'ed on the main 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 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../darwin/ios/framework/Source/FlutterEngine.mm | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 713d4fd0e2..b87195f59f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -125,7 +125,6 @@ static constexpr int kNumProfilerSamplesPerSec = 5; std::shared_ptr _platformViewsController; flutter::IOSRenderingAPI _renderingApi; - std::shared_ptr _profiler_metrics; std::shared_ptr _profiler; // Channels @@ -570,10 +569,13 @@ static constexpr int kNumProfilerSamplesPerSec = 5; - (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 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS result->_threadHost = _threadHost; result->_profiler = _profiler; - result->_profiler_metrics = _profiler_metrics; result->_isGpuDisabled = _isGpuDisabled; [result setUpShell:std::move(shell) withVMServicePublication:NO]; return [result autorelease];