ui: Avoid creating empty slice at Touches

This commit is contained in:
Hajime Hoshi 2018-02-12 20:23:24 +09:00
parent 7d9b901ab3
commit cc423c276d

View File

@ -37,6 +37,8 @@ func (i *Input) GamepadIDs() []int {
i.m.RLock() i.m.RLock()
defer i.m.RUnlock() defer i.m.RUnlock()
if len(i.gamepads) == 0 { if len(i.gamepads) == 0 {
// Avoid creating a slice if possible.
// This is a performance optimization for browsers.
return emptyIDs return emptyIDs
} }
r := []int{} r := []int{}
@ -84,9 +86,18 @@ func (i *Input) IsGamepadButtonPressed(id int, button GamepadButton) bool {
return i.gamepads[id].buttonPressed[button] return i.gamepads[id].buttonPressed[button]
} }
var emptyTouches = []Touch{}
func (in *Input) Touches() []Touch { func (in *Input) Touches() []Touch {
in.m.RLock() in.m.RLock()
defer in.m.RUnlock() defer in.m.RUnlock()
if len(in.touches) == 0 {
// Avoid creating a slice if possible.
// This is a performance optimization for browsers.
return emptyTouches
}
t := make([]Touch, len(in.touches)) t := make([]Touch, len(in.touches))
for i := 0; i < len(t); i++ { for i := 0; i < len(t); i++ {
t[i] = &in.touches[i] t[i] = &in.touches[i]