input: Remove unnecessary empty slices

This commit is contained in:
Hajime Hoshi 2019-03-24 23:44:36 +09:00
parent ccb23f1992
commit 92193b2362

View File

@ -26,15 +26,11 @@ func (i *Input) CursorPosition() (x, y int) {
return i.cursorX, i.cursorY return i.cursorX, i.cursorY
} }
var emptyIDs = []int{}
func (i *Input) GamepadIDs() []int { 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. return nil
// This is a performance optimization for browsers.
return emptyIDs
} }
r := []int{} r := []int{}
for id, g := range i.gamepads { for id, g := range i.gamepads {
@ -81,16 +77,12 @@ 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 { if len(in.touches) == 0 {
// Avoid creating a slice if possible. return nil
// This is a performance optimization for browsers.
return emptyTouches
} }
t := make([]*Touch, len(in.touches)) t := make([]*Touch, len(in.touches))