-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
33 lines (29 loc) · 842 Bytes
/
Copy pathcache.go
File metadata and controls
33 lines (29 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package databuilder
import (
"reflect"
"runtime"
"sync"
)
// Keys (reflect.Type identity, function PC) are stable for the lifetime of
// the process, so these caches never need eviction and are bounded by the
// number of distinct types and builder functions ever observed.
var (
structNameCache sync.Map // reflect.Type -> string
funcNameCache sync.Map // uintptr -> string
)
func cachedStructName(t reflect.Type) string {
if v, ok := structNameCache.Load(t); ok {
return v.(string)
}
name := t.PkgPath() + "." + t.Name()
actual, _ := structNameCache.LoadOrStore(t, name)
return actual.(string)
}
func resolveFuncName(pc uintptr) string {
if v, ok := funcNameCache.Load(pc); ok {
return v.(string)
}
name := runtime.FuncForPC(pc).Name()
actual, _ := funcNameCache.LoadOrStore(pc, name)
return actual.(string)
}