Skip to content

Commit 62200f4

Browse files
committed
specs-go: don't use fmt.Sprintf to init Version
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). Using strconv.Itoa and string concatenation takes almost no time and a single 16-byte allocation. As a bonus, the package no longer imports fmt. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent bb96cde commit 62200f4

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

‎specs-go/version.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package specs
22

3-
import "fmt"
3+
import "strconv"
44

55
const (
66
// VersionMajor is for an API incompatible changes
@@ -15,4 +15,7 @@ const (
1515
)
1616

1717
// Version is the specification version that the package types support.
18-
var Version = fmt.Sprintf("%d.%d.%d%s", VersionMajor, VersionMinor, VersionPatch, VersionDev)
18+
//
19+
// It is built using strconv rather than fmt.Sprintf, since the latter is
20+
// expensive to run during package initialization.
21+
var Version = strconv.Itoa(VersionMajor) + "." + strconv.Itoa(VersionMinor) + "." + strconv.Itoa(VersionPatch) + VersionDev

0 commit comments

Comments
 (0)