Skip to content

Share only immutable data in the README performance example [patch] - #123

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/117-readme-optimized-cloning
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/117-readme-optimized-cloning

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #117

What changed

The README's OptimizedObject example cached _resource.DeepClone() and handed that one instance to every clone. As a result:

  • all clones shared a single mutable resource, so an edit through one clone showed up in all the later ones;
  • in-place edits to the original were never cloned, because only the setter cleared the cache.

I took the "share only when truly immutable, and say so" option:

  • Example. The expensive resource is now an immutable LookupTable with read-only members holding only an ImmutableArray<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.
  • Tip 1 ("Lazy Clone When Possible") is now "Share Immutable Data Instead of Cloning It".
  • Tip 3 ("Consider Caching") is now "Never Share or Cache Clones of Mutable Data", with the reason.

About the acceptance check: ReferenceEquals(a.Table, b.Table) is true by 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_AreIndependentOfEachOther runs the issue's repro against the new example. It checks that an edit through clone a doesn't reach clone b, that a clone taken after editing the original in place picks up the edit, and that replacing Table on one clone leaves the others alone.
  • Readme_ShowsTheTestedExample and Readme_DoesNotCacheAClonedMutableResource check that the README shows the tested example and no longer shows the _clonedResource cache. On main, 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

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
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 34ee710 into main Nov 4, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/117-readme-optimized-cloning branch November 4, 2026 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants