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.

registry.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. type registryCell struct {
  16. addr int
  17. node *builderNode
  18. }
  19. type registry struct {
  20. builderNodePool *builderNodePool
  21. table []registryCell
  22. tableSize uint
  23. mruSize uint
  24. }
  25. func newRegistry(p *builderNodePool, tableSize, mruSize int) *registry {
  26. nsize := tableSize * mruSize
  27. rv := &registry{
  28. builderNodePool: p,
  29. table: make([]registryCell, nsize),
  30. tableSize: uint(tableSize),
  31. mruSize: uint(mruSize),
  32. }
  33. return rv
  34. }
  35. func (r *registry) Reset() {
  36. var empty registryCell
  37. for i := range r.table {
  38. r.builderNodePool.Put(r.table[i].node)
  39. r.table[i] = empty
  40. }
  41. }
  42. func (r *registry) entry(node *builderNode) (bool, int, *registryCell) {
  43. if len(r.table) == 0 {
  44. return false, 0, nil
  45. }
  46. bucket := r.hash(node)
  47. start := r.mruSize * uint(bucket)
  48. end := start + r.mruSize
  49. rc := registryCache(r.table[start:end])
  50. return rc.entry(node, r.builderNodePool)
  51. }
  52. const fnvPrime = 1099511628211
  53. func (r *registry) hash(b *builderNode) int {
  54. var final uint64
  55. if b.final {
  56. final = 1
  57. }
  58. var h uint64 = 14695981039346656037
  59. h = (h ^ final) * fnvPrime
  60. h = (h ^ b.finalOutput) * fnvPrime
  61. for _, t := range b.trans {
  62. h = (h ^ uint64(t.in)) * fnvPrime
  63. h = (h ^ t.out) * fnvPrime
  64. h = (h ^ uint64(t.addr)) * fnvPrime
  65. }
  66. return int(h % uint64(r.tableSize))
  67. }
  68. type registryCache []registryCell
  69. func (r registryCache) entry(node *builderNode, pool *builderNodePool) (bool, int, *registryCell) {
  70. if len(r) == 1 {
  71. if r[0].node != nil && r[0].node.equiv(node) {
  72. return true, r[0].addr, nil
  73. }
  74. pool.Put(r[0].node)
  75. r[0].node = node
  76. return false, 0, &r[0]
  77. }
  78. for i := range r {
  79. if r[i].node != nil && r[i].node.equiv(node) {
  80. addr := r[i].addr
  81. r.promote(i)
  82. return true, addr, nil
  83. }
  84. }
  85. // no match
  86. last := len(r) - 1
  87. pool.Put(r[last].node)
  88. r[last].node = node // discard LRU
  89. r.promote(last)
  90. return false, 0, &r[0]
  91. }
  92. func (r registryCache) promote(i int) {
  93. for i > 0 {
  94. r.swap(i-1, i)
  95. i--
  96. }
  97. }
  98. func (r registryCache) swap(i, j int) {
  99. r[i], r[j] = r[j], r[i]
  100. }