mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 11:37:46 +00:00
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:
committed by
Ron Evans
parent
9f8715c143
commit
b8f5627c9f
+3
-1
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user