diff --git a/pkg/detection/bench_test.go b/pkg/detection/bench_test.go new file mode 100644 index 0000000..0d49d0d --- /dev/null +++ b/pkg/detection/bench_test.go @@ -0,0 +1,31 @@ +package detection + +import ( + "fmt" + "testing" + + "github.com/BlackVectorOps/semantic_firewall/v3/pkg/analysis/topology" +) + +func BenchmarkGenerateTopologyHash(b *testing.B) { + topo := &topology.FunctionTopology{ + ParamCount: 2, + ReturnCount: 1, + BlockCount: 10, + InstrCount: 50, + LoopCount: 2, + BranchCount: 3, + CallSignatures: make(map[string]int), + } + + // Populate with some call signatures + for i := 0; i < 100; i++ { + key := fmt.Sprintf("pkg.Func%d", i) + topo.CallSignatures[key] = i % 5 + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + GenerateTopologyHash(topo) + } +} diff --git a/pkg/detection/engine.go b/pkg/detection/engine.go index c8b2369..9d13564 100644 --- a/pkg/detection/engine.go +++ b/pkg/detection/engine.go @@ -203,15 +203,16 @@ func GenerateTopologyHash(topo *topology.FunctionTopology) string { builder.WriteString("BR") builder.WriteString(strconv.Itoa(topo.BranchCount)) - var calls []string + calls := make([]string, 0, len(topo.CallSignatures)) + buf := make([]byte, 0, 128) for call, count := range topo.CallSignatures { - var cb strings.Builder - cb.WriteString(strconv.Itoa(len(call))) - cb.WriteString(":") - cb.WriteString(call) - cb.WriteString(":") - cb.WriteString(strconv.Itoa(count)) - calls = append(calls, cb.String()) + buf = buf[:0] + buf = strconv.AppendInt(buf, int64(len(call)), 10) + buf = append(buf, ':') + buf = append(buf, call...) + buf = append(buf, ':') + buf = strconv.AppendInt(buf, int64(count), 10) + calls = append(calls, string(buf)) } sort.Strings(calls) builder.WriteString(strings.Join(calls, ";"))