Description
Running os/exec's new BenchmarkExecEcho on my machine, 8% of allocations are from internal/poll/fd_poll_runtime.go:45
: return syscall.Errno(errno)
. This allocates when we convert from syscall.Errno
to an error
. (For those who are curious, the errno is 22, EINVAL, indicating that the file is not pollable. We end up noting only that the error is non-nil, at internal/poll/fd_unix.go:64
, and then dropping it, at os/file_unix.go:157
.)
There aren't many errnos. We could avoid this allocation, and others, by adding an array of all expected Errnos as errors in package syscall, and adding an accessor function to return them. Something like:
var errnoErrors = [...]error{nil, Errno(1), Errno(2), ... }
func ErrnoError(e int) error {
if e >= 0 && e < len(errnoErrors) {
return errnoErrors[e]
}
return Errno(e)
}
Note that errnoErrors would be generated by a script, and would be treated as static data by the compiler and linker, so the impact would be only on binary size (not compilation time or process startup).
Package syscall is frozen and sensitive, so I wanted to ask before doing the work.