Flutter Blog Aaron Clarke Jun 14, 2021

Improving Platform Channel Performance in Flutter

Article Summary

Aaron Clarke from Google's Flutter team eliminated unnecessary memory copies in platform channels and achieved up to 52% performance gains. Here's how he debugged his way through Objective-C class clusters and JNI limitations.

Platform channels are the bridge between Flutter and native iOS/Android code, but they were copying message data 4+ times during each round trip. Clarke spent months profiling, debugging compilation flags, and navigating memory management across C++, Objective-C, and Java to eliminate these bottlenecks.

Key Takeaways

Critical Insight

By removing superfluous data copies and transferring memory ownership directly to native platforms, Flutter platform channels now run up to 52% faster for binary data.

Clarke's next move: implementing FFI platform channels that skip thread hopping entirely and a custom MessageCodec for Pigeon that ditches reflection.

About This Article

Problem

Platform channel messages were being copied multiple times as they moved between Flutter and native code across C++, Objective-C, and Java. The data lived in a C++ std::vector inside PlatformMessage objects that used shared pointers, which made it impossible to safely transfer ownership.

Solution

Aaron Clarke switched PlatformMessage from shared pointers to unique pointers. He built a custom buffer class that could release std::vector ownership, then used NSData dataWithBytesNoCopy on iOS and direct ByteBuffers on Android to hand off memory ownership without creating extra copies.

Impact

iOS platform channels saw a 42% performance boost when handling 1MB binary payloads. Android local testing showed 52% gains with BinaryCodec.INSTANCE_DIRECT, but automated tests on older devices only showed 15% improvements.