⚡️ Speed up method InMemoryNodeState.verify_token by 12%
#35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 12% (0.12x) speedup for
InMemoryNodeState.verify_tokeninframework/py/flwr/supernode/nodestate/in_memory_nodestate.py⏱️ Runtime :
1.08 milliseconds→965 microseconds(best of201runs)📝 Explanation and details
The optimization replaces the
withstatement lock context manager with explicitacquire()/release()calls and moves the comparison operation outside the critical section.Key changes:
acquire()andrelease()instead ofwithstatement to avoid the overhead of context manager protocoltoken_store.get(run_id)) happens inside the lock, but the comparison (value == token) is performed outside the lockWhy it's faster:
withstatement invokes__enter__and__exit__methods, adding function call overhead. Directacquire()/release()eliminates thisPerformance characteristics:
The optimization is most effective for workloads with high-frequency token verification calls, as demonstrated by the test cases with large token stores and many verification attempts. The 11% speedup is achieved through reduced per-call overhead in this critical path function.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-InMemoryNodeState.verify_token-mh9zuts0and push.