diff --git a/version.go b/version.go index a8acef7..f7c038f 100644 --- a/version.go +++ b/version.go @@ -4,7 +4,6 @@ package version import ( - "bytes" "database/sql/driver" "fmt" "regexp" @@ -382,22 +381,29 @@ func (v *Version) Segments64() []int64 { // missing parts (1.0 => 1.0.0) will be made into a canonicalized form // as shown in the parenthesized examples. func (v *Version) String() string { - var buf bytes.Buffer - fmtParts := make([]string, len(v.segments)) + return string(v.bytes()) +} + +func (v *Version) bytes() []byte { + var buf []byte for i, s := range v.segments { - // We can ignore err here since we've pre-parsed the values in segments - str := strconv.FormatInt(s, 10) - fmtParts[i] = str + if i > 0 { + buf = append(buf, '.') + } + buf = strconv.AppendInt(buf, s, 10) } - fmt.Fprintf(&buf, "%s", strings.Join(fmtParts, ".")) + if v.pre != "" { - fmt.Fprintf(&buf, "-%s", v.pre) + buf = append(buf, '-') + buf = append(buf, v.pre...) } + if v.metadata != "" { - fmt.Fprintf(&buf, "+%s", v.metadata) + buf = append(buf, '+') + buf = append(buf, v.metadata...) } - return buf.String() + return buf } // Original returns the original parsed version as-is, including any diff --git a/version_test.go b/version_test.go index f42a8c1..e6d5659 100644 --- a/version_test.go +++ b/version_test.go @@ -739,3 +739,24 @@ func BenchmarkVersionString(b *testing.B) { _ = v.String() } } + +func BenchmarkCompareVersionV1(b *testing.B) { + v, _ := NewVersion("3.4.5") + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + v.Compare(v) + } +} + +func BenchmarkVersionCompareV2(b *testing.B) { + v, _ := NewVersion("1.2.3") + o, _ := NewVersion("v1.2.3.4") + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + v.Compare(o) + } +}