Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save mindscratch/a1a7e3cca72aff209f1d to your computer and use it in GitHub Desktop.

Select an option

Save mindscratch/a1a7e3cca72aff209f1d to your computer and use it in GitHub Desktop.
optimizing nsq

From "Lessons Learned Optimizing NSQ"

Reducing GC Pressure

  • avoid []byte to string conversions
  • re-use buffers or objects (use sync.Pool in Go 1.3)
  • pre-allocate slices make([]byte, 0, 1024)
  • explicitly specify number/size of items on the wire
  • leave nothing unbounded
  • apply sane limits to configurable dials (message size, # of messages)
  • avoid boxing (use of interface{}) or unnecessary wrapper types
  • avoid the use of defer in hot code paths (it allocates)

Other lessons learned

don’t be afraid of the sync package

  • channels are overkill for primitives
  • goroutines are cheap not free (~4k per)
  • use worker pools rather than “goroutine per X”
  • synchronizing goroutine exit and cleanup is hard
  • all IO must have timeouts (guarantee progress)
  • wrap IO with bufio.Reader/Writer to reduce context switches (syscalls)
  • select skips nil channels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment