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

Improve immutability of the data structure of the wasm module #501

Closed
wants to merge 1 commit into from

Conversation

andreaTP
Copy link
Collaborator

Opening in draft as I expect this PR to have impacts on performance.

@electrum would you like to propose improvements on top of this?

@andreaTP andreaTP requested a review from electrum August 29, 2024 17:36
@@ -127,15 +126,15 @@ public Instance initialize(boolean start) {
throw new InvalidException(
"type mismatch, invalid offset type in element " + offset.type());
}
List<List<Instruction>> initializers = ae.initializers();
// List<List<Instruction>> initializers = ae.initializers();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove

@@ -16,7 +16,7 @@ public long index() {
return idx;
}

public List<Instruction> offsetInstructions() {
return offsetInstructions;
public Instruction[] offsetInstructions() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why switch to an array? It's already returning an immutable list

@@ -38,7 +38,7 @@ public int tableIndex() {
/**
* @return a constant expression defining the offset into the table
*/
public List<Instruction> offset() {
return offset;
public Instruction[] offset() {
Copy link
Collaborator

@electrum electrum Sep 6, 2024

Choose a reason for hiding this comment

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

Same here, this is returning an immutable list

@@ -40,8 +40,12 @@ public int labelFalse() {
return labelFalse;
}

public List<Integer> labelTable() {
return labelTable;
public int labelTable(int idx) {
Copy link
Collaborator

@electrum electrum Sep 6, 2024

Choose a reason for hiding this comment

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

Hiding the list access is fine, but we do need to make the labelTable list immutable. After making it immutable, the hiding will not be needed.

@@ -32,16 +33,27 @@ public ValueType type() {
/**
* @return the list of instruction lists which are used to initialize each element in the range
*/
public List<List<Instruction>> initializers() {
return initializers;
public Instruction[] initializer(int idx) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should keep this as a list and change the defensive copy above to copy the inner lists (missed that earlier)

@@ -22,7 +22,7 @@ public OpCode opcode() {
}

public long[] operands() {
return operands;
return operands.clone();
Copy link
Collaborator

@electrum electrum Sep 6, 2024

Choose a reason for hiding this comment

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

For the interpreter, this results in an allocation per opcode with operands. It's possible the JIT could remove the allocation, but based on the benchmark result, it appears this doesn't happen.

I'll put up an alternative PR for this part: #517

@andreaTP
Copy link
Collaborator Author

@electrum now that we merged #517 should we close this one?
From your comments, looks like we only miss a couple of defensive copies of Lists, correct?

@electrum
Copy link
Collaborator

electrum commented Oct 1, 2024

I think most of these changes are separate from #517.

@andreaTP
Copy link
Collaborator Author

andreaTP commented Oct 1, 2024

Cool 👍 thanks for reviewing!

@andreaTP andreaTP closed this Oct 1, 2024
@electrum
Copy link
Collaborator

electrum commented Oct 1, 2024

What I meant is that we still need this PR, as most of the changes here are different from the ones in #517.

@andreaTP
Copy link
Collaborator Author

andreaTP commented Oct 1, 2024

Ouch, ok, I'll keep it on the Todo list.

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