-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
name.go
63 lines (54 loc) · 1.76 KB
/
name.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ecs
import (
"reflect"
"sync"
)
var componentIdMutex sync.Mutex
var registeredComponents = make(map[reflect.Type]componentId, maxComponentId)
var invalidComponentId componentId = 0
var componentRegistryCounter componentId = 1
func name(t any) componentId {
// Note: We have to lock here in case there are multiple worlds
// TODO!! - This probably causes some performance penalty
componentIdMutex.Lock()
defer componentIdMutex.Unlock()
typeof := reflect.TypeOf(t)
compId, ok := registeredComponents[typeof]
if !ok {
compId = componentRegistryCounter
registeredComponents[typeof] = compId
componentRegistryCounter++
}
return compId
}
// Possible solution: Runs faster than reflection (mostly useful for potentially removing/reducing ecs.C(...) overhead
// import (
// "sync"
// "unsafe"
// )
// type emptyInterface struct {
// typ unsafe.Pointer
// ptr unsafe.Pointer
// }
// var componentIdMutex sync.Mutex
// // var registeredComponents = make(map[reflect.Type]componentId, maxComponentId)
// var registeredComponents = make(map[uintptr]componentId, maxComponentId)
// var invalidComponentId componentId = 0
// var componentRegistryCounter componentId = 1
// func name(t any) componentId {
// // Note: We have to lock here in case there are multiple worlds
// // TODO!! - This probably causes some performance penalty
// componentIdMutex.Lock()
// defer componentIdMutex.Unlock()
// // typeof := reflect.TypeOf(t)
// // compId, ok := registeredComponents[typeof]
// iface := (*emptyInterface)(unsafe.Pointer(&t))
// typeptr := uintptr(iface.typ)
// compId, ok := registeredComponents[typeptr]
// if !ok {
// compId = componentRegistryCounter
// registeredComponents[typeptr] = compId
// componentRegistryCounter++
// }
// return compId
// }