Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Compromising Readability in Search of Performance

Compromising Readability in Search of Performance

We review a series of actual changes we've made the bleve and vellum projects to improve performance. Most of these relate to reducing memory allocations at runtime, so we look a little closer at those techniques. Finally, we consider the cost of readability to the codebase when we accept these changes.

Marty Schoch

June 12, 2018
Tweet

More Decks by Marty Schoch

Other Decks in Technology

Transcript

  1. RUNTIME SIGNATURES TO LOOK FOR makeslice growslice someslice := make([]*thing,

    1024) someslice = append(someslice, anotherslice) newobject something := &thing{…}
  2. REDUCE THE NEED TO GROW A SLICE Old version, calls

    to append() may have to grow slice
  3. REDUCE THE NEED TO GROW A SLICE New version, original

    allocation is sufficient, no need to grow.
  4. REUSING INTERNAL VS EXTERNAL Internal External Pro Simpler API Caller

    has full control of reuse (or never use it at all by passing in nil) Con Lifcecycle of reuse is dictated by the component Clutters the API NOTE: in both cases the caller can misuse and get into trouble
  5. REUSE OFTEN REQUIRES RESET struct slice *thing = Thing{} someslice

    = someslice[:0] All struct members go back to zero-value. Slice len set to 0, cap remains
  6. RESET IN PRACTICE Hang on to slice reference, reset struct

    to zero values, reset slice, associate struct reference to reset slice.
  7. REUSING VIA POOLS (MANUAL) ALLOCATE IN BLOCKS OF 256 AT

    A TIME RESET ITEM ON WAY OUT OF POOL
  8. REUSING VIA SYNC.POOL … do work reusing vcd (visit document

    context) get from pool return to pool PRO: reuse spans goroutines PRO: runtime reclaims items in the pool during GC CON: hard to use well in practice
  9. NECESSARY COMPLEXITY ▸ Everything you've seen here is real from

    Couchbase's bleve/vellum projects ▸ As ugly as they are, they've all shown measurable improvements to important metrics @mschoch [email protected]