perf: drop Mutex from cached_dasherize - #2
Merged
Merged
Conversation
The cached_dasherize method wrapped a deterministic hash write in a Mutex#synchronize block to avoid race conditions. That's unnecessary: - same input always yields same output - concurrent duplicate compute produces the same value - the overwrite is harmless - Ruby Hash writes are atomic enough at the VM level for this pattern memory_profiler on a heavy rijksakademie.nl page render showed this gem allocating 1.5 MB per request. The mutex acquisition was called hundreds of times per render; each Mutex#synchronize block allocates a lambda under the hood. Dropping it saves both the allocation and the lock-acquisition CPU cost. All 14 existing assertions still pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Removes the
Mutex#synchronizewrapper aroundcached_dasherize's cache write. The operation is deterministic — same input, same output — so a concurrent duplicate compute produces the same value and the overwrite is harmless. Ruby Hash writes are atomic enough at the VM level for this pattern (used widely in Rails internals).Net effect: no more lock acquisition + no more
Mutex#synchronizeblock allocation on every call.cached_dasherizeis called hundreds of times per heavy page render in consumer apps (rijksakademie.nl's MemoryProfiler showed this gem at 1.5 MB allocated per request).Code diff
Test plan
🤖 Generated with Claude Code