⚡ Performance Optimization: avoid redundant globalState copy in notifySubscribers#27
Conversation
Currently, `notifySubscribers` calls `globalState.get()` for each subscriber, which generates a fresh copy of the state object every time due to the internal structure of the `globalState` wrapper. This patch caches `globalState.get()` into a local variable before the loop and passes that cached copy to every subscriber. This low-risk change improves performance by avoiding an allocation per subscriber in an environment where state does not change during the notification loop. Co-authored-by: johnstrand <11484777+johnstrand@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What: The
notifySubscribersloop now cachesglobalState.get()locally before the loop, passing the same cached state object to all subscribers rather than queryingglobalState.get()for each callback execution.🎯 Why: The
globalState.get()method copies and returns a new object representing the current state. When notifying many subscribers, this results in an unnecessary object allocation per subscriber even though the state is unchanging within the loop.📊 Measured Improvement: We introduced a benchmark (
benchmark.ts) measuring 100 update iterations on a single state variable with 10,000 subscribers.The change provides a >2x performance improvement in the update path due to avoiding GC/allocation overhead for thousands of objects.
PR created automatically by Jules for task 7970911372007205055 started by @johnstrand