-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: optimize maps.Values and maps.Keys
This PR optimizes the performance of maps.Values and maps.Keys (around 125 usages in sidero code) by using the internal runtime functions runtime.keys and runtime.values. There is strong desire in Go Team to disable `go:linkname` usage in user code [^1], but this is a special case where we use `Push` `linkname` pattern which is said to be supported in the future. It is tested with both Go 1.22 and latest Go with CL 585556 [^2] applied. To further future-proof this code, we have added a build tag `go1.22 && !go1.24` to ensure this code is only compiled with Go 1.22 and 1.23 and falls back to the old implementation for Go 1.24 and above. This is because we don't know yet if `runtime.keys` and `runtime.values` are going to be present in Go 1.24. We will update this code when Go 1.24 freeze happens. Benchstat results below (overall 26% CPU usage reduction): ```bash ~ benchstat old.txt new.txt goos: darwin goarch: arm64 pkg: github.com/siderolabs/gen/maps │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Keys/small-10 111.05n ± 0% 80.74n ± 3% -27.29% (p=0.000 n=10) Keys/mid-10 837.8n ± 1% 607.8n ± 2% -27.46% (p=0.000 n=10) Keys/large-10 7.717µ ± 4% 5.711µ ± 0% -26.00% (p=0.000 n=10) Values/small-10 110.40n ± 0% 83.69n ± 0% -24.19% (p=0.000 n=10) Values/mid-10 835.9n ± 1% 583.5n ± 1% -30.19% (p=0.000 n=10) Values/large-10 7.720µ ± 2% 5.696µ ± 1% -26.22% (p=0.000 n=10) geomean 894.3n 653.6n -26.92% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ Keys/small-10 80.00 ± 0% 80.00 ± 0% ~ (p=1.000 n=10) ¹ Keys/mid-10 896.0 ± 0% 896.0 ± 0% ~ (p=1.000 n=10) ¹ Keys/large-10 8.000Ki ± 0% 8.000Ki ± 0% ~ (p=1.000 n=10) ¹ Values/small-10 80.00 ± 0% 80.00 ± 0% ~ (p=1.000 n=10) ¹ Values/mid-10 896.0 ± 0% 896.0 ± 0% ~ (p=1.000 n=10) ¹ Values/large-10 8.000Ki ± 0% 8.000Ki ± 0% ~ (p=1.000 n=10) ¹ geomean 837.4 837.4 +0.00% ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ Keys/small-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Keys/mid-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Keys/large-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Values/small-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Values/mid-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Values/large-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ geomean 1.000 1.000 +0.00% ¹ all samples are equal ``` [^1]: golang/go#67401 [^2]: https://go-review.googlesource.com/c/go/+/585556 Signed-off-by: Dmitriy Matrenichev <[email protected]>
- Loading branch information
Showing
13 changed files
with
238 additions
and
140 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
# syntax = docker/dockerfile-upstream:1.7.0-labs | ||
# syntax = docker/dockerfile-upstream:1.7.1-labs | ||
|
||
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. | ||
# | ||
# Generated on 2024-03-11T19:57:58Z by kres latest. | ||
# Generated on 2024-05-19T20:59:37Z by kres dccd292. | ||
|
||
ARG TOOLCHAIN | ||
|
||
# cleaned up specs and compiled versions | ||
FROM scratch AS generate | ||
|
||
# runs markdownlint | ||
FROM docker.io/node:21.6.2-alpine3.19 AS lint-markdown | ||
FROM docker.io/node:21.7.3-alpine3.19 AS lint-markdown | ||
WORKDIR /src | ||
RUN npm i -g [email protected] | ||
RUN npm i [email protected] | ||
|
@@ -40,9 +40,6 @@ RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/g | |
&& mv /go/bin/golangci-lint /bin/golangci-lint | ||
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/vuln/cmd/govulncheck@latest \ | ||
&& mv /go/bin/govulncheck /bin/govulncheck | ||
ARG GOIMPORTS_VERSION | ||
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/tools/cmd/goimports@${GOIMPORTS_VERSION} \ | ||
&& mv /go/bin/goimports /bin/goimports | ||
ARG GOFUMPT_VERSION | ||
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \ | ||
&& mv /go/bin/gofumpt /bin/gofumpt | ||
|
@@ -71,15 +68,12 @@ RUN --mount=type=cache,target=/go/pkg go list -mod=readonly all >/dev/null | |
FROM base AS lint-gofumpt | ||
RUN FILES="$(gofumpt -l .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumpt -w .':\n${FILES}"; exit 1) | ||
|
||
# runs goimports | ||
FROM base AS lint-goimports | ||
RUN FILES="$(goimports -l -local github.com/siderolabs/gen/ .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/siderolabs/gen/ .':\n${FILES}"; exit 1) | ||
|
||
# runs golangci-lint | ||
FROM base AS lint-golangci-lint | ||
WORKDIR /src | ||
COPY .golangci.yml . | ||
ENV GOGC 50 | ||
RUN golangci-lint config verify --config .golangci.yml | ||
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint --mount=type=cache,target=/go/pkg golangci-lint run --config .golangci.yml | ||
|
||
# runs govulncheck | ||
|
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
Oops, something went wrong.