From bb992667de3d98ab759f72403da4ff4057e7432d Mon Sep 17 00:00:00 2001 From: haxandmat <35293287+haxandmat@users.noreply.github.com> Date: Fri, 10 Dec 2021 00:14:06 +0300 Subject: [PATCH] Improved performance of the version.GetHumanVersion function by 50% on memory allocation. (#11507) Co-authored-by: Evan Culver --- .changelog/11507.txt | 3 +++ version/version.go | 6 +++--- version/version_test.go | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changelog/11507.txt create mode 100644 version/version_test.go diff --git a/.changelog/11507.txt b/.changelog/11507.txt new file mode 100644 index 000000000..e26ec07e5 --- /dev/null +++ b/.changelog/11507.txt @@ -0,0 +1,3 @@ +```release-note:improvement +version: Improved performance of the version.GetHumanVersion function by 50% on memory allocation. +``` \ No newline at end of file diff --git a/version/version.go b/version/version.go index 003d9fc44..13229b339 100644 --- a/version/version.go +++ b/version/version.go @@ -1,7 +1,6 @@ package version import ( - "fmt" "strings" ) @@ -29,9 +28,10 @@ func GetHumanVersion() string { release := VersionPrerelease if release != "" { - if !strings.HasSuffix(version, "-"+release) { + suffix := "-" + release + if !strings.HasSuffix(version, suffix) { // if we tagged a prerelease version then the release is in the version already - version += fmt.Sprintf("-%s", release) + version += suffix } } diff --git a/version/version_test.go b/version/version_test.go new file mode 100644 index 000000000..a7afb7dbc --- /dev/null +++ b/version/version_test.go @@ -0,0 +1,11 @@ +package version + +import ( + "testing" +) + +func BenchmarkGetHumanVersion(b *testing.B) { + for i := 0; i < b.N; i++ { + GetHumanVersion() + } +}