mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +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
+6
-1
@@ -9,6 +9,11 @@ import (
|
||||
|
||||
const deviceDescriptorSize = 18
|
||||
|
||||
var (
|
||||
errUSBCDCBufferEmpty = errors.New("USB-CDC buffer empty")
|
||||
errUSBCDCWriteByteTimeout = errors.New("USB-CDC write byte timeout")
|
||||
)
|
||||
|
||||
// DeviceDescriptor implements the USB standard device descriptor.
|
||||
//
|
||||
// Table 9-8. Standard Device Descriptor
|
||||
@@ -598,7 +603,7 @@ func (usbcdc USBCDC) ReadByte() (byte, error) {
|
||||
// check if RX buffer is empty
|
||||
buf, ok := usbcdc.Buffer.Get()
|
||||
if !ok {
|
||||
return 0, errors.New("Buffer empty")
|
||||
return 0, errUSBCDCBufferEmpty
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user