sort _realNamePairs from shortest to longest obfuscated name

This lets us only check names up to the remaining input string length.

                  │     old     │                new                 │
                  │   sec/op    │   sec/op     vs base               │
    AbiRealName-8   196.7µ ± 1%   172.3µ ± 1%  -12.41% (p=0.001 n=7)

                  │     old      │                 new                 │
                  │     B/s      │     B/s       vs base               │
    AbiRealName-8   1.774Mi ± 1%   2.022Mi ± 0%  +13.98% (p=0.001 n=7)

                  │     old      │                new                 │
                  │     B/op     │     B/op      vs base              │
    AbiRealName-8   5.359Ki ± 0%   5.336Ki ± 0%  -0.44% (p=0.001 n=7)

                  │    old     │               new                │
                  │ allocs/op  │ allocs/op   vs base              │
    AbiRealName-8   19.00 ± 0%   18.00 ± 0%  -5.26% (p=0.001 n=7)
pull/892/head
Daniel Martí 4 months ago
parent c012f08c66
commit c83c5ce3e6

@ -44,7 +44,9 @@ func _realName(name string) string {
real := pair[1] real := pair[1]
keyLen := len(obfName) keyLen := len(obfName)
if remLen < keyLen { if remLen < keyLen {
continue // Since the pairs are sorted from shortest to longest name,
// we know that the rest of the pairs are at least just as long.
break
} }
if name[i:i+keyLen] == obfName { if name[i:i+keyLen] == obfName {
name = name[:i] + real + name[i+keyLen:] name = name[:i] + real + name[i+keyLen:]
@ -60,4 +62,6 @@ func _realName(name string) string {
return name return name
} }
// Each pair is the obfuscated and then the real name.
// The slice is sorted from shortest to longest obfuscated name.
var _realNamePairs = [][2]string{} var _realNamePairs = [][2]string{}

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"cmp"
_ "embed" _ "embed"
"fmt" "fmt"
"maps" "maps"
@ -62,7 +63,12 @@ func reflectMainPostPatch(file []byte, lpkg *listedPackage, pkg pkgCache) []byte
nameMap := fmt.Sprintf("%s = [][2]string{", obfVarName) nameMap := fmt.Sprintf("%s = [][2]string{", obfVarName)
var b strings.Builder var b strings.Builder
keys := slices.Sorted(maps.Keys(pkg.ReflectObjectNames)) keys := slices.SortedFunc(maps.Keys(pkg.ReflectObjectNames), func(a, b string) int {
if c := cmp.Compare(len(a), len(b)); c != 0 {
return c
}
return cmp.Compare(a, b)
})
for _, obf := range keys { for _, obf := range keys {
b.WriteString(fmt.Sprintf("{%q, %q},", obf, pkg.ReflectObjectNames[obf])) b.WriteString(fmt.Sprintf("{%q, %q},", obf, pkg.ReflectObjectNames[obf]))
} }

Loading…
Cancel
Save