Skip to content

specs-go: make Version a string literal - #1330

Merged
kolyshkin merged 2 commits into
opencontainers:mainfrom
kolyshkin:version-no-sprintf
Oct 16, 2026
Merged

kolyshkin merged 2 commits into
opencontainers:mainfrom
kolyshkin:version-no-sprintf

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Version is initialized during package initialization, which happens on every start of any program importing this package (for example, runc, which re-executes itself on every container start and exec).

Using fmt.Sprintf for that takes about 4µs and 5 allocations (2920 bytes). Since VersionMajor, VersionMinor, VersionPatch and VersionDev are all constants, there is no need to compute Version at runtime at all, so make it a plain string literal (as suggested by @thaJeztah in review).

Version is kept as a variable rather than a constant, since some users overwrite it (e.g. kata-containers), and making it a constant would break them.

To make sure Version stays in sync with the constants, a test is added, and CI now runs go test. The release checklist in RELEASES.md is updated accordingly.

As a bonus, the package no longer imports fmt.

@AkihiroSuda

Copy link
Copy Markdown
Member

Using fmt.Sprintf for that takes about 4µs and 5 allocations (2920 bytes).

Does it really matter?

Comment thread specs-go/version.go Outdated

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kolyshkin

Copy link
Copy Markdown
Contributor Author

Using fmt.Sprintf for that takes about 4µs and 5 allocations (2920 bytes).

Does it really matter?

Ever so slightly 🤏🏻
For some reason printf-like functions are not very optimized in Go.

@thaJeztah

Copy link
Copy Markdown
Member

For some reason printf-like functions are not very optimized in Go.

Yeah, there's perfsprint linter in golangci-lint, which flags various cases.

I think it's at least the parsing of the format-string that will always cause overhead, but I also have some recollection of trivial cases that stdlib probably could have a fast-path for, but didn't (which made me curious if there was some reason behind it, or just "nobody worked on improving it").

@thaJeztah

Copy link
Copy Markdown
Member

To Akihiro's point;

Does it really matter?

It's definitely not critical, but (even more so if it becomes just a string); does it hurt? Also "no" I think?

I think this is part of @kolyshkin trip round code used to see "what more could we squeeze out of this", and I really enjoyed many of his discoveries. Some small, some more substantial, some I knew about, but also some that were interesting.

@kolyshkin kolyshkin changed the title specs-go: don't use fmt.Sprintf to init Version specs-go: make Version a string literal Oct 15, 2026
There were no Go tests so far, but we are about to add one.

Add test target to Makefile, and run it from CI.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Comment thread specs-go/version.go Outdated
Version is initialized during package initialization, which happens on
every start of any program importing this package (for example, runc,
which re-executes itself on every container start and exec). Using
fmt.Sprintf for that takes about 4us and 5 allocations (2920
bytes).

Remove all this, making it a plain string literal. Kept as a variable
rather than a constant, since some users overwrite it, and changing it
to a constant would break them.

Add a test to make sure Version is in sync with the constants, and
update the release checklist accordingly.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kolyshkin
kolyshkin merged commit 469dd5b into opencontainers:main Oct 16, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants