machine: move errors.New calls to globals

Calling errors.New in an error path causes a heap allocation at an
already unfortunate moment. It is more efficient to create these error
values in globals and return these constant globals. If these errors are
not used (because the related code was optimized out), the globals will
also be optimized out.
This commit is contained in:
Ayke van Laethem
2020-04-04 14:33:29 +02:00
committed by Ron Evans
parent 9f8715c143
commit b8f5627c9f
8 changed files with 70 additions and 55 deletions
+3 -1
View File
@@ -4,6 +4,8 @@ package machine
import "errors"
var errUARTBufferEmpty = errors.New("UART buffer empty")
type UARTConfig struct {
BaudRate uint32
TX Pin
@@ -60,7 +62,7 @@ func (uart UART) ReadByte() (byte, error) {
// check if RX buffer is empty
buf, ok := uart.Buffer.Get()
if !ok {
return 0, errors.New("Buffer empty")
return 0, errUARTBufferEmpty
}
return buf, nil
}