Repository navigation
Share only immutable data in the README performance example [patch] - #123
Merged
Merged
Conversation
The optimized-cloning example cached one clone of a mutable resource and handed that same instance to every clone, so an edit through one clone showed up in the others and in-place edits to the original were never cloned. Share only an immutable resource by reference, clone the mutable state every time, and limit the caching tips to immutable data. Fixes #117 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW2HqCSuHeAzmHzLkRc
|
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.



Fixes #117
What changed
The README's
OptimizedObjectexample cached_resource.DeepClone()and handed that one instance to every clone. As a result:I took the "share only when truly immutable, and say so" option:
LookupTablewith read-only members holding only anImmutableArray<int>, and it is shared by reference. The mutable state (Scratch) is cloned on every call. A closing note says to build a new table to change it, and to clone the resource on every call if it has to stay mutable.About the acceptance check:
ReferenceEquals(a.Table, b.Table)istrueby design, because the shared object is immutable. No edit can be made through it, so nothing can leak between clones. All mutable state in the example gets a fresh copy (ReferenceEquals(a.Scratch, b.Scratch) == false).Tests
ReadmePerformanceExampleTests:OptimizedObject_Clones_AreIndependentOfEachOtherruns the issue's repro against the new example. It checks that an edit through cloneadoesn't reach cloneb, that a clone taken after editing the original in place picks up the edit, and that replacingTableon one clone leaves the others alone.Readme_ShowsTheTestedExampleandReadme_DoesNotCacheAClonedMutableResourcecheck that the README shows the tested example and no longer shows the_clonedResourcecache. Onmain, 5 of these cases fail. With this change they pass. I checked that by stashing the README change and running them again.Full suite: 72/72 passing on net10.0.
Related: #122 (README inheritance sample, #104) touches a different section of
README.md. The two PRs are independent and don't conflict.Generated by Claude Code