Skip to content

fix: Declared element section output is hash unstable - #308

Closed
logan-gatlin wants to merge 1 commit into
mainfrom
output-stability
Closed

logan-gatlin wants to merge 1 commit into
mainfrom
output-stability

Conversation

@logan-gatlin

Copy link
Copy Markdown
Contributor

No description provided.

@guybedford

Copy link
Copy Markdown
Contributor

Does this fix the long-standing issues we've had here, or is it new?

@guybedford

Copy link
Copy Markdown
Contributor

Curious if there's a perf implication of this worth considering? In which case the benefit should be carefully justified?

@logan-gatlin

logan-gatlin commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Yeah thats true. Will benchmark it. It would actually be better to do insertion order if that would be stable

@wasm-bindgen-bot wasm-bindgen-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR sorts the undeclared ref.func targets before ensure_func_declarations puts them into the synthesized Declared element segment. Until now that segment came out in IdHashSet iteration order, which depends on insertion order and table capacity (#330 had to avoid extend to keep the order). I'm guessing the aim is byte-identical output for wasm-bindgen builds. The fix itself is correct: id_arena::Id implements Ord, and all IDs come from the one function arena, so the result is a deterministic order by arena index. The arena index is not always the emitted function index, but the output is deterministic either way. Still missing: the NB comment from #330 above step 1 is now stale, there is no CHANGELOG.md entry, and no test pins the new ordering.

Not merge-ready:

  • There is no Fixed entry under Unreleased in CHANGELOG.md for the now-deterministic Declared element segment order.
  • No test pins the new order. For example, two modules whose ref.funcs name the same undeclared functions in different orders should emit the same declared segment.

Comment thread src/module/mod.rs
.collect();
// Sort to ensure we emit the Declared element segment deterministically,
// regardless of IdHashSet iteration order.
undeclared.sort_unstable();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The NB comment above step 1 (lines 345–348) is now wrong.
It says the undeclared functions are listed in the set's iteration order, so the set must be built one insert at a time without extend. After this sort, neither is true. A maintainer who reads it will keep working around a constraint that no longer exists.

Drop the NB sentence. If you want to keep a note, say that the segment is sorted, so the order in which ref_funcs is built doesn't matter.

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