Split giant VM type-switch functions into per-node methods - #376
Merged
Conversation
invokeExpr (787 lines), runSingleStmt (767), invokeLetExpr (378) and invokeOperator (279) each held their entire logic in a single type switch. Keep the switches as thin dispatchers and move each case body into its own method taking the typed AST node, matching the existing funcExpr/callExpr style. ForStmt's three loop kinds and let-ItemExpr's three item kinds are split one level further. No behavior change. Benchmarks improve across the board (geomean -12%, Loop -29%) since the smaller stack frames make the recursive dispatch cheaper.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The four largest functions in the VM held all their logic inside a single giant type switch:
invokeExpr(vmExpr.go)runSingleStmt(vmStmt.go)invokeLetExpr(vmLetExpr.go)invokeOperator(vmOperator.go)The type switches now only dispatch; each case body moved into its own method taking the typed AST node (e.g.
invokeArrayExpr(expr *ast.ArrayExpr)), matching the existingfuncExpr/callExprnaming. Two hotspots are split one level further:ForStmt→runForSliceStmt/runForMapStmt/runForChanStmtItemExpr→invokeLetItemSlice/invokeLetItemMap/invokeLetItemStringThis is a pure code move — no behavior change. The
defer recoverFunc(runInfo)panic capture inChanExpr/CloseStmtkeeps its effective scope, and the for-loops still preservervonErrReturn.Benchmarks
No regression — everything got faster (benchstat, n=6): the much smaller stack frames make the recursive dispatch cheaper.
Testing
go build ./...,go vet ./vm/go test ./...— all packages passgo test -race ./vm/— pass