⚡️ Speed up method UsersGroupsDataSource.users_user_presence_clear_presence by 9%
#206
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.
📄 9% (0.09x) speedup for
UsersGroupsDataSource.users_user_presence_clear_presenceinbackend/python/app/sources/external/microsoft/users_groups/users_groups.py⏱️ Runtime :
1.11 milliseconds→1.02 milliseconds(best of125runs)📝 Explanation and details
The optimized code achieves an 8% runtime improvement by eliminating object allocation overhead in the
users_user_presence_clear_presencemethod.Key optimizations:
Reduced object instantiation: Instead of creating two separate
RequestConfiguration()instances (one forquery_paramsand one forconfig), the optimized version creates only one instance and reuses it viaquery_params = config.Streamlined header handling: The original code unconditionally created a headers dictionary when search was present. The optimized version uses a more efficient pattern - it checks if headers already exist before creating a new dictionary, reducing unnecessary allocations.
Direct attribute assignment: Parameters are assigned directly to the single
configobject instead of being assigned to a separatequery_paramsobject and then copied over.Why this matters for performance:
RequestConfiguration()call took 561,013 nanoseconds vs 600,856 in the optimized version, but the overall method execution improved due to eliminating the second instantiationTest case performance:
The optimization is most effective for high-frequency scenarios like the throughput tests (
test_users_user_presence_clear_presence_throughput_*) where the same method is called repeatedly. While throughput remained stable at 71,125 operations/second, the 8% runtime reduction means lower CPU utilization per operation, allowing for better resource efficiency under sustained load.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-UsersGroupsDataSource.users_user_presence_clear_presence-mh5zd4ozand push.