-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Lox’s semantics define a method invocation as two operations—accessing the method and then calling the result. The VM must support those as separate operations because the user can separate them. - You can access a method without calling it and then invoke the bound method later. - But always executing those as separate operations has a significant cost. Every single time a Lox program accesses and invokes a method, the runtime heap allocates a new ObjBoundMethod, initializes its fields, then pulls them right back out. Later, the GC has to spend time freeing all of those ephemeral bound methods. - Implement a new instruction OpInvoke which is used when a Lox program accesses a method and then immediately calls it. - Since the compiler can see this invocation (dotted property access followed by an opening parenthesis), this is a good opportunity for a superinstruction optimization. - In invoke() first we grab the receiver off the stack. The arguments passed to the method are above it on the stack, so we peek that many slots down. - We don’t need to heap allocate and initialize an ObjBoundMethod. In fact, we don’t even need to juggle anything on the stack. The receiver and method arguments are already right where they need to be. - In a microbenchmark whic does a batch of 10,000 method calls and tests how many of these batches the VM can execute in 10 seconds, there was a 7.6x improvement.
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ typedef enum | |
OpJumpIfFalse, | ||
OpLoop, | ||
OpCall, | ||
OpInvoke, | ||
OpClosure, | ||
OpCloseUpvalue, | ||
OpReturn, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters