mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Reduce heap allocations in thread.go and input.go (#1085)
I've been doing some profiling of a very simple ebiten project, and noticed that thread.go was doing a bunch of unnecessary allocations to accomplish its work. This change seeks to reduce GC work. Input.go was also doing some unnecessary allocations. The thread.go change reduces the total number of allocations per frame from 1342 to 852 (~36% reduction). The input.go change reduces it further to 752 (~44% total reduction). Perf tests were done on windows.
This commit is contained in:
parent
5b7151595b
commit
a18cddb39f
8
input.go
8
input.go
@ -52,13 +52,13 @@ func IsKeyPressed(key Key) bool {
|
||||
var keys []driver.Key
|
||||
switch key {
|
||||
case KeyAlt:
|
||||
keys = append(keys, driver.KeyLeftAlt, driver.KeyRightAlt)
|
||||
keys = []driver.Key{driver.KeyLeftAlt, driver.KeyRightAlt}
|
||||
case KeyControl:
|
||||
keys = append(keys, driver.KeyLeftControl, driver.KeyRightControl)
|
||||
keys = []driver.Key{driver.KeyLeftControl, driver.KeyRightControl}
|
||||
case KeyShift:
|
||||
keys = append(keys, driver.KeyLeftShift, driver.KeyRightShift)
|
||||
keys = []driver.Key{driver.KeyLeftShift, driver.KeyRightShift}
|
||||
default:
|
||||
keys = append(keys, driver.Key(key))
|
||||
keys = []driver.Key{driver.Key(key)}
|
||||
}
|
||||
for _, k := range keys {
|
||||
if uiDriver().Input().IsKeyPressed(k) {
|
||||
|
@ -20,7 +20,8 @@ import (
|
||||
|
||||
// Thread represents an OS thread.
|
||||
type Thread struct {
|
||||
funcs chan func()
|
||||
funcs chan func() error
|
||||
results chan error
|
||||
}
|
||||
|
||||
// New creates a new thread.
|
||||
@ -28,7 +29,8 @@ type Thread struct {
|
||||
// It is assumed that the OS thread is fixed by runtime.LockOSThread when New is called.
|
||||
func New() *Thread {
|
||||
return &Thread{
|
||||
funcs: make(chan func()),
|
||||
funcs: make(chan func() error),
|
||||
results: make(chan error),
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +42,7 @@ loop:
|
||||
for {
|
||||
select {
|
||||
case f := <-t.funcs:
|
||||
f()
|
||||
t.results <- f()
|
||||
case <-context.Done():
|
||||
break loop
|
||||
}
|
||||
@ -51,12 +53,6 @@ loop:
|
||||
//
|
||||
// Do not call this from the same thread. This would block forever.
|
||||
func (t *Thread) Call(f func() error) error {
|
||||
ch := make(chan struct{})
|
||||
var err error
|
||||
t.funcs <- func() {
|
||||
err = f()
|
||||
close(ch)
|
||||
}
|
||||
<-ch
|
||||
return err
|
||||
t.funcs <- f
|
||||
return <-t.results
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user