forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 57
perf: replace RLock with Lock where re-entrant locking is not needed (~11ns saving, -14%) #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mykaul
wants to merge
2
commits into
scylladb:master
Choose a base branch
from
mykaul:perf/rlock-to-lock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Copyright ScyllaDB, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Micro-benchmark: orphaned request lock skip in process_msg. | ||
|
|
||
| Measures the cost of always acquiring a lock vs checking the set first. | ||
|
|
||
| Run: | ||
| python benchmarks/micro/bench_orphan_lock_skip.py | ||
| """ | ||
|
|
||
| import sys | ||
| import timeit | ||
| from threading import Lock | ||
|
|
||
|
|
||
| def bench(): | ||
| n = 2_000_000 | ||
| lock = Lock() | ||
| orphaned_set = set() # empty — the common case | ||
| stream_id = 42 | ||
| in_flight = 100 | ||
|
|
||
| # Old: always acquire lock | ||
| def old_check(): | ||
| nonlocal in_flight | ||
| with lock: | ||
| if stream_id in orphaned_set: | ||
| in_flight -= 1 | ||
| orphaned_set.remove(stream_id) | ||
|
|
||
| # New: check set first, skip lock if empty | ||
| def new_check(): | ||
| nonlocal in_flight | ||
| if orphaned_set: | ||
| with lock: | ||
| if stream_id in orphaned_set: | ||
| in_flight -= 1 | ||
| orphaned_set.remove(stream_id) | ||
|
|
||
| print(f"=== orphaned request lock skip ({n:,} iters) ===\n") | ||
|
|
||
| # Warmup | ||
| for _ in range(10000): | ||
| old_check() | ||
| new_check() | ||
|
|
||
| t_old = timeit.timeit(old_check, number=n) | ||
| t_new = timeit.timeit(new_check, number=n) | ||
| ns_old = t_old / n * 1e9 | ||
| ns_new = t_new / n * 1e9 | ||
| saving = ns_old - ns_new | ||
| speedup = ns_old / ns_new if ns_new > 0 else float('inf') | ||
| print(f" Empty orphaned set (common case):") | ||
| print(f" Old (always lock): {ns_old:.1f} ns") | ||
| print(f" New (check first): {ns_new:.1f} ns") | ||
| print(f" Saving: {saving:.1f} ns ({speedup:.2f}x)") | ||
|
|
||
| # Non-empty set (rare case) — should still work | ||
| orphaned_set_full = {99, 100, 101} | ||
| def old_check_full(): | ||
| nonlocal in_flight | ||
| with lock: | ||
| if stream_id in orphaned_set_full: | ||
| in_flight -= 1 | ||
| orphaned_set_full.remove(stream_id) | ||
|
|
||
| def new_check_full(): | ||
| nonlocal in_flight | ||
| if orphaned_set_full: | ||
| with lock: | ||
| if stream_id in orphaned_set_full: | ||
| in_flight -= 1 | ||
| orphaned_set_full.remove(stream_id) | ||
|
|
||
| for _ in range(10000): | ||
| old_check_full() | ||
| new_check_full() | ||
|
|
||
| t_old = timeit.timeit(old_check_full, number=n) | ||
| t_new = timeit.timeit(new_check_full, number=n) | ||
| ns_old = t_old / n * 1e9 | ||
| ns_new = t_new / n * 1e9 | ||
| diff = ns_new - ns_old | ||
| print(f"\n Non-empty orphaned set (rare case):") | ||
| print(f" Old (always lock): {ns_old:.1f} ns") | ||
| print(f" New (check first): {ns_new:.1f} ns") | ||
| print(f" Overhead: {diff:.1f} ns (extra truthiness check)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(f"Python {sys.version}\n") | ||
| bench() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Copyright DataStax, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Micro-benchmark: RLock vs Lock acquire/release overhead. | ||
|
|
||
| Measures the performance difference between threading.RLock and | ||
| threading.Lock for non-recursive lock acquisition patterns. | ||
|
|
||
| Run: | ||
| python benchmarks/micro/bench_rlock_vs_lock.py | ||
| """ | ||
| import timeit | ||
| from threading import Lock, RLock | ||
|
|
||
|
|
||
| def bench_lock_types(): | ||
| """Compare Lock vs RLock acquire/release cycles.""" | ||
| lock = Lock() | ||
| rlock = RLock() | ||
|
|
||
| n = 2_000_000 | ||
|
|
||
| def use_lock(): | ||
| lock.acquire() | ||
| lock.release() | ||
|
|
||
| def use_rlock(): | ||
| rlock.acquire() | ||
| rlock.release() | ||
|
|
||
| def use_lock_with(): | ||
| with lock: | ||
| pass | ||
|
|
||
| def use_rlock_with(): | ||
| with rlock: | ||
| pass | ||
|
|
||
| t_lock = timeit.timeit(use_lock, number=n) | ||
| t_rlock = timeit.timeit(use_rlock, number=n) | ||
|
|
||
| print(f"Lock acquire/release ({n} iters): {t_lock:.3f}s ({t_lock / n * 1e9:.1f} ns/cycle)") | ||
| print(f"RLock acquire/release ({n} iters): {t_rlock:.3f}s ({t_rlock / n * 1e9:.1f} ns/cycle)") | ||
| print(f"RLock overhead: {(t_rlock / t_lock - 1) * 100:.0f}% ({t_rlock / t_lock:.2f}x)") | ||
|
|
||
| t_lock_with = timeit.timeit(use_lock_with, number=n) | ||
| t_rlock_with = timeit.timeit(use_rlock_with, number=n) | ||
|
|
||
| print(f"\nLock 'with' stmt ({n} iters): {t_lock_with:.3f}s ({t_lock_with / n * 1e9:.1f} ns/cycle)") | ||
| print(f"RLock 'with' stmt ({n} iters): {t_rlock_with:.3f}s ({t_rlock_with / n * 1e9:.1f} ns/cycle)") | ||
| print(f"RLock overhead: {(t_rlock_with / t_lock_with - 1) * 100:.0f}% ({t_rlock_with / t_lock_with:.2f}x)") | ||
|
|
||
|
|
||
| def main(): | ||
| bench_lock_types() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.