You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.5 KiB

  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vellum
  15. import (
  16. "hash"
  17. "hash/fnv"
  18. )
  19. type registryCell struct {
  20. addr int
  21. node *builderNode
  22. }
  23. type registry struct {
  24. table []registryCell
  25. tableSize uint
  26. mruSize uint
  27. hasher hash.Hash64
  28. }
  29. func newRegistry(tableSize, mruSize int) *registry {
  30. nsize := tableSize * mruSize
  31. rv := &registry{
  32. table: make([]registryCell, nsize),
  33. tableSize: uint(tableSize),
  34. mruSize: uint(mruSize),
  35. hasher: fnv.New64a(),
  36. }
  37. return rv
  38. }
  39. func (r *registry) Reset() {
  40. for i := 0; i < len(r.table); i++ {
  41. r.table[i] = registryCell{}
  42. }
  43. r.hasher.Reset()
  44. }
  45. func (r *registry) entry(node *builderNode) (bool, int, *registryCell) {
  46. if len(r.table) == 0 {
  47. return false, 0, nil
  48. }
  49. bucket := r.hash(node)
  50. start := r.mruSize * uint(bucket)
  51. end := start + r.mruSize
  52. rc := registryCache(r.table[start:end])
  53. return rc.entry(node)
  54. }
  55. const fnvPrime = 1099511628211
  56. func (r *registry) hash(b *builderNode) int {
  57. var final uint64
  58. if b.final {
  59. final = 1
  60. }
  61. var h uint64 = 14695981039346656037
  62. h = (h ^ final) * fnvPrime
  63. h = (h ^ b.finalOutput) * fnvPrime
  64. for _, t := range b.trans {
  65. h = (h ^ uint64(t.in)) * fnvPrime
  66. h = (h ^ t.out) * fnvPrime
  67. h = (h ^ uint64(t.addr)) * fnvPrime
  68. }
  69. return int(h % uint64(r.tableSize))
  70. }
  71. type registryCache []registryCell
  72. func (r registryCache) entry(node *builderNode) (bool, int, *registryCell) {
  73. if len(r) == 1 {
  74. if r[0].node != nil && r[0].node.equiv(node) {
  75. return true, r[0].addr, nil
  76. }
  77. r[0].node = node
  78. return false, 0, &r[0]
  79. }
  80. for i := range r {
  81. if r[i].node != nil && r[i].node.equiv(node) {
  82. addr := r[i].addr
  83. r.promote(i)
  84. return true, addr, nil
  85. }
  86. }
  87. // no match
  88. last := len(r) - 1
  89. r[last].node = node // discard LRU
  90. r.promote(last)
  91. return false, 0, &r[0]
  92. }
  93. func (r registryCache) promote(i int) {
  94. for i > 0 {
  95. r.swap(i-1, i)
  96. i--
  97. }
  98. }
  99. func (r registryCache) swap(i, j int) {
  100. r[i], r[j] = r[j], r[i]
  101. }