Over the past couple of years, I’ve been interested in the problem of “How do we make communication between Flutter and its host platform faster and easier?” This is a question of particular interest to Flutter plugin developers and add-to-app developers.
Communication between Flutter and the host platform is typically done with platform channels, so my energy has been focused there. In late 2019, to remedy the amount of boilerplate and stringly typed code required to use platform channels, I designed a codegen
package, Pigeon, that makes platform channels type-safe, and the team continues to improve it. In spring 2020, I performed an audit of platform channel and foreign function interface (FFI) performance. Now, I’ve set my sights on improving the performance of platform channels. Because Pigeon is built on top of platform channels, and I plan to build a data synchronization solution for multiple Flutter instances on top of Pigeon, this is a good opportunity to help with the many different needs of developers and with my initiatives.
After some investigation, I was able to identify superfluous copies of the data being sent over platform channels and was able to remove them. Below you’ll find the results of that change and an overview of the work that lead to identifying and removing the copies.
After removing the superfluous copies when sending 1 MB of binary data from Flutter to the host platform with a 1 MB response, we saw an approximately 42% increase in performance on iOS. On Android, the results were a bit more nuanced. Our automated performance tests improved approximately 15% while local testing saw an approximately 52% increase when migrating to the new BinaryCodec.INSTANCE_DIRECT
codec. This discrepancy might be because the automated performance tests are running on an older device, but the discrepancy could be an artifact of how the microbenchmarks, in particular, perform on an older device (for example, hammering the garbage collector). You can find the automated performance test’s source code at platform_channels_benchmarks/lib/main.dart.
#dart #flutter-plugin #flutter