Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase parsing and comparison performance #17

Merged
merged 9 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package semver

import (
"testing"
)

var list = []string{
"0.0.1-rc.0", // 0
"0.0.1-rc.0+build", // 1
"0.0.1-rc.1", // 2
"0.0.1", // 3
"0.0.1+build", // 4
"0.0.2-rc.1", // 5 - BREAKING CHANGE
"0.0.2-rc.1+build", // 6
"0.0.2", // 7
"0.0.2+build", // 8
"0.0.3-rc.1", // 9 - BREAKING CHANGE
"0.0.3-rc.2", // 10
"0.0.3", // 11
"0.1.0", // 12 - BREAKING CHANGE
"0.3.3-rc.0", // 13 - BREAKING CHANGE
"0.3.3-rc.1", // 14
"0.3.3", // 15
"0.3.3+build", // 16
"0.3.4-rc.1", // 17
"0.3.4", // 18
"0.4.0", // 19 - BREAKING CHANGE
"1.0.0-rc", // 20 - BREAKING CHANGE
"1.0.0", // 21
"1.0.0+build", // 22
"1.2.1-rc", // 23
"1.2.1", // 24
"1.2.1+build", // 25
"1.2.3-rc.2", // 26
"1.2.3-rc.2+build", // 27
"1.2.3", // 28
"1.2.3+build", // 29
"1.2.4", // 30
"1.3.0-rc.0+build", // 31
"1.3.0", // 32
"1.3.0+build", // 33
"1.3.1-rc.0", // 34
"1.3.1-rc.1", // 35
"1.3.1", // 36
"1.3.5", // 37
"2.0.0-rc", // 38 - BREAKING CHANGE
"2.0.0-rc+build", // 39
"2.0.0", // 40
"2.0.0+build", // 41
"2.1.0-rc", // 42
"2.1.0-rc+build", // 43
"2.1.0", // 44
"2.1.0+build", // 45
"2.1.3-rc", // 46
"2.1.3", // 47
"2.3.0", // 48
"2.3.1", // 49
"3.0.0", // 50 - BREAKING CHANGE
}

func BenchmarkVersionParser(b *testing.B) {
res := &Version{}
for i := 0; i < b.N; i++ {
for _, v := range list {
res.raw = v
res.bytes = []byte(v)
parse(res)
}
}

// $ go test -benchmem -run=^$ -bench ^BenchmarkVersionParser$ go.bug.st/relaxed-semver
// goos: linux
// goarch: amd64
// pkg: go.bug.st/relaxed-semver
// cpu: AMD Ryzen 5 3600 6-Core Processor

// Results for v0.11.0:
// BenchmarkVersionParser-12 188611 7715 ns/op 8557 B/op 51 allocs/op

// Results for v0.12.0: \o/
// BenchmarkVersionParser-12 479626 3719 ns/op 616 B/op 51 allocs/op
}

func BenchmarkVersionComparator(b *testing.B) {
b.StopTimer()
vList := []*Version{}
for _, in := range list {
vList = append(vList, MustParse(in))
}
l := len(vList)
b.StartTimer()

for i := 0; i < b.N; i++ {
// cross compare all versions
for x := 0; x < l; x++ {
for y := 0; y < l; y++ {
vList[x].CompareTo(vList[y])
}
}
}

// $ go test -benchmem -run=^$ -bench ^BenchmarkVersionComparator$ go.bug.st/relaxed-semver -v
// goos: linux
// goarch: amd64
// pkg: go.bug.st/relaxed-semver
// cpu: AMD Ryzen 5 3600 6-Core Processor

// Results for v0.11.0:
// BenchmarkVersionComparator-12 74793 17347 ns/op 0 B/op 0 allocs/op

// Results for v0.12.0: :-D
// BenchmarkVersionComparator-12 101772 11720 ns/op 0 B/op 0 allocs/op
}
71 changes: 21 additions & 50 deletions binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,22 @@ func marshalByteArray(b []byte) []byte {
return res
}

func marshalInt(i int) []byte {
res := make([]byte, 4)
binary.BigEndian.PutUint32(res, uint32(i))
return res
}

// MarshalBinary implements binary custom encoding
func (v *Version) MarshalBinary() ([]byte, error) {
// TODO could be preallocated without bytes.Buffer
res := new(bytes.Buffer)
_, _ = res.Write(marshalByteArray(v.major))
_, _ = res.Write(marshalByteArray(v.minor))
_, _ = res.Write(marshalByteArray(v.patch))
_, _ = res.Write(marshalInt(len(v.prerelases)))
for _, pre := range v.prerelases {
_, _ = res.Write(marshalByteArray(pre))
}
_, _ = res.Write(marshalInt(len(v.numericPrereleases)))
for _, npre := range v.numericPrereleases {
v := []byte{0}
if npre {
v[0] = 1
}
_, _ = res.Write(v)
}
_, _ = res.Write(marshalInt(len(v.builds)))
for _, build := range v.builds {
_, _ = res.Write(marshalByteArray(build))
}
intBuff := [4]byte{}
_, _ = res.Write(marshalByteArray([]byte(v.raw)))
binary.BigEndian.PutUint32(intBuff[:], uint32(v.major))
_, _ = res.Write(intBuff[:])
binary.BigEndian.PutUint32(intBuff[:], uint32(v.minor))
_, _ = res.Write(intBuff[:])
binary.BigEndian.PutUint32(intBuff[:], uint32(v.patch))
_, _ = res.Write(intBuff[:])
binary.BigEndian.PutUint32(intBuff[:], uint32(v.prerelease))
_, _ = res.Write(intBuff[:])
binary.BigEndian.PutUint32(intBuff[:], uint32(v.build))
_, _ = res.Write(intBuff[:])
return res.Bytes(), nil
}

Expand All @@ -63,31 +51,14 @@ func decodeInt(data []byte) (int, []byte) {
func (v *Version) UnmarshalBinary(data []byte) error {
var buff []byte

v.major, data = decodeArray(data)
v.minor, data = decodeArray(data)
v.patch, data = decodeArray(data)
n, data := decodeInt(data)
v.prerelases = nil
for i := 0; i < n; i++ {
buff, data = decodeArray(data)
v.prerelases = append(v.prerelases, buff)
}
v.numericPrereleases = nil
n, data = decodeInt(data)
for i := 0; i < n; i++ {
num := false
if data[0] == 1 {
num = true
}
v.numericPrereleases = append(v.numericPrereleases, num)
data = data[1:]
}
v.builds = nil
n, data = decodeInt(data)
for i := 0; i < n; i++ {
buff, data = decodeArray(data)
v.builds = append(v.builds, buff)
}
buff, data = decodeArray(data)
v.raw = string(buff)
v.bytes = []byte(v.raw)
v.major, data = decodeInt(data)
v.minor, data = decodeInt(data)
v.patch, data = decodeInt(data)
v.prerelease, data = decodeInt(data)
v.build, _ = decodeInt(data)
return nil
}

Expand Down
18 changes: 15 additions & 3 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestGOBEncoderVersion(t *testing.T) {

v, err := Parse(testVersion)
require.NoError(t, err)
dumpV := fmt.Sprintf("%s,%s,%s,%s,%v,%s", v.major, v.minor, v.patch, v.prerelases, v.numericPrereleases, v.builds)
require.Equal(t, "1,2,3,[aaa 4 5 6],[false true true true],[bbb 7 8 9]", dumpV)
dumpV := fmt.Sprintf("%v,%v,%v,%v,%v,%v", v.raw, v.major, v.minor, v.patch, v.prerelease, v.build)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9,1,3,5,15,25", dumpV)
require.Equal(t, testVersion, v.String())

dataV := new(bytes.Buffer)
Expand All @@ -31,10 +31,22 @@ func TestGOBEncoderVersion(t *testing.T) {
var u Version
err = gob.NewDecoder(dataV).Decode(&u)
require.NoError(t, err)
dumpU := fmt.Sprintf("%s,%s,%s,%s,%v,%s", u.major, u.minor, u.patch, u.prerelases, u.numericPrereleases, u.builds)
dumpU := fmt.Sprintf("%v,%v,%v,%v,%v,%v", v.raw, u.major, u.minor, u.patch, u.prerelease, u.build)

require.Equal(t, dumpV, dumpU)
require.Equal(t, testVersion, u.String())

{
dataV := new(bytes.Buffer)
dataU := new(bytes.Buffer)
require.NoError(t, gob.NewEncoder(dataV).Encode(MustParse("1.6.2")))
require.NoError(t, gob.NewEncoder(dataU).Encode(MustParse("1.6.3")))

var v, u *Version
require.NoError(t, gob.NewDecoder(dataV).Decode(&v))
require.NoError(t, gob.NewDecoder(dataU).Decode(&u))
require.True(t, u.GreaterThan(v))
}
}

func TestGOBEncoderRelaxedVersion(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ func (v *Version) UnmarshalJSON(data []byte) error {
return err
}

v.raw = parsed.raw
v.bytes = []byte(v.raw)
v.major = parsed.major
v.minor = parsed.minor
v.patch = parsed.patch
v.prerelases = parsed.prerelases
v.numericPrereleases = parsed.numericPrereleases
v.builds = parsed.builds
v.prerelease = parsed.prerelease
v.build = parsed.build
return nil
}

Expand Down
12 changes: 7 additions & 5 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ func TestJSONParseVersion(t *testing.T) {
var u Version
err = json.Unmarshal(data, &u)
require.NoError(t, err)
dump := fmt.Sprintf("%s,%s,%s,%s,%v,%s",
u.major, u.minor, u.patch,
u.prerelases, u.numericPrereleases,
u.builds)
require.Equal(t, "1,2,3,[aaa 4 5 6],[false true true true],[bbb 7 8 9]", dump)
dump := fmt.Sprintf("%v,%v,%v,%v,%v,%v",
u.raw, u.major, u.minor, u.patch, u.prerelease, u.build)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9,1,3,5,15,25", dump)
require.Equal(t, testVersion, u.String())

err = json.Unmarshal([]byte(`"invalid"`), &u)
require.Error(t, err)

err = json.Unmarshal([]byte(`123`), &u)
require.Error(t, err)

require.NoError(t, json.Unmarshal([]byte(`"1.6.2"`), &v))
require.NoError(t, json.Unmarshal([]byte(`"1.6.3"`), &u))
require.True(t, u.GreaterThan(v))
}

func TestJSONParseRelaxedVersion(t *testing.T) {
Expand Down
Loading
Loading