File tree Expand file tree Collapse file tree 1 file changed +26
-3
lines changed
Expand file tree Collapse file tree 1 file changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -57,13 +57,36 @@ func (c *Context) idScopeFromIDPart(idPart string, f func(id widgetID)) {
5757}
5858
5959func idPartFromString (str string ) string {
60- return fmt . Sprintf ( "string:%q" , str )
60+ return theStringIDCache . get ( str )
6161}
6262
6363func idPartFromInt (i int ) string {
64- return fmt . Sprintf ( "number:%d" , i )
64+ return theIntIDCache . get ( i )
6565}
6666
6767func 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+ )
You can’t perform that action at this time.
0 commit comments