Skip to content

Commit 5ab5780

Browse files
authored
get rid of unneeded memory allocations when using string builders instead of byte buffers (#487)
Co-authored-by: Ferdinand Neman <ferdinand@hyperjump.tech> As suggested by @maxwey and merged into my fork you can get rid of unneeded memory allocations when using string builders instead of byte buffers
1 parent b41013b commit 5ab5780

14 files changed

+31
-31
lines changed

ast/ArgumentList.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2019
"reflect"
20+
"strings"
2121

2222
"github.com/hyperjumptech/grule-rule-engine/pkg"
2323
)
@@ -103,7 +103,7 @@ func (e *ArgumentList) GetGrlText() string {
103103

104104
// GetSnapshot will create a structure signature or AST graph
105105
func (e *ArgumentList) GetSnapshot() string {
106-
var buff bytes.Buffer
106+
var buff strings.Builder
107107
buff.WriteString(ARGUMENTLIST)
108108
buff.WriteString("(")
109109
for i, v := range e.Arguments {

ast/ArrayMapSelector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2120
"github.com/hyperjumptech/grule-rule-engine/pkg"
2221
"reflect"
22+
"strings"
2323
)
2424

2525
// NewArrayMapSelector create a new array selector graph
@@ -106,7 +106,7 @@ func (e *ArrayMapSelector) GetGrlText() string {
106106

107107
// GetSnapshot will create a structure signature or AST graph
108108
func (e *ArrayMapSelector) GetSnapshot() string {
109-
var buff bytes.Buffer
109+
var buff strings.Builder
110110
buff.WriteString(MAPARRAYSELECTOR)
111111
buff.WriteString("(")
112112
if e.Expression != nil {

ast/Assignment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"errors"
2019
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2120
"github.com/hyperjumptech/grule-rule-engine/pkg"
21+
"strings"
2222
)
2323

2424
// NewAssignment will create new instance of Assignment AST Node
@@ -141,7 +141,7 @@ func (e *Assignment) GetGrlText() string {
141141

142142
// GetSnapshot will create a structure signature or AST graph
143143
func (e *Assignment) GetSnapshot() string {
144-
var buff bytes.Buffer
144+
var buff strings.Builder
145145
buff.WriteString(ASSIGMENT)
146146
buff.WriteString("(")
147147
buff.WriteString(e.Variable.GetSnapshot())

ast/Constant.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2222
"math"
2323
"reflect"
24+
"strings"
2425

2526
"github.com/hyperjumptech/grule-rule-engine/pkg"
2627
)
@@ -116,7 +117,7 @@ func (e *Constant) GetGrlText() string {
116117

117118
// GetSnapshot will create a structure signature or AST graph
118119
func (e *Constant) GetSnapshot() string {
119-
var buff bytes.Buffer
120+
var buff strings.Builder
120121
buff.WriteString(CONSTANT)
121122
buff.WriteString("(")
122123
buff.WriteString(e.Value.Kind().String())

ast/Expression.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"errors"
2019
"fmt"
2120
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2221
"reflect"
22+
"strings"
2323

2424
"github.com/hyperjumptech/grule-rule-engine/pkg"
2525
)
@@ -208,7 +208,7 @@ func (e *Expression) GetGrlText() string {
208208

209209
// GetSnapshot will create a structure signature or AST graph
210210
func (e *Expression) GetSnapshot() string {
211-
var buff bytes.Buffer
211+
var buff strings.Builder
212212
buff.WriteString(EXPRESSION)
213213
buff.WriteString("(")
214214
if e.SingleExpression != nil {

ast/ExpressionAtom.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"errors"
2019
"fmt"
2120
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2221
"github.com/hyperjumptech/grule-rule-engine/model"
2322
"reflect"
23+
"strings"
2424

2525
"github.com/hyperjumptech/grule-rule-engine/pkg"
2626
)
@@ -224,7 +224,7 @@ func (e *ExpressionAtom) GetGrlText() string {
224224

225225
// GetSnapshot will create a structure signature or AST graph
226226
func (e *ExpressionAtom) GetSnapshot() string {
227-
var buff bytes.Buffer
227+
var buff strings.Builder
228228
buff.WriteString(EXPRESSIONATOM)
229229
buff.WriteString("(")
230230
if e.Variable != nil {

ast/FunctionCall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2120
"github.com/hyperjumptech/grule-rule-engine/pkg"
2221
"reflect"
22+
"strings"
2323
)
2424

2525
// NewFunctionCall creates new instance of FunctionCall
@@ -99,7 +99,7 @@ func (e *FunctionCall) GetGrlText() string {
9999

100100
// GetSnapshot will create a structure signature or AST graph
101101
func (e *FunctionCall) GetSnapshot() string {
102-
var buff bytes.Buffer
102+
var buff strings.Builder
103103
buff.WriteString(FUNCTIONCALL)
104104
buff.WriteString(fmt.Sprintf("(n:%s", e.FunctionName))
105105
if e.ArgumentList != nil {

ast/KnowledgeBase.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"io"
2120
"sort"
@@ -98,12 +97,12 @@ func (lib *KnowledgeLibrary) LoadKnowledgeBaseFromReader(reader io.Reader, overw
9897
return nil, err
9998
}
10099
if overwrite {
101-
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)] = knowledgeBase
100+
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase
102101

103102
return knowledgeBase, nil
104103
}
105-
if _, ok := lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)]; !ok {
106-
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)] = knowledgeBase
104+
if _, ok := lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name, knowledgeBase.Version)]; !ok {
105+
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase
107106

108107
return knowledgeBase, nil
109108
}
@@ -194,7 +193,7 @@ func (e *KnowledgeBase) IsIdentical(that *KnowledgeBase) bool {
194193

195194
// GetSnapshot will create this knowledge base signature
196195
func (e *KnowledgeBase) GetSnapshot() string {
197-
var buffer bytes.Buffer
196+
var buffer strings.Builder
198197
buffer.WriteString(fmt.Sprintf("%s:%s[", e.Name, e.Version))
199198
keys := make([]string, 0)
200199
for i := range e.RuleEntries {
@@ -318,4 +317,4 @@ func (e *KnowledgeBase) Reset() {
318317
// GetKnowledgeBaseKey returns the key corresponding to the knowledgeBase in the KnowledgeLibrary
319318
func GetKnowledgeBaseKey(name, version string) string {
320319
return fmt.Sprintf("%s:%s", name, version)
321-
}
320+
}

ast/RuleEntry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"fmt"
2120
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2221
"reflect"
22+
"strings"
2323

2424
"github.com/hyperjumptech/grule-rule-engine/pkg"
2525
)
@@ -148,7 +148,7 @@ func (e *RuleEntry) GetGrlText() string {
148148

149149
// GetSnapshot will create a structure signature or AST graph
150150
func (e *RuleEntry) GetSnapshot() string {
151-
var buff bytes.Buffer
151+
var buff strings.Builder
152152
buff.WriteString(RULEENTRY)
153153
buff.WriteString("(")
154154
buff.WriteString(fmt.Sprintf("N:%s DEC:\"%s\" SAL:%d W:%s T:%s}", e.RuleName, e.RuleDescription, e.Salience, e.WhenScope.GetSnapshot(), e.ThenScope.GetSnapshot()))

ast/ThenExpression.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
package ast
1616

1717
import (
18-
"bytes"
1918
"github.com/hyperjumptech/grule-rule-engine/ast/unique"
2019
"github.com/hyperjumptech/grule-rule-engine/pkg"
20+
"strings"
2121
)
2222

2323
// NewThenExpression create new instance of ThenExpression
@@ -121,7 +121,7 @@ func (e *ThenExpression) GetGrlText() string {
121121

122122
// GetSnapshot will create a structure signature or AST graph
123123
func (e *ThenExpression) GetSnapshot() string {
124-
var buff bytes.Buffer
124+
var buff strings.Builder
125125
buff.WriteString(THENEXPRESSION)
126126
buff.WriteString("(")
127127
if e.Assignment != nil {

0 commit comments

Comments
 (0)