sharedtables.nim 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import
  14. hashes, math, locks
  15. include "system/inclrtl"
  16. type
  17. KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
  18. KeyValuePairSeq[A, B] = ptr array[10_000_000, KeyValuePair[A, B]]
  19. SharedTable* [A, B] = object ## generic hash SharedTable
  20. data: KeyValuePairSeq[A, B]
  21. counter, dataLen: int
  22. lock: Lock
  23. template maxHash(t): untyped = t.dataLen-1
  24. include tableimpl
  25. proc enlarge[A, B](t: var SharedTable[A, B]) =
  26. let oldSize = t.dataLen
  27. let size = oldSize * growthFactor
  28. var n = cast[KeyValuePairSeq[A, B]](allocShared0(
  29. sizeof(KeyValuePair[A, B]) * size))
  30. t.dataLen = size
  31. swap(t.data, n)
  32. for i in 0..<oldSize:
  33. let eh = n[i].hcode
  34. if isFilled(eh):
  35. var j: Hash = eh and maxHash(t)
  36. while isFilled(t.data[j].hcode):
  37. j = nextTry(j, maxHash(t))
  38. rawInsert(t, t.data, n[i].key, n[i].val, eh, j)
  39. deallocShared(n)
  40. template withLock(t, x: untyped) =
  41. acquire(t.lock)
  42. x
  43. release(t.lock)
  44. template withValue*[A, B](t: var SharedTable[A, B], key: A,
  45. value, body: untyped) =
  46. ## retrieves the value at ``t[key]``.
  47. ## `value` can be modified in the scope of the ``withValue`` call.
  48. ##
  49. ## .. code-block:: nim
  50. ##
  51. ## sharedTable.withValue(key, value) do:
  52. ## # block is executed only if ``key`` in ``t``
  53. ## # value is threadsafe in block
  54. ## value.name = "username"
  55. ## value.uid = 1000
  56. ##
  57. acquire(t.lock)
  58. try:
  59. var hc: Hash
  60. var index = rawGet(t, key, hc)
  61. let hasKey = index >= 0
  62. if hasKey:
  63. var value {.inject.} = addr(t.data[index].val)
  64. body
  65. finally:
  66. release(t.lock)
  67. template withValue*[A, B](t: var SharedTable[A, B], key: A,
  68. value, body1, body2: untyped) =
  69. ## retrieves the value at ``t[key]``.
  70. ## `value` can be modified in the scope of the ``withValue`` call.
  71. ##
  72. ## .. code-block:: nim
  73. ##
  74. ## sharedTable.withValue(key, value) do:
  75. ## # block is executed only if ``key`` in ``t``
  76. ## # value is threadsafe in block
  77. ## value.name = "username"
  78. ## value.uid = 1000
  79. ## do:
  80. ## # block is executed when ``key`` not in ``t``
  81. ## raise newException(KeyError, "Key not found")
  82. ##
  83. acquire(t.lock)
  84. try:
  85. var hc: Hash
  86. var index = rawGet(t, key, hc)
  87. let hasKey = index >= 0
  88. if hasKey:
  89. var value {.inject.} = addr(t.data[index].val)
  90. body1
  91. else:
  92. body2
  93. finally:
  94. release(t.lock)
  95. proc mget*[A, B](t: var SharedTable[A, B], key: A): var B =
  96. ## retrieves the value at ``t[key]``. The value can be modified.
  97. ## If `key` is not in `t`, the ``KeyError`` exception is raised.
  98. withLock t:
  99. var hc: Hash
  100. var index = rawGet(t, key, hc)
  101. let hasKey = index >= 0
  102. if hasKey: result = t.data[index].val
  103. if not hasKey:
  104. when compiles($key):
  105. raise newException(KeyError, "key not found: " & $key)
  106. else:
  107. raise newException(KeyError, "key not found")
  108. proc mgetOrPut*[A, B](t: var SharedTable[A, B], key: A, val: B): var B =
  109. ## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
  110. ## returning a value which can be modified. **Note**: This is inherently
  111. ## unsafe in the context of multi-threading since it returns a pointer
  112. ## to ``B``.
  113. withLock t:
  114. mgetOrPutImpl(enlarge)
  115. proc hasKeyOrPut*[A, B](t: var SharedTable[A, B], key: A, val: B): bool =
  116. ## returns true iff `key` is in the table, otherwise inserts `value`.
  117. withLock t:
  118. hasKeyOrPutImpl(enlarge)
  119. proc withKey*[A, B](t: var SharedTable[A, B], key: A,
  120. mapper: proc(key: A, val: var B, pairExists: var bool)) =
  121. ## Computes a new mapping for the ``key`` with the specified ``mapper``
  122. ## procedure.
  123. ##
  124. ## The ``mapper`` takes 3 arguments:
  125. ##
  126. ## 1. ``key`` - the current key, if it exists, or the key passed to
  127. ## ``withKey`` otherwise;
  128. ## 2. ``val`` - the current value, if the key exists, or default value
  129. ## of the type otherwise;
  130. ## 3. ``pairExists`` - ``true`` if the key exists, ``false`` otherwise.
  131. ##
  132. ## The ``mapper`` can can modify ``val`` and ``pairExists`` values to change
  133. ## the mapping of the key or delete it from the table.
  134. ## When adding a value, make sure to set ``pairExists`` to ``true`` along
  135. ## with modifying the ``val``.
  136. ##
  137. ## The operation is performed atomically and other operations on the table
  138. ## will be blocked while the ``mapper`` is invoked, so it should be short and
  139. ## simple.
  140. ##
  141. ## Example usage:
  142. ##
  143. ## .. code-block:: nim
  144. ##
  145. ## # If value exists, decrement it.
  146. ## # If it becomes zero or less, delete the key
  147. ## t.withKey(1'i64) do (k: int64, v: var int, pairExists: var bool):
  148. ## if pairExists:
  149. ## dec v
  150. ## if v <= 0:
  151. ## pairExists = false
  152. withLock t:
  153. var hc: Hash
  154. var index = rawGet(t, key, hc)
  155. var pairExists = index >= 0
  156. if pairExists:
  157. mapper(t.data[index].key, t.data[index].val, pairExists)
  158. if not pairExists:
  159. delImplIdx(t, index)
  160. else:
  161. var val: B
  162. mapper(key, val, pairExists)
  163. if pairExists:
  164. maybeRehashPutImpl(enlarge)
  165. proc `[]=`*[A, B](t: var SharedTable[A, B], key: A, val: B) =
  166. ## puts a (key, value)-pair into `t`.
  167. withLock t:
  168. putImpl(enlarge)
  169. proc add*[A, B](t: var SharedTable[A, B], key: A, val: B) =
  170. ## puts a new (key, value)-pair into `t` even if ``t[key]`` already exists.
  171. ## This can introduce duplicate keys into the table!
  172. withLock t:
  173. addImpl(enlarge)
  174. proc del*[A, B](t: var SharedTable[A, B], key: A) =
  175. ## deletes `key` from hash table `t`.
  176. withLock t:
  177. delImpl()
  178. proc init*[A, B](t: var SharedTable[A, B], initialSize=64) =
  179. ## creates a new hash table that is empty.
  180. ##
  181. ## `initialSize` needs to be a power of two. If you need to accept runtime
  182. ## values for this you could use the ``nextPowerOfTwo`` proc from the
  183. ## `math <math.html>`_ module or the ``rightSize`` proc from this module.
  184. assert isPowerOfTwo(initialSize)
  185. t.counter = 0
  186. t.dataLen = initialSize
  187. t.data = cast[KeyValuePairSeq[A, B]](allocShared0(
  188. sizeof(KeyValuePair[A, B]) * initialSize))
  189. initLock t.lock
  190. proc deinitSharedTable*[A, B](t: var SharedTable[A, B]) =
  191. deallocShared(t.data)
  192. deinitLock t.lock
  193. proc initSharedTable*[A, B](initialSize=64): SharedTable[A, B] {.deprecated.} =
  194. ## Deprecated. Use `init` instead.
  195. ## This is not posix compliant, may introduce undefined behavior.
  196. assert isPowerOfTwo(initialSize)
  197. result.counter = 0
  198. result.dataLen = initialSize
  199. result.data = cast[KeyValuePairSeq[A, B]](allocShared0(
  200. sizeof(KeyValuePair[A, B]) * initialSize))
  201. initLock result.lock