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
+6 -1
View File
@@ -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
}