perf: reduce per-query allocations on hot path#239
Open
Conversation
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
Reduces per-query allocations on the sqlds hot path. Every change is mechanical and preserves existing behaviour; the fleet-wide impact across ~15 Grafana SQL datasource plugins (snowflake, bigquery, clickhouse, databricks, redshift, mssql, postgres, mysql, oracle, saphana, athena, …) is proportional.
Note
Related: #240 —
perf: Plumb RowCapacityHint through DBQuery to presize frame fields. The two PRs are independent and can land in either order; #240 targets per-column slice growth insidesqlutil.FrameFromRows(and is blocked on grafana/grafana-plugin-sdk-go#1536), while this PR targets allocations in sqlds itself around it. The performance wins are additive.Five targeted changes:
defaultKeyonce onConnectorinstead offmt.Sprintfing it perGetConnectionFromQuery/Connectcall.driverSettings,Converters(), and the six optional mutator type assertions (QueryDataMutator,QueryMutator,ResponseMutator,QueryArgSetter,QueryErrorMutator,CheckHealthMutator) at construction; hoistsettings := ds.DriverSettings()at the top ofhandleQueryinstead of re-copying the struct 10× per query.applyHeaders'json.Unmarshal→ map →json.Marshalround-trip with a byte-level JSON-key injection fast path; fall back to the original code on malformed input. PreallocatefixFrameForLongToMulti's time slice to the known length.DriverSettings/Convertersdirect,Response_ConcurrentSet).TestMainsilencesbackend.Loggerso hclog doesn't interleave withgo testoutput and corrupt samples.Benchmark results
go test -bench=. -benchmem -count=10 -run=^$ ./...on Apple M2 Pro, Darwin arm64, benchstat comparison againstmain:sec/op
B/op
allocs/op
Benchmarks that show no significant change (
DriverSettings,HandleQuery_SettingsReads,DriverConverters,DefaultKey,Response_ConcurrentSet) were added as controls / deferred candidates — they were included in the harness before picking which fixes to land, and are kept so future regressions are detectable.Running the benchmarks and tests
go test ./...\go vet ./...\go test -bench=. -benchmem -count=10 -run=^$ ./...\before and after, compare the two sets with `benchstat`