ring.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2019 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package ristretto
  17. import (
  18. "sync"
  19. )
  20. // ringConsumer is the user-defined object responsible for receiving and
  21. // processing items in batches when buffers are drained.
  22. type ringConsumer interface {
  23. Push([]uint64) bool
  24. }
  25. // ringStripe is a singular ring buffer that is not concurrent safe.
  26. type ringStripe struct {
  27. cons ringConsumer
  28. data []uint64
  29. capa int
  30. }
  31. func newRingStripe(cons ringConsumer, capa int64) *ringStripe {
  32. return &ringStripe{
  33. cons: cons,
  34. data: make([]uint64, 0, capa),
  35. capa: int(capa),
  36. }
  37. }
  38. // Push appends an item in the ring buffer and drains (copies items and
  39. // sends to Consumer) if full.
  40. func (s *ringStripe) Push(item uint64) {
  41. s.data = append(s.data, item)
  42. // Decide if the ring buffer should be drained.
  43. if len(s.data) >= s.capa {
  44. // Send elements to consumer and create a new ring stripe.
  45. if s.cons.Push(s.data) {
  46. s.data = make([]uint64, 0, s.capa)
  47. } else {
  48. s.data = s.data[:0]
  49. }
  50. }
  51. }
  52. // ringBuffer stores multiple buffers (stripes) and distributes Pushed items
  53. // between them to lower contention.
  54. //
  55. // This implements the "batching" process described in the BP-Wrapper paper
  56. // (section III part A).
  57. type ringBuffer struct {
  58. pool *sync.Pool
  59. }
  60. // newRingBuffer returns a striped ring buffer. The Consumer in ringConfig will
  61. // be called when individual stripes are full and need to drain their elements.
  62. func newRingBuffer(cons ringConsumer, capa int64) *ringBuffer {
  63. // LOSSY buffers use a very simple sync.Pool for concurrently reusing
  64. // stripes. We do lose some stripes due to GC (unheld items in sync.Pool
  65. // are cleared), but the performance gains generally outweigh the small
  66. // percentage of elements lost. The performance primarily comes from
  67. // low-level runtime functions used in the standard library that aren't
  68. // available to us (such as runtime_procPin()).
  69. return &ringBuffer{
  70. pool: &sync.Pool{
  71. New: func() interface{} { return newRingStripe(cons, capa) },
  72. },
  73. }
  74. }
  75. // Push adds an element to one of the internal stripes and possibly drains if
  76. // the stripe becomes full.
  77. func (b *ringBuffer) Push(item uint64) {
  78. // Reuse or create a new stripe.
  79. stripe := b.pool.Get().(*ringStripe)
  80. stripe.Push(item)
  81. b.pool.Put(stripe)
  82. }