Expensive cryptographic operations such like RSA key generation or large-block AES encryption can easily block single-threaded UI event loop and cause visible “jank” (missed frames, freezes).
This talk shares my journey optimizing the open-source webcrypto.dart library to eliminate jank during expensive cryptographic operations.
Below is how I plan to present my work. Each segment will be accompanied by a working demo - of the errors and of the fixes.
Presentation outline:
Motivation: Tracking Down UI Jank in Dart Apps: We open with the story of a Flutter app suffering from freezes during cryptographic operations. The audience will learn what UI jank is (missed frames and stutters) and why it’s particularly unacceptable in modern apps.
Profiling and Hotspot Analysis: In this segment, we delve into how we analyzed the performance hotspots. We describe measuring the execution time of cryptographic functions (like RSA key generation, ECDH key derivation, and large AES encryptions) and observing how these long-running tasks blocked the event loop.
Walk through Flutter’s frame pipeline: VSync -> beginFrame() -> build/layout/paint -> raster -> present [Ref. 2]
The key takeaway is identifying which cryptographic routines are the culprits behind the jank (RSA prime generation was a major offender, along with other intensive math operations).
Offloading expensive operations to Background Isolates: In this segment we'll move rsaKeyGen() into a background isolate so the main isolate [Ref. 3] can continue rendering and then we will introduce a new challenge - the issue with offloading FFI pointers to isolates.
FFI Pointer Transfer Challenge: In this section we'll explain why you can’t simply send a pointer across isolates and crash on attempt [Ref. 4]. This is often referred to as creating "cheap isolates" - cheap in the sense that you simply pass object reference instead of passing the object itself.
Then we will demonstrate the optimal approach where we only offload serializable objects. To wrap it up, we'll show timeline overlays and frame counters to prove “zero jank”.