sketch.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package cache
  2. const sketchDepth = 4
  3. // countMinSketch is an implementation of count-min sketch with 4-bit counters.
  4. // See http://dimacs.rutgers.edu/~graham/pubs/papers/cmsoft.pdf
  5. type countMinSketch struct {
  6. counters []uint64
  7. mask uint32
  8. }
  9. // init initialize count-min sketch with the given width.
  10. func (c *countMinSketch) init(width int) {
  11. // Need (width x 4 x 4) bits = width/4 x uint64
  12. size := nextPowerOfTwo(uint32(width)) >> 2
  13. if size < 1 {
  14. size = 1
  15. }
  16. c.mask = size - 1
  17. if len(c.counters) == int(size) {
  18. c.clear()
  19. } else {
  20. c.counters = make([]uint64, size)
  21. }
  22. }
  23. // add increases counters associated with the given hash.
  24. func (c *countMinSketch) add(h uint64) {
  25. h1, h2 := uint32(h), uint32(h>>32)
  26. for i := uint32(0); i < sketchDepth; i++ {
  27. idx, off := c.position(h1 + i*h2)
  28. c.inc(idx, (16*i)+off)
  29. }
  30. }
  31. // estimate returns minimum value of counters associated with the given hash.
  32. func (c *countMinSketch) estimate(h uint64) uint8 {
  33. h1, h2 := uint32(h), uint32(h>>32)
  34. var min uint8 = 0xFF
  35. for i := uint32(0); i < sketchDepth; i++ {
  36. idx, off := c.position(h1 + i*h2)
  37. count := c.val(idx, (16*i)+off)
  38. if count < min {
  39. min = count
  40. }
  41. }
  42. return min
  43. }
  44. // reset divides all counters by two.
  45. func (c *countMinSketch) reset() {
  46. for i, v := range c.counters {
  47. if v != 0 {
  48. c.counters[i] = (v >> 1) & 0x7777777777777777
  49. }
  50. }
  51. }
  52. func (c *countMinSketch) position(h uint32) (idx uint32, off uint32) {
  53. idx = (h >> 2) & c.mask
  54. off = (h & 3) << 2
  55. return
  56. }
  57. // inc increases value at index idx.
  58. func (c *countMinSketch) inc(idx, off uint32) {
  59. v := c.counters[idx]
  60. count := uint8(v>>off) & 0x0F
  61. if count < 15 {
  62. c.counters[idx] = v + (1 << off)
  63. }
  64. }
  65. // val returns value at index idx.
  66. func (c *countMinSketch) val(idx, off uint32) uint8 {
  67. v := c.counters[idx]
  68. return uint8(v>>off) & 0x0F
  69. }
  70. func (c *countMinSketch) clear() {
  71. for i := range c.counters {
  72. c.counters[i] = 0
  73. }
  74. }
  75. // nextPowerOfTwo returns the smallest power of two which is greater than or equal to i.
  76. func nextPowerOfTwo(i uint32) uint32 {
  77. n := i - 1
  78. n |= n >> 1
  79. n |= n >> 2
  80. n |= n >> 4
  81. n |= n >> 8
  82. n |= n >> 16
  83. n++
  84. return n
  85. }