Move the default block to the front of the underlying jump table storage#5770
Conversation
| pub fn new(def: Block, table: &[Block]) -> Self { | ||
| Self { | ||
| table: std::iter::once(def).chain(table.iter().copied()).collect(), | ||
| } |
There was a problem hiding this comment.
While reviewing this I considered two alternative implementations but decided I like the one you've chosen best.
One alternative is to continue to accept a Vec and use table.insert(0, def), which implicitly does a memmove. Some callers already have a Vec allocated and it's a little annoying to have to allocate a second one to copy into, only for the caller to drop theirs immediately afterward.
The other alternative is to take table: impl IntoIterator<Item = Block>. That's somewhat appealing for cranelift/wasm/src/code_translator.rs which I think could then avoid allocating a temporary Vec. It also works fine for all the tests since arrays implement the right IntoIterator. Passing in a Vec by-value works too, although like the current approach that still allocates a new Vec and copies into it. However, I think this is a little more magic than necessary. Passing in a slice is fine.
The new api on
JumpTableDatamakese it easy to keep the default label first, and that shrinks the diff in #5731 a bit.