Skip to content

optimize performance: reduce allocations, buffered I/O, zero-copy reads - #127

Merged
carmark merged 1 commit into
masterfrom
perf/reduce-alloc-and-copies
Mar 14, 2026
Merged

optimize performance: reduce allocations, buffered I/O, zero-copy reads#127
carmark merged 1 commit into
masterfrom
perf/reduce-alloc-and-copies

Conversation

@carmark

@carmark carmark commented Mar 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Performance optimizations targeting the hot I/O paths.

Read path - eliminate double allocation (HIGH impact)

  • Removed redundant make([]byte, tl) before bs.Read() in bsPerformCommand
  • Replaced inefficient append loop for zero-fill with direct in-place zeroing
  • Result: 1 fewer allocation per read, no slice growth overhead

parseHeader - use command pool (MEDIUM impact)

  • Changed &ISCSICommand{} to getCommand() from sync.Pool
  • Every SCSI command was allocating a new ISCSICommand on heap
  • Result: near-zero allocation for command parsing under steady load

Unmap - shared zero buffer (MEDIUM impact)

  • Replaced per-descriptor make([]byte, desc.TL) with shared 1MB zero buffer
  • Large unmaps (e.g., 1GB) previously caused 262K allocations, now zero
  • Result: O(1) memory usage regardless of unmap size

Network I/O - buffered writer (MEDIUM impact)

  • Added 256KB bufio.Writer to iSCSI connections
  • Batches small PDU writes into fewer syscalls
  • Flush after txHandler completes to ensure data delivery
  • Result: fewer write syscalls, better TCP segment utilization

Test plan

  • go build ./... passes
  • go vet ./... passes
  • go test ./... all tests pass
  • Local daemon startup + port binding verified

… reads

- Read path: eliminate redundant allocation in bsPerformCommand - remove
  the pre-allocation before bs.Read() and the append loop for zero-fill,
  use direct copy and in-place zero-fill instead
- parseHeader: use command pool (getCommand) instead of direct allocation,
  reducing GC pressure on the hot path
- Unmap: use a shared 1MB zero buffer instead of allocating per-descriptor,
  dramatically reducing allocations for large unmap operations
- Network I/O: add 256KB bufio.Writer to iSCSI connections, batching
  small PDU writes into fewer syscalls. Flush after txHandler completes

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@carmark
carmark merged commit 87c25cf into master Mar 14, 2026
1 check passed
@carmark
carmark deleted the perf/reduce-alloc-and-copies branch March 14, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant