Skip to content

interp: avoid repeated object clones on partial stores#5368

Open
jakebailey wants to merge 1 commit intotinygo-org:devfrom
jakebailey:interp-memory-cow
Open

interp: avoid repeated object clones on partial stores#5368
jakebailey wants to merge 1 commit intotinygo-org:devfrom
jakebailey:interp-memory-cow

Conversation

@jakebailey
Copy link
Copy Markdown
Member

memoryView.store cloned the entire destination object on every partial store. tsgo imports golang.org/x/text/collate and hits this badly. The generated tables are initialized one element at a time, and each element store copied the whole global, making interp quadratic in the table size.

By avoiding this copy until needed, we can greatly improve the perf.

Wall User CPU Peak RSS
dev 6:09.06 2280.14 s 12.44 GiB
This PR 3:52.46 430.69 s 10.79 GiB

https://jakebailey.dev/how-much-faster/#old=369&new=232 1.59x faster 😄

memoryView.store cloned the entire destination object on every partial
store. Compiling a program that pulls in golang.org/x/text/collate hit
this hard: its generated tables are initialized one element at a time,
and each element store copied the whole global, making interp quadratic
in the table size.

Switch to copy-on-first-write per memory view: clone the object the
first time this view writes to it, then mutate that private copy in
place on later partial stores. Rollback is unaffected because revert
still discards the view's objects wholesale.

Two extra safety tweaks fall out of this:

  - Full overwrites now clone the incoming value, so the stored buffer
    can never alias state held by the caller.
  - Partial in-place stores snapshot the source buffer, so a store
    whose source is a load from the same object (overlapping or not)
    still sees the pre-store bytes.

Add an interp golden test exercising both the overlapping load/store
case and a cross-object aliased load/store case.
@jakebailey jakebailey requested a review from dgryski May 5, 2026 05:50
@jakebailey
Copy link
Copy Markdown
Member Author

jakebailey commented May 5, 2026

I mistakenly benchmarked on non-wasip1 but the effect is about the same in absolute time (Wasm just has extra steps I have optimized in other places but not on this branch; other PRs coming)

@dgryski
Copy link
Copy Markdown
Member

dgryski commented May 5, 2026

Realizing I don't know enough about interp here.
/cc @niaow @aykevl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants