Skip to content

Commit baa33a1

Browse files
committed
debugui: cache ID strings
Updates #41
1 parent 2feb6b3 commit baa33a1

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

id.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,36 @@ func (c *Context) idScopeFromIDPart(idPart string, f func(id widgetID)) {
5757
}
5858

5959
func idPartFromString(str string) string {
60-
return fmt.Sprintf("string:%q", str)
60+
return theStringIDCache.get(str)
6161
}
6262

6363
func idPartFromInt(i int) string {
64-
return fmt.Sprintf("number:%d", i)
64+
return theIntIDCache.get(i)
6565
}
6666

6767
func idPartFromCaller(callerPC uintptr) string {
68-
return fmt.Sprintf("caller:%d", callerPC)
68+
return theCallerIDCache.get(callerPC)
6969
}
70+
71+
type idCache[T comparable] struct {
72+
values map[T]string
73+
prefix string
74+
}
75+
76+
func (c *idCache[T]) get(id T) string {
77+
if idStr, ok := c.values[id]; ok {
78+
return idStr
79+
}
80+
if c.values == nil {
81+
c.values = map[T]string{}
82+
}
83+
idStr := fmt.Sprintf("%s:%v", c.prefix, id)
84+
c.values[id] = idStr
85+
return idStr
86+
}
87+
88+
var (
89+
theStringIDCache = idCache[string]{prefix: "string"}
90+
theIntIDCache = idCache[int]{prefix: "number"}
91+
theCallerIDCache = idCache[uintptr]{prefix: "caller"}
92+
)

0 commit comments

Comments
 (0)