Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't seem to work with wasm32-unknown-unknown #116

Closed
d3lm opened this issue Dec 7, 2023 · 7 comments
Closed

Doesn't seem to work with wasm32-unknown-unknown #116

d3lm opened this issue Dec 7, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@d3lm
Copy link

d3lm commented Dec 7, 2023

I tried to use scc for a crate that we compile to wasm32-unknown-unknown. I tried to use a HashIndex as a static item and while it compiles just fine, it crashes at runtime if I try to insert an item.

Here's what I did:

static HASH_INDEX: Lazy<HashIndex<u32, u32>> = Lazy::new(|| HashIndex::default());

#[wasm_bindgen)]
pub fn some_fn() -> Result<...> {
  HASH_INDEX.insert(1, 2)?;
}

It then crashes at runtime with the following error:

panicked at .../.cargo/registry/src/index.crates.io-6f17d22bba15001f/scc-2.0.4/src/hash_table/bucket_array.rs:55:13:
assertion `left == right` failed
  left: 16
 right: 0

Here's where it throws:

CleanShot 2023-12-07 at 17 44 32@2x

Any idea what could be causing this?

@Amanieu
Copy link

Amanieu commented Dec 7, 2023

Possibly related: calculate_memory_layout uses an alignment of 1 instead of the actual alignment:

Layout::from_size_align_unchecked(allocation_size, 1)

@d3lm
Copy link
Author

d3lm commented Dec 7, 2023

Oh yea, that is very good point indeed. The alignment is prolly wrong.

@wvwwvwwv
Copy link
Owner

wvwwvwwv commented Dec 7, 2023

Hi, thanks for reporting this issue!

The alignment is definitely different from the actual alignment, but that is intended; if the alignment is above a certain threshold (~4?), alloc_zeroed becomes awfully slow due to a call to memset, otherwise alloc_zeroed is simply translated into calloc which takes full advantage of demand paging.

I think the problem is related to wasm32 because this crate has never been tested on a 32-bit machine.

I'll try to fix it on this weekend!

@wvwwvwwv wvwwvwwv self-assigned this Dec 8, 2023
@wvwwvwwv
Copy link
Owner

wvwwvwwv commented Dec 8, 2023

Reproduced the issue and fixed it.

Cause: the size of the Bucket struct is no longer a power of two in a 32-bit binary.

I'll cleanup the change, add more tests, and then upload SCC 2.0.5 soon.

wvwwvwwv added a commit that referenced this issue Dec 8, 2023
@wvwwvwwv
Copy link
Owner

wvwwvwwv commented Dec 8, 2023

The commit message is wrong, but the issue was fixed in 33e4fc5

@wvwwvwwv
Copy link
Owner

wvwwvwwv commented Dec 8, 2023

Fixed in 2.0.5!

-> Theoretically, everything shall work as expected in a 32-bit binary.
-> If you find anything unexpected, please open a new issue, or reopen this one in case it's related to this.

@wvwwvwwv wvwwvwwv closed this as completed Dec 8, 2023
@d3lm
Copy link
Author

d3lm commented Dec 8, 2023

Oh wow, thanks so much for that insanely fast turnaround! I really appreciate it 🙏

@wvwwvwwv wvwwvwwv added the bug Something isn't working label Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants