sharedtables.nim 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Shared table support for Nim. Use plain old non GC'ed keys and values or
  10. ## you'll be in trouble. Uses a single lock to protect the table, lockfree
  11. ## implementations welcome but if lock contention is so high that you need a
  12. ## lockfree hash table, you're doing it wrong.
  13. ##
  14. ## Unstable API.
  15. import
  16. hashes, math, locks
  17. type
  18. KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
  19. KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]]
  20. SharedTable*[A, B] = object ## generic hash SharedTable
  21. data: KeyValuePairSeq[A, B]
  22. counter, dataLen: int
  23. lock: Lock
  24. template maxHash(t): untyped = t.dataLen-1
  25. include tableimpl
  26. template st_maybeRehashPutImpl(enlarge) {.dirty.} =
  27. if mustRehash(t):
  28. enlarge(t)
  29. index = rawGetKnownHC(t, key, hc)
  30. index = -1 - index # important to transform for mgetOrPutImpl
  31. rawInsert(t, t.data, key, val, hc, index)
  32. inc(t.counter)
  33. proc enlarge[A, B](t: var SharedTable[A, B]) =
  34. let oldSize = t.dataLen
  35. let size = oldSize * growthFactor
  36. var n = cast[KeyValuePairSeq[A, B]](allocShared0(
  37. sizeof(KeyValuePair[A, B]) * size))
  38. t.dataLen = size
  39. swap(t.data, n)
  40. for i in 0..<oldSize:
  41. let eh = n[i].hcode
  42. if isFilled(eh):
  43. var j: Hash = eh and maxHash(t)
  44. while isFilled(t.data[j].hcode):
  45. j = nextTry(j, maxHash(t))
  46. rawInsert(t, t.data, n[i].key, n[i].val, eh, j)
  47. deallocShared(n)
  48. template withLock(t, x: untyped) =
  49. acquire(t.lock)
  50. x
  51. release(t.lock)
  52. template withValue*[A, B](t: var SharedTable[A, B], key: A,
  53. value, body: untyped) =
  54. ## retrieves the value at ``t[key]``.
  55. ## `value` can be modified in the scope of the ``withValue`` call.
  56. ##
  57. ## .. code-block:: nim
  58. ##
  59. ## sharedTable.withValue(key, value) do:
  60. ## # block is executed only if ``key`` in ``t``
  61. ## # value is threadsafe in block
  62. ## value.name = "username"
  63. ## value.uid = 1000
  64. ##
  65. acquire(t.lock)
  66. try:
  67. var hc: Hash
  68. var index = rawGet(t, key, hc)
  69. let hasKey = index >= 0
  70. if hasKey:
  71. var value {.inject.} = addr(t.data[index].val)
  72. body
  73. finally:
  74. release(t.lock)
  75. template withValue*[A, B](t: var SharedTable[A, B], key: A,
  76. value, body1, body2: untyped) =
  77. ## retrieves the value at ``t[key]``.
  78. ## `value` can be modified in the scope of the ``withValue`` call.
  79. ##
  80. ## .. code-block:: nim
  81. ##
  82. ## sharedTable.withValue(key, value) do:
  83. ## # block is executed only if ``key`` in ``t``
  84. ## # value is threadsafe in block
  85. ## value.name = "username"
  86. ## value.uid = 1000
  87. ## do:
  88. ## # block is executed when ``key`` not in ``t``
  89. ## raise newException(KeyError, "Key not found")
  90. ##
  91. acquire(t.lock)
  92. try:
  93. var hc: Hash
  94. var index = rawGet(t, key, hc)
  95. let hasKey = index >= 0
  96. if hasKey:
  97. var value {.inject.} = addr(t.data[index].val)
  98. body1
  99. else:
  100. body2
  101. finally:
  102. release(t.lock)
  103. proc mget*[A, B](t: var SharedTable[A, B], key: A): var B =
  104. ## retrieves the value at ``t[key]``. The value can be modified.
  105. ## If `key` is not in `t`, the ``KeyError`` exception is raised.
  106. withLock t:
  107. var hc: Hash
  108. var index = rawGet(t, key, hc)
  109. let hasKey = index >= 0
  110. if hasKey: result = t.data[index].val
  111. if not hasKey:
  112. when compiles($key):
  113. raise newException(KeyError, "key not found: " & $key)
  114. else:
  115. raise newException(KeyError, "key not found")
  116. proc mgetOrPut*[A, B](t: var SharedTable[A, B], key: A, val: B): var B =
  117. ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
  118. ## returning a value which can be modified. **Note**: This is inherently
  119. ## unsafe in the context of multi-threading since it returns a pointer
  120. ## to ``B``.
  121. withLock t:
  122. mgetOrPutImpl(enlarge)
  123. proc hasKeyOrPut*[A, B](t: var SharedTable[A, B], key: A, val: B): bool =
  124. ## returns true if `key` is in the table, otherwise inserts `value`.
  125. withLock t:
  126. hasKeyOrPutImpl(enlarge)
  127. template tabMakeEmpty(i) = t.data[i].hcode = 0
  128. template tabCellEmpty(i) = isEmpty(t.data[i].hcode)
  129. template tabCellHash(i) = t.data[i].hcode
  130. proc withKey*[A, B](t: var SharedTable[A, B], key: A,
  131. mapper: proc(key: A, val: var B, pairExists: var bool)) =
  132. ## Computes a new mapping for the ``key`` with the specified ``mapper``
  133. ## procedure.
  134. ##
  135. ## The ``mapper`` takes 3 arguments:
  136. ##
  137. ## 1. ``key`` - the current key, if it exists, or the key passed to
  138. ## ``withKey`` otherwise;
  139. ## 2. ``val`` - the current value, if the key exists, or default value
  140. ## of the type otherwise;
  141. ## 3. ``pairExists`` - ``true`` if the key exists, ``false`` otherwise.
  142. ##
  143. ## The ``mapper`` can can modify ``val`` and ``pairExists`` values to change
  144. ## the mapping of the key or delete it from the table.
  145. ## When adding a value, make sure to set ``pairExists`` to ``true`` along
  146. ## with modifying the ``val``.
  147. ##
  148. ## The operation is performed atomically and other operations on the table
  149. ## will be blocked while the ``mapper`` is invoked, so it should be short and
  150. ## simple.
  151. ##
  152. ## Example usage:
  153. ##
  154. ## .. code-block:: nim
  155. ##
  156. ## # If value exists, decrement it.
  157. ## # If it becomes zero or less, delete the key
  158. ## t.withKey(1'i64) do (k: int64, v: var int, pairExists: var bool):
  159. ## if pairExists:
  160. ## dec v
  161. ## if v <= 0:
  162. ## pairExists = false
  163. withLock t:
  164. var hc: Hash
  165. var index = rawGet(t, key, hc)
  166. var pairExists = index >= 0
  167. if pairExists:
  168. mapper(t.data[index].key, t.data[index].val, pairExists)
  169. if not pairExists:
  170. delImplIdx(t, index, tabMakeEmpty, tabCellEmpty, tabCellHash)
  171. else:
  172. var val: B
  173. mapper(key, val, pairExists)
  174. if pairExists:
  175. st_maybeRehashPutImpl(enlarge)
  176. proc `[]=`*[A, B](t: var SharedTable[A, B], key: A, val: B) =
  177. ## puts a (key, value)-pair into `t`.
  178. withLock t:
  179. putImpl(enlarge)
  180. proc add*[A, B](t: var SharedTable[A, B], key: A, val: B) =
  181. ## puts a new (key, value)-pair into `t` even if ``t[key]`` already exists.
  182. ## This can introduce duplicate keys into the table!
  183. withLock t:
  184. addImpl(enlarge)
  185. proc del*[A, B](t: var SharedTable[A, B], key: A) =
  186. ## deletes `key` from hash table `t`.
  187. withLock t:
  188. delImpl(tabMakeEmpty, tabCellEmpty, tabCellHash)
  189. proc len*[A, B](t: var SharedTable[A, B]): int =
  190. ## number of elements in `t`
  191. withLock t:
  192. result = t.counter
  193. proc init*[A, B](t: var SharedTable[A, B], initialSize = 32) =
  194. ## creates a new hash table that is empty.
  195. ##
  196. ## This proc must be called before any other usage of `t`.
  197. let initialSize = slotsNeeded(initialSize)
  198. t.counter = 0
  199. t.dataLen = initialSize
  200. t.data = cast[KeyValuePairSeq[A, B]](allocShared0(
  201. sizeof(KeyValuePair[A, B]) * initialSize))
  202. initLock t.lock
  203. proc deinitSharedTable*[A, B](t: var SharedTable[A, B]) =
  204. deallocShared(t.data)
  205. deinitLock t.lock