pool_wasm.go raw

   1  //go:build js && wasm
   2  
   3  // Package bufpool provides buffer pools for reducing GC pressure in hot paths.
   4  // This is the WASM version which uses simple allocations since sync.Pool
   5  // behavior differs in WASM environments.
   6  package bufpool
   7  
   8  import (
   9  	"bytes"
  10  )
  11  
  12  const (
  13  	// SmallBufferSize for index keys (8-64 bytes typical)
  14  	SmallBufferSize = 64
  15  
  16  	// MediumBufferSize for event encoding (300-1000 bytes typical)
  17  	MediumBufferSize = 1024
  18  )
  19  
  20  // GetSmall returns a small buffer (64 bytes).
  21  // In WASM, we simply allocate new buffers as sync.Pool is less effective.
  22  func GetSmall() *bytes.Buffer {
  23  	return bytes.NewBuffer(make([]byte, 0, SmallBufferSize))
  24  }
  25  
  26  // PutSmall is a no-op in WASM; the buffer will be garbage collected.
  27  func PutSmall(buf *bytes.Buffer) {
  28  	// No-op in WASM
  29  }
  30  
  31  // GetMedium returns a medium buffer (1KB).
  32  func GetMedium() *bytes.Buffer {
  33  	return bytes.NewBuffer(make([]byte, 0, MediumBufferSize))
  34  }
  35  
  36  // PutMedium is a no-op in WASM; the buffer will be garbage collected.
  37  func PutMedium(buf *bytes.Buffer) {
  38  	// No-op in WASM
  39  }
  40  
  41  // CopyBytes copies the buffer contents to a new slice.
  42  func CopyBytes(buf *bytes.Buffer) []byte {
  43  	if buf == nil || buf.Len() == 0 {
  44  		return nil
  45  	}
  46  	result := make([]byte, buf.Len())
  47  	copy(result, buf.Bytes())
  48  	return result
  49  }
  50