A recent performance issue showed how middleware changes in Go can negate zero-copy optimizations, notably when using io.Copy with os.File. The article details how Go implements zero-copy with methods like sendfile, and the consequences of adding wrappers that disrupt this functionality.
In a server environment with specific configurations, a one-line change in middleware led to significant performance degradation. Instead of directly using an os.File with io.Copy, adding a logging reader caused the system to stop utilizing zero-copy optimizations, doubling CPU usage and halving throughput.
The sendfile(2) system call in Linux allows for direct transfers from disk to a socket buffer, bypassing user space to improve performance. This avoids the overhead of multiple copies required by traditional read/write operations, making it crucial for high-performance file serving.
Go abstracts the zero-copy mechanism with methods such as ReadFrom on *net.TCPConn, which strategically utilizes sendfile when working with os.File. However, if the input is wrapped in another reader type, such as io.LimitedReader, this optimization may be bypassed, leading to inefficiencies.
For developers utilizing Go for file serving, understanding the implications of changes to middleware is crucial. Maintaining direct use of os.File with io.Copy ensures that performance is optimized, leveraging the powerful features provided by the underlying OS.
β¨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors β check the original sources. How BrevFeed works β
A recent performance issue showed how middleware changes in Go can negate zero-copy optimizations, notably when using io.Copy with os.File. The article details how Go implements zero-copy with methods like sendfile, and the consequences of adding wrappers that disrupt this functionality.