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 727e0cb
Show file tree
Hide file tree
Showing 2 changed files with 50 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
47 changes: 46 additions & 1 deletion pipeline/doif/len_cmp_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,51 @@ 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
// Note: there is one corner case for field names with escaping `\"`.
// In that case output bytes count will be less than actual because insaneJSON always escapes field names when using `AsFields`.
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:
if node.IsString() {
if node.TypeStr() == "hellBitString" {
// Note: in case of unescaped string computed bytes length can diverse from the actual
size += len(node.AsString()) + 2 // add quotes to unescaped string
} else {
size += len(node.AsEscapedString())
}
} else {
size += len(node.AsString())
}
}
return size
}

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

Expand All @@ -140,7 +185,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 727e0cb

Please sign in to comment.