Skip to content

Commit

Permalink
Merge pull request #4 from olliNiinivaara/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
olliNiinivaara authored Aug 13, 2023
2 parents 95b01fd + b9b93c0 commit d472ee8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Concurrent hash table for Nim. Excellent way to safely share arbitrary data betw
## Example

```nim
# nim c -r --gc:orc --threads:on example.nim
# nim r example.nim
import os, random, stashtable
type SharedData = StashTable[string, seq[string], 100]
Expand All @@ -25,8 +25,8 @@ echo shareddata

## Installation

latest stable release (1.2.1):
`nimble install StashTable`
latest stable release (1.2.2):
`atlas use StashTable`

## Documentation
https://olliNiinivaara.github.io/StashTable/
Expand Down
16 changes: 8 additions & 8 deletions src/stashtable.nim → stashtable.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
#
# StashTable, a contender for Nim stdlib's SharedTable
# (c) Copyright 2020-2021 Olli Niinivaara
# (c) Copyright 2020-2023 Olli Niinivaara
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
Expand Down Expand Up @@ -151,10 +151,10 @@ iterator keys*[K, V, Capacity](stash: StashTable[K, V, Capacity]): (K , Index) =
for i in 0 .. stash.freeindex - 1:
if likely(not(stash.storage[i].hash == int(NotInStash))): yield (stash.storage[i].key , Index(i))

template hashis(): int =
template hashis[K, V, Capacity](stash: StashTable[K, V, Capacity]): int =
hash(key) and stash.hashes.high

template hashis(key: untyped): int =
template hashis[K, V, Capacity](stash: StashTable[K, V, Capacity], key: untyped): int =
hash(key) and stash.hashes.high

proc len*(stash: StashTable): int {.inline.} =
Expand All @@ -164,7 +164,7 @@ proc len*(stash: StashTable): int {.inline.} =
proc findIndex*[K, V, Capacity](stash: StashTable[K, V, Capacity], key: K): Index =
## Returns ``Index`` for given ``key``, or ``NotInStash`` if key was not in ``stash``.
## Note that the returned ``Index`` may be invalidated at any moment by other threads.
let h = hashis()
let h = hashis(stash)
#let h = stash.hashis(key)
if stash.hashes[h].count == 0: return NotInStash
var founds = 0
Expand Down Expand Up @@ -277,7 +277,7 @@ proc reserveIndex[K, V, Capacity](thestash: StashTable[K, V, Capacity]): Index {
assert(thestash[result].hash == int(NotInStash))

template useIndex(stash: StashTable) =
let h = hashis()
let h = stash.hashis()
withLock(stash[index].lock):
if stash.hashes[h].first == NotInStash or index.int < stash.hashes[h].first.int:
if stash.hashes[h].last == NotInStash: stash.hashes[h].last = stash.hashes[h].first
Expand Down Expand Up @@ -348,7 +348,7 @@ proc addAll*[K; V; Capacity1: static int, Capacity2: static int](
return true

proc removeHash[K, V, Capacity](stash: StashTable[K, V, Capacity], index: Index, key: K) {.inline.} =
let h = hashis()
let h = stash.hashis()
stash.hashes[h].count.dec
if stash.hashes[h].count == 0:
stash.hashes[h].first = NotInStash
Expand All @@ -359,7 +359,7 @@ proc removeHash[K, V, Capacity](stash: StashTable[K, V, Capacity], index: Index,
stash.hashes[h].last = NotInStash
else:
for i in stash.hashes[h].first.int + 1 ..< stash.hashes[h].last.int:
if(unlikely) hashis(stash.storage[i].key) == h:
if(unlikely) stash.hashis(stash.storage[i].key) == h:
if likely(not(stash.storage[i].hash == int(NotInStash))):
stash.hashes[h].first = Index(i)
return
Expand All @@ -368,7 +368,7 @@ proc removeHash[K, V, Capacity](stash: StashTable[K, V, Capacity], index: Index,
stash.hashes[h].last = NotInStash
else:
for i in countdown(stash.hashes[h].last.int - 1, stash.hashes[h].first.int + 1):
if(unlikely) hashis(stash.storage[i].key) == h:
if(unlikely) stash.hashis(stash.storage[i].key) == h:
if likely(not(stash.storage[i].hash == int(NotInStash))):
stash.hashes[h].last = Index(i)
return
Expand Down
8 changes: 2 additions & 6 deletions stashtable.nimble
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Package

version = "1.2.1"
version = "1.2.2"
author = "Olli Niinivaara"
description = "Concurrent hash table"
license = "MIT"
srcDir = "src"

# Dependencies

requires "nim >= 1.4.2"
installFiles = @["stashtable.nim"]
3 changes: 0 additions & 3 deletions tests/config.nims

This file was deleted.

2 changes: 2 additions & 0 deletions tests/nim.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-d:danger
-p:".."
5 changes: 2 additions & 3 deletions tests/testing.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## testshared

testshared.nim compares StashTable against [SharedTable](https://nim-lang.org/docs/sharedtables.html). You can benchmark different
aspects by modifying the consts in file.
testshared.nim compares StashTable against [SharedTable](https://nim-lang.org/docs/sharedtables.html). You can benchmark different aspects by modifying the consts in file.

Essentially, SharedTable will drastically slow down when reading or writing
takes some time (simulateio parameter in test.nim), while StashTable just keeps going.
Expand All @@ -27,7 +26,7 @@ This nondeterminism is not an implication that either StashTable or SharedTable

## testtable

testshared.nim compares StashTable against Nim stdlib's single-threaded [TableRef](https://nim-lang.org/docs/tables.html).
testtable.nim compares StashTable against Nim stdlib's single-threaded [TableRef](https://nim-lang.org/docs/tables.html).

Essentially, StashTable is bit slower than Table, but searching for a key and iterating over all keys
are much faster with StashTable (because those operations do not need locking).
Expand Down
2 changes: 1 addition & 1 deletion tests/testtable.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
when not compileOption("threads"):
{.fatal: "threads are not enabled".}

import ../src/stashtable, tables
import stashtable, tables

import unittest
import random
Expand Down

0 comments on commit d472ee8

Please sign in to comment.