Skip to content

Commit df0dd23

Browse files
author
boekkooi-terramate
authored
chore: use a single buffer for field checks (#16)
Instead of creating a new string every for each possible version of a field we now (re-)use a single byte array to do the same. Benchmark comparison is: ``` goos: linux goarch: amd64 pkg: github.com/terramate-io/tfjson/v2/sanitize cpu: 13th Gen Intel(R) Core(TM) i7-13620H │ v2.txt │ v2_pr.txt │ │ sec/op │ sec/op vs base │ LargeChangeset-16 24.55µ ± 6% 13.01µ ± 2% -46.99% (p=0.000 n=10) │ v2.txt │ v2_pr.txt │ │ B/op │ B/op vs base │ LargeChangeset-16 7672.0 ± 0% 784.0 ± 0% -89.78% (p=0.000 n=10) │ v2.txt │ v2_pr.txt │ │ allocs/op │ allocs/op vs base │ LargeChangeset-16 368.00 ± 0% 42.00 ± 0% -88.59% (p=0.000 n=10) ``` Before: ``` goos: linux goarch: amd64 pkg: github.com/terramate-io/tfjson/v2/sanitize cpu: 13th Gen Intel(R) Core(TM) i7-13620H BenchmarkLargeChangeset BenchmarkLargeChangeset-16 46780 22959 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 45748 23622 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 51043 24620 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 48404 23003 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 44822 25041 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 46743 24831 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 51252 25010 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 48674 23974 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 45248 24541 ns/op 7672 B/op 368 allocs/op BenchmarkLargeChangeset-16 50532 24560 ns/op 7672 B/op 368 allocs/op PASS ok github.com/terramate-io/tfjson/v2/sanitize 14.217s ``` With PR: ``` goos: linux goarch: amd64 pkg: github.com/terramate-io/tfjson/v2/sanitize cpu: 13th Gen Intel(R) Core(TM) i7-13620H BenchmarkLargeChangeset BenchmarkLargeChangeset-16 88532 12938 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 88912 13293 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91579 13061 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91285 12980 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91048 13062 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91712 12895 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91569 12948 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91477 13012 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91344 13227 ns/op 784 B/op 42 allocs/op BenchmarkLargeChangeset-16 91731 13015 ns/op 784 B/op 42 allocs/op PASS ok github.com/terramate-io/tfjson/v2/sanitize 13.270s ```
2 parents 9a5484f + 887768c commit df0dd23

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

sanitize/sanitize_change.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,28 @@ func sanitizeChangeValue(old, sensitive, replaceWith interface{}) interface{} {
6060
return old
6161
}
6262

63-
var sanitizeAuxiliaryPostfix = []string{
64-
"_base64",
65-
"_base64sha1",
66-
"_base64sha256",
67-
"_base64sha512",
68-
"_md5",
69-
"_sha1",
70-
"_sha256",
71-
"_sha512",
63+
var sanitizeAuxiliaryPostfix = [][]byte{
64+
[]byte("_base64"),
65+
[]byte("_base64sha1"),
66+
[]byte("_base64sha256"),
67+
[]byte("_base64sha512"),
68+
[]byte("_md5"),
69+
[]byte("_sha1"),
70+
[]byte("_sha256"),
71+
[]byte("_sha512"),
7272
}
7373

7474
func sanitizeAuxiliary(field string, values map[string]interface{}, sensitive, replaceWith interface{}) {
7575
if val, ok := sensitive.(bool); !ok || !val {
7676
return
7777
}
7878

79-
var auxField string
79+
auxField := []byte(field)
80+
auxFieldLen := len(auxField)
8081
for _, aux := range sanitizeAuxiliaryPostfix {
81-
auxField = field + aux
82-
if val, ok := values[auxField]; ok && val != nil {
83-
values[auxField] = replaceWith
82+
auxField = append(auxField[:auxFieldLen], aux...)
83+
if val, ok := values[string(auxField)]; ok && val != nil {
84+
values[string(auxField)] = replaceWith
8485
}
8586
}
8687
}

0 commit comments

Comments
 (0)