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

perf: various small improvements #7357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions v1/ast/interning.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ func HasInternedIntNumberTerm(i int) bool {
return i >= -1 && i < len(intNumberTerms)
}

func InternedStringTerm(s string) *Term {
if term, ok := internedStringTerms[s]; ok {
return term
}

return StringTerm(s)
}

var internedStringTerms = map[string]*Term{
"": InternedEmptyString,
"0": StringTerm("0"),
"1": StringTerm("1"),
"2": StringTerm("2"),
"3": StringTerm("3"),
"4": StringTerm("4"),
"5": StringTerm("5"),
"6": StringTerm("6"),
"7": StringTerm("7"),
"8": StringTerm("8"),
"9": StringTerm("9"),
"10": StringTerm("10"),
}

var stringToIntNumberTermMap = map[string]*Term{
"-1": minusOneTerm,
"0": intNumberTerms[0],
Expand Down
4 changes: 4 additions & 0 deletions v1/ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,10 @@ func (p *Parser) parseNumber() *Term {

func (p *Parser) parseString() *Term {
if p.s.lit[0] == '"' {
if p.s.lit == "\"\"" {
return NewTerm(InternedEmptyString.Value).SetLocation(p.s.Loc())
}

var s string
err := json.Unmarshal([]byte(p.s.lit), &s)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions v1/topdown/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func builtinObjectUnion(_ BuiltinContext, operands []*ast.Term, iter func(*ast.T
return err
}

if objA.Compare(objB) == 0 {
return iter(operands[0])
}

r := mergeWithOverwrite(objA, objB)

return iter(ast.NewTerm(r))
Expand Down
3 changes: 3 additions & 0 deletions v1/topdown/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func builtinRegexReplace(bctx BuiltinContext, operands []*ast.Term, iter func(*a
}

res := re.ReplaceAllString(string(base), string(value))
if res == string(base) {
return iter(operands[0])
}

return iter(ast.StringTerm(res))
}
Expand Down
10 changes: 9 additions & 1 deletion v1/topdown/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,21 @@ func builtinSubstring(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
return iter(ast.StringTerm(sbase[startIndex:]))
}

if startIndex == 0 && length >= len(sbase) {
return iter(operands[0])
}

upto := startIndex + length
if len(sbase) < upto {
upto = len(sbase)
}
return iter(ast.StringTerm(sbase[startIndex:upto]))
}

if startIndex == 0 && length >= utf8.RuneCountInString(sbase) {
return iter(operands[0])
}

runes := []rune(base)

if startIndex >= len(runes) {
Expand Down Expand Up @@ -638,7 +646,7 @@ func builtinSprintf(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
if s == "%d" && astArr.Len() == 1 {
if n, ok := astArr.Elem(0).Value.(ast.Number); ok {
if i, ok := n.Int(); ok {
return iter(ast.StringTerm(strconv.Itoa(i)))
return iter(ast.InternedStringTerm(strconv.Itoa(i)))
}
}
}
Expand Down
Loading