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

Optimise levelJSONValue #265

Merged
merged 1 commit into from
Jul 2, 2021
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
4 changes: 2 additions & 2 deletions eventcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,11 @@ func (v *levelJSONValue) UnmarshalJSON(data []byte) error {
var err error

// First try to unmarshal as an int64.
if err = json.Unmarshal(data, &int64Value); err != nil {
if int64Value, err = strconv.ParseInt(string(data), 10, 64); err != nil {
// If unmarshalling as an int64 fails try as a string.
if err = json.Unmarshal(data, &stringValue); err != nil {
// If unmarshalling as a string fails try as a float.
if err = json.Unmarshal(data, &floatValue); err != nil {
if floatValue, err = strconv.ParseFloat(string(data), 64); err != nil {
return err
}
int64Value = int64(floatValue)
Expand Down
21 changes: 21 additions & 0 deletions eventcontent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ import (
"testing"
)

func BenchmarkLevelJSONValueInt(b *testing.B) {
for n := 0; n < b.N; n++ {
var value []levelJSONValue
_ = json.Unmarshal([]byte(`[1, 2, 3]`), &value)
}
}

func BenchmarkLevelJSONValueFloat(b *testing.B) {
for n := 0; n < b.N; n++ {
var value []levelJSONValue
_ = json.Unmarshal([]byte(`[1.1, 1.2, 1.3]`), &value)
}
}

func BenchmarkLevelJSONValueString(b *testing.B) {
for n := 0; n < b.N; n++ {
var value []levelJSONValue
_ = json.Unmarshal([]byte(`["1", "2", "3"]`), &value)
}
}

func TestLevelJSONValueValid(t *testing.T) {
var values []levelJSONValue
input := `[0,"1",2.0]`
Expand Down