Skip to content

Implement the semi-space copying garbage collector#13107

Open
fitzgen wants to merge 8 commits intobytecodealliance:mainfrom
fitzgen:initial-impl-copying-collector
Open

Implement the semi-space copying garbage collector#13107
fitzgen wants to merge 8 commits intobytecodealliance:mainfrom
fitzgen:initial-impl-copying-collector

Conversation

@fitzgen
Copy link
Copy Markdown
Member

@fitzgen fitzgen commented Apr 15, 2026

This is a classic semi-space copying collector that uses bump allocation and Cheney-style worklists to avoid an explicit stack for grey (relocated but not yet scanned) objects outside the GC heap. Forwarding "pointers" (really VMGcRef indices) are stored inline in objects in the old semi-space, which again avoids explicit data structures outside of the GC heap. Furthermore, an intrusive linked-list of all externrefs is maintained to allow for efficiently sweeping their associated host data after collection (and, once again, avoiding additional data structures outside the GC heap).

Allocation in compiled Wasm code always happens by calling out to the gc_alloc_raw libcall currently. This is expected to become inline bump allocation, very similar to the null collector, in a follow-up commit shortly after this one. It is delayed to make review easier.

Collection is not incremental (in the Wasmtime sense, not the GC literature sense) yet and the CopyingCollection::collect_increment implementation only has a single increment. This is delayed to make review easier and is also expected to come shortly in follow-up commits.

I've also added new disas tests for the copying collector and also enabled running wast tests which need a GC heap with the copying collector.

Finally, fuzz config generation can now generate configurations that enable the copying collector.

This is a classic semi-space copying collector that uses bump allocation and
Cheney-style worklists to avoid an explicit stack for grey (relocated but not
yet scanned) objects outside the GC heap. Forwarding "pointers" (really
`VMGcRef` indices) are stored inline in objects in the old semi-space, which
again avoids explicit data structures outside of the GC heap. Furthermore, an
intrusive linked-list of all `externref`s is maintained to allow for efficiently
sweeping their associated host data after collection (and, once again, avoiding
additional data structures outside the GC heap).

Allocation in compiled Wasm code always happens by calling out to the
`gc_alloc_raw` libcall currently. This is expected to become inline bump
allocation, very similar to the null collector, in a follow-up commit shortly
after this one. It is delayed to make review easier.

Collection is not incremental (in the Wasmtime sense, not the GC literature
sense) yet and the `CopyingCollection::collect_increment` implementation only
has a single increment. This is delayed to make review easier and is also
expected to come shortly in follow-up commits.

I've also added new disas tests for the copying collector and also enabled
running wast tests which need a GC heap with the copying collector.

Finally, fuzz config generation can now generate configurations that enable the
copying collector.
@fitzgen fitzgen requested review from a team as code owners April 15, 2026 14:28
@fitzgen fitzgen requested review from alexcrichton and removed request for a team April 15, 2026 14:28
@github-actions github-actions bot added fuzzing Issues related to our fuzzing infrastructure wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime labels Apr 15, 2026
@github-actions
Copy link
Copy Markdown

Subscribe to Label Action

cc @fitzgen

Details This issue or pull request has been labeled: "fuzzing", "wasmtime:api", "wasmtime:ref-types"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: fuzzing, wasmtime:ref-types

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Copy Markdown
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think this looks fantastic, thanks so much!

I've got some minor comments below, but I'm also happy to defer anything to issues as you see fit. My rough assumption as well is that long-term we'll want nurseries as well, right? If that's the case would it make sense to document that in an issue as an open future work item?

Comment thread tests/disas/gc/copying/v128-fields.wat
Comment thread crates/cranelift/src/func_environ/gc/enabled/copying.rs
Comment thread crates/cranelift/src/func_environ/gc/enabled/copying.rs
Comment thread crates/cranelift/src/func_environ/gc/enabled/copying.rs
Comment thread crates/cranelift/src/func_environ/gc/enabled/copying.rs
Comment thread crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs Outdated
Comment thread crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs Outdated
Comment thread crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs Outdated
Comment thread crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs Outdated
Comment thread crates/wasmtime/src/runtime/vm/gc/enabled/copying.rs Outdated
@alexcrichton
Copy link
Copy Markdown
Member

Oh, also, to kick the tire some more I might recommend locally making the copying collector the default (instead of drc) and running the full test suite. Iunno if that'll turn up much but might be good as a first pass

@fitzgen
Copy link
Copy Markdown
Member Author

fitzgen commented Apr 15, 2026

My rough assumption as well is that long-term we'll want nurseries as well, right? If that's the case would it make sense to document that in an issue as an open future work item?

Not totally clear to me right now. A nursery probably isn't worth it for our typical short-running programs, since we can mostly just avoid collecting in those cases, and would only slow down wasm execution via the write barriers it would require. (Also, our amortized GC heap growth algorithm effectively means we have N nursery collections on the way to the full heap size, if you squint hard.)

But someone with long-running wasm instance use cases may indeed want a nursery. But then we also wouldn't want to force a nursery on the short-running use cases that don't need it.

Seems like the kind of bridge we can cross when we get to it. I wouldn't go so far as to say that we should expect it to happen with enough certainty that a tracking issue is required at this time.

@fitzgen fitzgen requested a review from a team as a code owner April 15, 2026 21:30
@fitzgen fitzgen requested review from uweigand and removed request for a team April 15, 2026 21:30
@fitzgen fitzgen enabled auto-merge April 15, 2026 21:31
@fitzgen
Copy link
Copy Markdown
Member Author

fitzgen commented Apr 15, 2026

Adding this to the merge queue now, but we can definitely continue with some of these other bits in follow ups.

@fitzgen fitzgen added this pull request to the merge queue Apr 15, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fuzzing Issues related to our fuzzing infrastructure wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants