Skip to content

Conversation

KKiiim
Copy link

@KKiiim KKiiim commented Sep 17, 2025

  • Reduce unnecessary zero-initialization memory in the constructor

  • According to wasm-spec, grown memories should initialized with value 0x00

  • I've read the contributing guidelines

  • I've added my name and email to the NOTICE file

@KKiiim KKiiim changed the title feat: remove the active init operation of zero-initialized memory of makeFieldInitializationInConstructor feat: optimize the active init operation of zero-initialized memory of makeFieldInitializationInConstructor Sep 17, 2025
Copy link
Member

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

optimization goal LGTM. But I think we should only check whether the field is straightly initialized by zero and ignore it.

@KKiiim
Copy link
Author

KKiiim commented Sep 25, 2025

@CountBleck Could you please have a look on this PR (^_^)

Copy link
Member

@CountBleck CountBleck left a comment

Choose a reason for hiding this comment

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

This seems mostly okay, but I'm wondering what all the removed $~lib/arraybuffer/ArrayBufferView#set:buffer is about...what is that setter function supposed to be doing anyway?

I'd like @MaxGraey to sign off before merging.

if (this.currentType != Type.void) { // in case
expr = module.drop(expr);

if (initializerNode) {
Copy link
Member

Choose a reason for hiding this comment

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

If there isn't an initializerNode, this code neglects to zero-initialize the field when stub runtime is used (and the stub runtime doesn't zero-initialize allocated memory!). The previous code's this.makeZero(fieldType) happened to cover this case.

Copy link
Author

Choose a reason for hiding this comment

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

Looks no need to zero-initialize because:

  1. memory will not be collected and reused in stub runtime.
  2. memory grow will default to initialized with 0 as wasm spec.

Copy link
Member

Choose a reason for hiding this comment

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

The exception is when the latest allocation is being freed. There's also the case when memory is imported (which means it's not necessarily zeroed), as well as the very niche case when the heap is reset (which also leaves the memory dirty upon future allocations).

You are correct in saying that memory.grow zeroes out the new memory, but the above cases still show why fields need to be zeroed when using the stub allocator.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I understand your point.
Perhaps the safest approach here is to simply disable this optimization in stub rt.

@KKiiim KKiiim requested a review from CountBleck October 2, 2025 07:01
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.

4 participants