Skip to content

Commit

Permalink
Pre-allocate array in Children() for mmap and add benchmark. (#133)
Browse files Browse the repository at this point in the history
* Pre-allocate array in Children() for mmap and add benchmark.

* Fixed an append issue noticed by Mihai

* use append instead of direct assignment, they are equivalent but the
append is cleaner

---------

Co-authored-by: Steve McDaniel <[email protected]>
Co-authored-by: Ashley Jeffs <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2023
1 parent d0749ab commit e5b2e65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (g *Container) Children() []*Container {
return children
}
if mmap, ok := g.Data().(map[string]interface{}); ok {
children := []*Container{}
children := make([]*Container, 0, len(mmap))
for _, obj := range mmap {
children = append(children, &Container{obj})
}
Expand Down
19 changes: 19 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,25 @@ func TestFlattenIncludeEmpty(t *testing.T) {
}
}

func BenchmarkChildren(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jsonObj, err := ParseJSON(bigSample)
if err != nil {
b.Errorf("Error parsing json: %v\n", err)
}

_ = jsonObj.Children()

FOSI := jsonObj.S("firstOutter", "secondInner")
_ = FOSI.Children()
SOSI := jsonObj.S("secondOutter", "secondInner")
_ = SOSI.Children()
SOTI := jsonObj.S("secondOutter", "thirdInner")
_ = SOTI.Children()
}
}

func BenchmarkWildcardSearch(b *testing.B) {
sample := []byte(`{"test":[{"value":10},{"value":20}]}`)

Expand Down

0 comments on commit e5b2e65

Please sign in to comment.