Source File
ring.go
Belonging Package
github.com/dgraph-io/ristretto/v2
/** SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>* SPDX-License-Identifier: Apache-2.0*/package ristrettoimport ()// ringConsumer is the user-defined object responsible for receiving and// processing items in batches when buffers are drained.type ringConsumer interface {Push([]uint64) bool}// ringStripe is a singular ring buffer that is not concurrent safe.type ringStripe struct {cons ringConsumerdata []uint64capa int}func newRingStripe( ringConsumer, int64) *ringStripe {return &ringStripe{cons: ,data: make([]uint64, 0, ),capa: int(),}}// Push appends an item in the ring buffer and drains (copies items and// sends to Consumer) if full.func ( *ringStripe) ( uint64) {.data = append(.data, )// Decide if the ring buffer should be drained.if len(.data) >= .capa {// Send elements to consumer and create a new ring stripe.if .cons.Push(.data) {.data = make([]uint64, 0, .capa)} else {.data = .data[:0]}}}// ringBuffer stores multiple buffers (stripes) and distributes Pushed items// between them to lower contention.//// This implements the "batching" process described in the BP-Wrapper paper// (section III part A).type ringBuffer struct {pool *sync.Pool}// newRingBuffer returns a striped ring buffer. The Consumer in ringConfig will// be called when individual stripes are full and need to drain their elements.func newRingBuffer( ringConsumer, int64) *ringBuffer {// LOSSY buffers use a very simple sync.Pool for concurrently reusing// stripes. We do lose some stripes due to GC (unheld items in sync.Pool// are cleared), but the performance gains generally outweigh the small// percentage of elements lost. The performance primarily comes from// low-level runtime functions used in the standard library that aren't// available to us (such as runtime_procPin()).return &ringBuffer{pool: &sync.Pool{New: func() interface{} { return newRingStripe(, ) },},}}// Push adds an element to one of the internal stripes and possibly drains if// the stripe becomes full.func ( *ringBuffer) ( uint64) {// Reuse or create a new stripe.:= .pool.Get().(*ringStripe).Push().pool.Put()}
![]() |
The pages are generated with Golds v0.8.4. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |