sharedtables.nim 7.3 KB

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