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

Make Instruction class immutable #517

Merged
merged 1 commit into from
Sep 10, 2024
Merged

Conversation

electrum
Copy link
Collaborator

@electrum electrum commented Sep 7, 2024

This keeps the long[] to avoid boxing, which might have a performance impact for the interpreter. It also adds a constant for the common case of an empty array, which avoids allocations and reduces memory usage.

The extra clone for instructions created during parsing could be eliminated by adding a trusted package-private constructor, but I hope we don't need to optimize to that level.

Copy link
Collaborator

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

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

I like those changes!
Unfortunately, it looks like they are introducing a noticeable performance regression:
https://github.com/dylibso/chicory/actions/runs/10754183983?pr=517

Screenshot from 2024-09-09 10-19-00

It would be great if you can verify locally if the data is accurate or we are just looking at different machines noise levels.

@electrum
Copy link
Collaborator Author

electrum commented Sep 9, 2024

I missed a critical place in the interpreter that calls operands() and passes the array to the static methods.

@electrum
Copy link
Collaborator Author

electrum commented Sep 9, 2024

This should be fixed now. I added a private Operands interface in InterpreterMachine that is functionally identical to IntToLongFunction, but makes the code read better (the latter looked ugly).

@electrum
Copy link
Collaborator Author

Screenshot 2024-09-09 at 9 27 41 PM

@bhelx
Copy link
Contributor

bhelx commented Sep 10, 2024

good catch @andreaTP :)

Copy link
Collaborator

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

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

Thanks for the double check and the quick fix @electrum

var operands = instruction.operands();
instance.onExecution(instruction, operands, stack);
Operands operands = instruction::operand;
instance.onExecution(instruction, stack);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice clean up!
operands was redundant here.

@@ -1982,4 +1982,9 @@ private static void checkInterruption() {
throw new ChicoryException("Thread interrupted");
}
}

@FunctionalInterface
private interface Operands {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this trick!

@andreaTP andreaTP merged commit ebf4ddf into dylibso:main Sep 10, 2024
13 checks passed
@electrum electrum deleted the instruction branch September 11, 2024 01:40
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.

3 participants