Skip to content

Conversation

@timkaechele
Copy link
Contributor

@timkaechele timkaechele commented Nov 5, 2025

This PR adds a new version of the hb_array called hb_narray that implements an array that can actually hold the elements it stores.

The existing hb_array only allows to store pointers to heap allocated elements, the new hb_narray acts as a container that also provides the backing memory for the elements it holds.

It's important to note that you can still make the elements pointers if you want to.

Why?

  • Improves locality of the elements
  • Reduces the number of individual heap allocations for elements

Next steps

  1. Get this merged
  2. Refactor existing hb_array usages
  3. Remove hb_array and rename hb_narray to hb_array

Memory layout

herb_new_array

@timkaechele timkaechele marked this pull request as ready for review November 5, 2025 07:23
@marcoroth
Copy link
Owner

I'm curious where you think this would come in handy. I think the problem is, at least for the AST Nodes, that they don't share the same item size, unless you have an array of a specific AST Node type.

@timkaechele timkaechele force-pushed the hb-new-array branch 3 times, most recently from 2b3ddaf to 6d903e3 Compare November 6, 2025 18:53
@timkaechele timkaechele mentioned this pull request Nov 6, 2025
4 tasks
@timkaechele timkaechele force-pushed the hb-new-array branch 3 times, most recently from 014444e to 3cd7856 Compare November 12, 2025 16:59
@timkaechele timkaechele force-pushed the hb-new-array branch 3 times, most recently from 2ac4ca6 to 75c0651 Compare December 12, 2025 16:17
Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

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

Thank you @timkaechele! 🙏🏼

@marcoroth marcoroth merged commit ba1d227 into marcoroth:main Dec 25, 2025
24 checks passed
@timkaechele timkaechele deleted the hb-new-array branch December 25, 2025 18:44
marcoroth added a commit that referenced this pull request Dec 29, 2025
…ge (#1025)

This pull request reverts #885 which caused memory issues in WebAssembly
by always allocating arrays, even when `NULL` was passed.

The `hb_array_size()` function safely returns `0` for `NULL` arrays,
eliminating the need for allocating arrays for all AST Nodes. This was
causing the `"Cannot enlarge memory"` errors in WASM builds, as seen in
#1016.

This pull request brings back `hb_array_size()` in places where we
previously accessed `array->size` directly. With that, we don't need to
allocate `array_init(8)` just so we can assume we always have a `size`.

The problem is, even if we would use `array_init(0)` it would allocate
memory that we are not going to need. So for now, I think it's better to
be avoid allocating arrays and use `hb_array_size()` until we have fully
integrated the new array implementation from #782. (/cc @timkaechele).

I also added a `hb_narray_size()` for API consistency that should help
with the future array migration.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants