Skip to content

Commit c1794ee

Browse files
committed
refactor: optimize object and array handling by preallocating storage
1 parent 3672b96 commit c1794ee

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ docs/
1212
*.jsonte
1313
/.idea
1414
/gen
15-
data.json
15+
data.json
16+
*.out

jsonte/json_processor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func (v *TemplateVisitor) visit(obj types.JsonType, path string) (types.JsonType
547547
}
548548

549549
func (v *TemplateVisitor) visitObject(obj *types.JsonObject, path string) (types.JsonType, error) {
550-
var result = types.NewJsonObject()
550+
var result = types.NewJsonObjectWithCapacity(obj.Size())
551551
for _, key := range obj.Keys() {
552552
err := checkDeadline(v.deadline)
553553
if err != nil {
@@ -687,7 +687,7 @@ func PutValue(result *types.JsonObject, key string, r types.JsonType, path strin
687687
}
688688

689689
func (v *TemplateVisitor) visitArray(arr *types.JsonArray, path string) (types.JsonType, error) {
690-
var result = make([]types.JsonType, 0)
690+
var result = make([]types.JsonType, 0, len(arr.Value))
691691
for i, value := range arr.Value {
692692
err := checkDeadline(v.deadline)
693693
if err != nil {
@@ -705,7 +705,7 @@ func (v *TemplateVisitor) visitArray(arr *types.JsonArray, path string) (types.J
705705
// Special visitor for cases when the array element is an object, that generates multiple values
706706
func (v *TemplateVisitor) visitArrayElement(array []types.JsonType, element types.JsonType, path string) ([]types.JsonType, error) {
707707
if obj, ok := element.(*types.JsonObject); ok {
708-
filteredKeys := make([]string, 0)
708+
filteredKeys := make([]string, 0, obj.Size())
709709
for _, key := range obj.Keys() {
710710
if key != "$assert" && key != "$comment" {
711711
filteredKeys = append(filteredKeys, key)

jsonte/types/object.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ func NewJsonObject() *JsonObject {
398398
return &JsonObject{&navigableMap, nil, nil, nil, nil}
399399
}
400400

401+
// NewJsonObjectWithCapacity creates a JsonObject preallocating internal storage.
402+
func NewJsonObjectWithCapacity(capacity int) *JsonObject {
403+
navigableMap := utils.NewNavigableMapWithCapacity[string, JsonType](capacity)
404+
return &JsonObject{&navigableMap, nil, nil, nil, nil}
405+
}
406+
401407
func IsReservedKey(k string) bool {
402408
return strings.EqualFold(k, "$comment") || strings.EqualFold(k, "$assert")
403409
}

jsonte/utils/navigable_map.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ type NavigableMap[K comparable, V any] struct {
1111

1212
// NewNavigableMap creates a new NavigableMap.
1313
func NewNavigableMap[K comparable, V any]() NavigableMap[K, V] {
14+
return NewNavigableMapWithCapacity[K, V](0)
15+
}
16+
17+
// NewNavigableMapWithCapacity creates a new NavigableMap with preallocated storage.
18+
func NewNavigableMapWithCapacity[K comparable, V any](capacity int) NavigableMap[K, V] {
19+
if capacity < 0 {
20+
capacity = 0
21+
}
1422
return NavigableMap[K, V]{
15-
data: map[K]V{},
16-
keys: []K{},
17-
index: map[K]int{},
23+
data: make(map[K]V, capacity),
24+
keys: make([]K, 0, capacity),
25+
index: make(map[K]int, capacity),
1826
}
1927
}
2028

0 commit comments

Comments
 (0)