Skip to content

Commit

Permalink
Fix allocations in bytes_len_cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
HeadHunter483 committed Sep 6, 2024
1 parent 1237e07 commit 864b863
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pipeline/doif/do_if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,10 @@ const userInfoRawJSON = `
{
"name": "jack",
"age": 120,
"hobbies": ["football", "diving"]
"hobbies": ["football", "diving"],
"obj": {
"a": "b"
}
}`

func dryJSON(rawJSON string) string {
Expand Down
41 changes: 40 additions & 1 deletion pipeline/doif/len_cmp_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,45 @@ func (n *lenCmpOpNode) Type() NodeType {
return NodeLengthCmpOp
}

func getNodeFieldsBytesSize(node *insaneJSON.Node) int {
size := 0
fields := node.AsFields()
for _, elemNode := range fields {
// field node is a field name and it is always a string enclosed with double quotes
size += len(elemNode.AsString()) + 2 + 1 // quotes enclosing field name and colon between key and value

elemNodeVal := elemNode.AsFieldValue()
size += getNodeBytesSize(elemNodeVal)
}
size += len(fields) - 1 // commas between object fields
return size
}

func getNodeBytesSize(node *insaneJSON.Node) int {
if node == nil {
return 0
}
size := 0
switch {
case node.IsArray():
nodeArr := node.AsArray()
for _, elemNode := range nodeArr {
size += getNodeBytesSize(elemNode)
}
size += len(nodeArr) - 1 + 2 // commas between elements and square brackets enclosing array
case node.IsObject():
size += getNodeFieldsBytesSize(node) + 2 // curly brackets enclosing object
default:
// Note: node.AsString() unescapes all escaped symbols and can result in less length than there actually is.
// This can be fixed by implementing counting node bytes length directly in insaneJSON.
size += len(node.AsString())
if node.IsString() {
size += 2 // quotes enclosing string
}
}
return size
}

func (n *lenCmpOpNode) Check(eventRoot *insaneJSON.Root) bool {
value := 0

Expand All @@ -140,7 +179,7 @@ func (n *lenCmpOpNode) Check(eventRoot *insaneJSON.Root) bool {
}

if node.IsObject() || node.IsArray() {
value = len(node.EncodeToByte())
value = getNodeBytesSize(node)
} else {
value = len(node.AsString())
}
Expand Down

0 comments on commit 864b863

Please sign in to comment.