-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1913 from vasilmkd/striped
Striped fiber callback hashtable
- Loading branch information
Showing
7 changed files
with
257 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 0 additions & 73 deletions
73
core/shared/src/main/scala/cats/effect/unsafe/FiberErrorHashtable.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/shared/src/main/scala/cats/effect/unsafe/StripedHashtable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.unsafe | ||
|
||
/** | ||
* A conceptual hash table which balances between several | ||
* [[ThreadSafeHashtable]]s, in order to reduce the contention on the single | ||
* lock by spreading it to several different locks controlling parts of the | ||
* hash table. | ||
*/ | ||
private[effect] final class StripedHashtable { | ||
val numTables: Int = { | ||
val cpus = Runtime.getRuntime().availableProcessors() | ||
// Bit twiddling hacks. | ||
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 | ||
var value = cpus - 1 | ||
value |= value >> 1 | ||
value |= value >> 2 | ||
value |= value >> 4 | ||
value |= value >> 8 | ||
value |= value >> 16 | ||
value + 1 | ||
} | ||
|
||
private[this] val mask: Int = numTables - 1 | ||
|
||
private[this] val initialCapacity: Int = 8 | ||
|
||
val tables: Array[ThreadSafeHashtable] = { | ||
val array = new Array[ThreadSafeHashtable](numTables) | ||
var i = 0 | ||
while (i < numTables) { | ||
array(i) = new ThreadSafeHashtable(initialCapacity) | ||
i += 1 | ||
} | ||
array | ||
} | ||
|
||
def put(cb: Throwable => Unit): Unit = { | ||
val hash = System.identityHashCode(cb) | ||
val idx = hash & mask | ||
tables(idx).put(cb, hash) | ||
} | ||
|
||
def remove(cb: Throwable => Unit): Unit = { | ||
val hash = System.identityHashCode(cb) | ||
val idx = hash & mask | ||
tables(idx).remove(cb, hash) | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
core/shared/src/main/scala/cats/effect/unsafe/ThreadSafeHashtable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
package unsafe | ||
|
||
/** | ||
* A primitive thread safe hash table implementation specialized for a single | ||
* purpose, to hold references to the error callbacks of fibers. The hashing | ||
* function is [[System.identityHashCode]] simply because the callbacks are | ||
* functions and therefore have no defined notion of [[Object#hashCode]]. The | ||
* thread safety is achieved by pessimistically locking the whole structure. | ||
* This is fine in practice because this data structure is only accessed when | ||
* running [[cats.effect.IO#unsafeRunFiber]], which is not expected to be | ||
* executed often in a realistic system. | ||
* | ||
* @param initialCapacity the initial capacity of the hashtable, ''must'' be a | ||
* power of 2 | ||
*/ | ||
private[effect] final class ThreadSafeHashtable(initialCapacity: Int) { | ||
var hashtable: Array[Throwable => Unit] = new Array(initialCapacity) | ||
private[this] var size = 0 | ||
private[this] var mask = initialCapacity - 1 | ||
private[this] var capacity = initialCapacity | ||
|
||
def put(cb: Throwable => Unit, hash: Int): Unit = this.synchronized { | ||
val cap = capacity | ||
if (size == cap) { | ||
val newCap = cap * 2 | ||
val newHashtable = new Array[Throwable => Unit](newCap) | ||
System.arraycopy(hashtable, 0, newHashtable, 0, cap) | ||
hashtable = newHashtable | ||
mask = newCap - 1 | ||
capacity = newCap | ||
} | ||
|
||
var idx = hash & mask | ||
while (true) { | ||
if (hashtable(idx) == null) { | ||
hashtable(idx) = cb | ||
size += 1 | ||
return | ||
} else { | ||
idx += 1 | ||
idx &= mask | ||
} | ||
} | ||
} | ||
|
||
def remove(cb: Throwable => Unit, hash: Int): Unit = this.synchronized { | ||
val init = hash & mask | ||
var idx = init | ||
while (true) { | ||
if (cb eq hashtable(idx)) { | ||
hashtable(idx) = null | ||
size -= 1 | ||
return | ||
} else { | ||
idx += 1 | ||
idx &= mask | ||
if (idx == init) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/jvm/src/test/scala/cats/effect/unsafe/StripedHashtableSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
package unsafe | ||
|
||
import cats.syntax.parallel._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
import java.util.concurrent.CountDownLatch | ||
|
||
class StripedHashtableSpec extends BaseSpec with Runners { | ||
|
||
override def executionTimeout: FiniteDuration = 30.seconds | ||
|
||
def hashtableRuntime(): IORuntime = { | ||
lazy val rt: IORuntime = { | ||
val (blocking, blockDown) = | ||
IORuntime.createDefaultBlockingExecutionContext(threadPrefix = | ||
s"io-blocking-${getClass.getName}") | ||
val (scheduler, schedDown) = | ||
IORuntime.createDefaultScheduler(threadPrefix = s"io-scheduler-${getClass.getName}") | ||
val (compute, compDown) = | ||
IORuntime.createDefaultComputeThreadPool( | ||
rt, | ||
threadPrefix = s"io-compute-${getClass.getName}") | ||
|
||
new IORuntime( | ||
compute, | ||
blocking, | ||
scheduler, | ||
{ () => | ||
compDown() | ||
blockDown() | ||
schedDown() | ||
}, | ||
IORuntimeConfig() | ||
) | ||
} | ||
|
||
rt | ||
} | ||
|
||
"StripedHashtable" should { | ||
"work correctly in the presence of many unsafeRuns" in real { | ||
val iterations = 1000000 | ||
|
||
object Boom extends RuntimeException("Boom!") | ||
|
||
def io(n: Int): IO[Unit] = | ||
(n % 3) match { | ||
case 0 => IO.unit | ||
case 1 => IO.canceled | ||
case 2 => IO.raiseError[Unit](Boom) | ||
} | ||
|
||
Resource.make(IO(hashtableRuntime()))(rt => IO(rt.shutdown())).use { rt => | ||
IO(new CountDownLatch(iterations)).flatMap { counter => | ||
(0 until iterations) | ||
.toList | ||
.parTraverse { n => IO(io(n).unsafeRunAsync { _ => counter.countDown() }(rt)) } | ||
.flatMap { _ => IO.blocking(counter.await()) } | ||
.flatMap { _ => | ||
IO.blocking { | ||
rt.fiberErrorCbs.synchronized { | ||
rt.fiberErrorCbs.tables.forall(_.hashtable.forall(_ eq null)) mustEqual true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |