Repository navigation
perf(cmd): build the command parser once per process - #333
Merged
Merged
Conversation
execute checked the command table, attached the tree and ran kong.New on every call. That build is almost the whole cost of a run that stops in Validate (12.5 ms without -race, 73 ms with it), and the internal/cmd tests run about 6000 such calls. execute now keeps one idle parser in a one-slot channel and reuses it. Each call sets the writers, the exit function and the version text of its run. A concurrent call that finds the slot empty builds its own parser. Kong's Parse zeroes every flag and positional before it applies the args, but it keeps Value.Set, Active and Flag.Negated, which the required-flag check, the existingfile mapper and the negatable bool read in the next Parse. parserSet.reset clears them before each reuse. make test with -race on 16 cores: 356 s before, 69 s after.
Owner
Author
|
Independent verification: PASS+NOTES
All runs used own worktrees, HOME and XDG_CONFIG_HOME in a temp dir, BROWSER=/bin/false, and no live Garmin calls. Gate (head, and head merged into main)
Binary vs base on the HTTPS mock
Leak tests (throwaway, in-process, on the merge, not committed)Each one runs
Injected defects (on the merge)
My own pair and sequence tests also fail for the Speed (same machine, 16 cores, load 2 to 6)
Notes
|
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
Every
executecall checked the command table, attached the tree and rankong.Newover all of it. That build is almost the whole cost of a run that stops in Validate (12.5 ms of 12.5 ms without-race, 73 ms with it), and theinternal/cmdtests run about 6000 such calls, so the four test shards ofmake testspent most of their time building the same parser.executenow keeps one idle parser in a one-slot channel and reuses it. Each call sets the writers, the exit function and the version text of its run on the parser. A concurrent call that finds the slot empty builds its own parser, so the 40-goroutine isolation test still holds without a lock.Kong's
Parsezeroes every flag and positional of every command before it applies the args (kong v1.16.1context.goContext.Reset,model.goValue.Reset), so the struct values of a run do not reach the next run. It does not clear three model fields that the nextParsereads:Value.Set(the required-flag check and theexistingfilemapper),Value.ActiveandNode.Active(theexistingfilemapper) andFlag.Negated(a--no-xflag flips the next--x).parserSet.resetclears them before each reuse. Without it, 1 golden case (wellness_menstrual-log_set@empty-day) and 2 of the new tests fail.What changed
executetakes the parser fromidleParseror builds one withnewParserSet, and returns it to the slot afterrun.versionFlag.BeforeResetreads the version through a bound*versionText, which each run sets.TestExecuteRejectsBadTableswaps the table throughswapCommands, which drops the idle parser.TestExecuteResetsFlagsBetweenRuns(pointer, list and negatable flags of run 1 do not reach run 2 or 3),TestExecuteResetsParseChecksBetweenRuns(a required flag and anexistingfilepositional are checked again in run 2) andTestCommandStateIsResetByParse(lists every unexported orkong:"-"field of the tree, which Parse does not reset, against a known list).Scope
Parse; unexported fields of a command keep their value across runs, and the known list inTestCommandStateIsResetByParsenames the four that exist today, all set on every run inValidate.Set,ActiveandNegatedis a Kong limitation for a reused parser. This PR works around it; an upstream fix is a separate matter.Verification
Same machine (16 cores, load 2 to 4 from other sessions), 5 runs per side, throwaway benchmark of
executeforactivities list --limit x:make test -j)make teston the branch, same setupgo test ./internal/cmd -run TestGoldenpasses without-update;git statusshows no golden change.p.reset()removed,TestExecuteResetsFlagsBetweenRunsand both cases ofTestExecuteResetsParseChecksBetweenRunsfail. With kong'sContext.Resetreplaced by a no-op (module replace),TestExecuteResetsFlagsBetweenRunsfails on the leaked--flow,--symptomsand--no-ovulationvalues.make fmt-check,make lint,go vet -tags e2e ./...,make test,make build && make conformance GCCLI_BIN=bin/gccli.