Coalese commands from RN Bridge
Taken from an email thread where a "scene tearing" issue was observed:
Roberto Morales Valencia says:
I noticed that checking/unchecking a Calendar with many elements in the surface can cause a delay on the re rendering of the checkbox. Originally, I thought this is a performance issue as it seemed like the thread is busy rendering the elements in the surface, then allowing the checkbox to be re rendered.
Andy Himberger says: As we get instructions from react over the bridge, we queue each one separately to run on the UI thread.
Keith says: I would definitely recommend coalescing the commands, for a couple reasons: • Scheduling of a UI thread tick is > 16 ms as the tick scheduler, which runs on a background thread, throttles to vsync, then uses CoreMessaging to wake up the UI thread to actually do the tick, which will take a bit more time (not sure how much). Ultimately this will translate into a lot of additional latency for large numbers of commands. • I think we should offer the same transactional guarantee to apps that XAML does (ie if you call a bunch of APIs synchronously to change the tree, those changes should show up in a single frame). We could look at opportunistically breaking batches but I think the starting point should be transactional. Since the app thread is separate from the UI thread, this already creates more parallelism than what exists for native XAML apps.
In the C# react-native-windows, we created EventDispatcher, which subscribes to CompositionTarget.Rendering and dispatches native events to JavaScript in batches.
oh interesting, I think Eric's comment is about combining things in the other direction too
https://github.com/microsoft/react-native-windows/pull/2481
There is a remaining issue here - we're still dispatching every command as it comes across the bridge. We should change this to gather up the commands and dispatch only once per batch.
I was curious about this so I looked at the precise more closely at one of the traces I already had taken.
Zooming into a 289ms window where i expected the UI Thread to be at 100% utilization (processing commands) I could see that the UI Thread spent 19ms in ready state, or ~6% of the total duration.
I think this is an interesting issue. It seems possible that dispatching each command might give the ui thread time to respond to handlers to user input. But it also is not ideal to leave 6% of total throughput on the table.

Let me know if you want the trace file. @kmelmon curious if you have any more thoughts on this given this data. Also curious if you have thought through how you would implement the batching in the thread queue.
@stecrain thanks for the data - pretty strong indicator the extra latency is measurable - I expect this to be even more compelling with larger trees. In addition, I talked this over with Scott Moore (owner of CoreMessaging), the guidance is to avoid dispatching many small work items as each item incurs multiple heap allocations.
I studied the code a bit, which works like this: -XAML has a central object, UIMessageQueueThread, which implements the dispatching of work items as they come over the bridge. It currently uses the CoreDispatcher for each work item. -At the end of a batch, we also dispatch a message via UIMessageQueueThread (see BridgeUIBatchInstanceCallback::onBatchComplete()). This is how we know a batch is complete on the UI thread.
My initial thought on what we could do here is to change UIMessageQueueThread (or create an alternate version if necessary) to do the following: -Instead of using CoreDispatcher to dispatch work, enqueue this work in our own queue. -When BridgeUIBatchInstanceCallback::onBatchComplete() is called, we tell the UIMessageQueueThread the batch is complete and it would dispatch a single message to the UI thread to signal it's time to process everything in the queue. This way there's only one dispatch per batch and we avoid extra heap allocations.
Since all enqueueing would be happening from the js thread, and all dequeueing on the UI thread, we'd need thread safety and a way to guarantee we only process what was in the queue at the time we know a batch is complete. This seems pretty easy to achieve as we're the ones that own the queue. Easy solution would be to have a queue of queues and a lock around it. Another way is "detach" the queue and send it along with the dispatched message as a parameter.
I'm not sure if all work is always dispatched within batches. If that's the case then we could change everything to do this coalescing. If there are cases where messages are dispatched outside of batches, we'd need to keep the current code for that work and only use the coalescing version for batches.
done!