File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed
Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,12 @@ const (
1313 maxPDULength = 253 // Maximum Modbus PDU length
1414)
1515
16- // Buffer pool for Modbus message handling
16+ // Buffer pool for Modbus message handling - Store slice pointers to avoid allocations
1717var modbusBufferPool = sync.Pool {
1818 New : func () interface {} {
1919 // Pre-allocate maximum possible Modbus TCP frame size
20- return make ([]byte , maxTCPFrameLength )
20+ buf := make ([]byte , maxTCPFrameLength )
21+ return & buf // Return pointer to avoid allocations
2122 },
2223}
2324
@@ -28,9 +29,11 @@ func (m ModbusMessageReader) Name() string {
2829}
2930
3031func (m ModbusMessageReader ) ReadMessage (conn io.Reader ) ([]byte , error ) {
31- // Get buffer from pool
32- buffer := modbusBufferPool .Get ().([]byte )
33- defer modbusBufferPool .Put (buffer )
32+ // Get buffer from pool (as pointer to avoid allocations)
33+ bufPtr := modbusBufferPool .Get ().(* []byte )
34+ defer modbusBufferPool .Put (bufPtr ) // Return pointer to pool
35+
36+ buffer := * bufPtr // Dereference for easier use
3437
3538 // Read the MBAP header (first 6 bytes)
3639 if _ , err := io .ReadFull (conn , buffer [:mbapHeaderLength ]); err != nil {
You can’t perform that action at this time.
0 commit comments