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
- iOS platform channels saw 42% faster performance with 1MB payloads
- Android improved 52% locally using new BinaryCodec.INSTANCE_DIRECT codec
- Migrated from shared to unique pointers to safely pass data ownership
- StandardMessageCodec gains were smaller due to reflection overhead
- Direct ByteBuffers required new API to prevent memory access violations
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.
About This Article
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.
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.
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.