-
Notifications
You must be signed in to change notification settings - Fork 530
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 #3284 from djspiewak/feature/specialize-deferred
- Loading branch information
Showing
26 changed files
with
209 additions
and
38 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
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: 73 additions & 0 deletions
73
benchmarks/src/main/scala/cats/effect/benchmarks/DeferredBenchmark.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,73 @@ | ||
/* | ||
* Copyright 2020-2022 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.benchmarks | ||
|
||
import cats.effect._ | ||
import cats.effect.unsafe.implicits.global | ||
import cats.syntax.all._ | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* To do comparative benchmarks between versions: | ||
* | ||
* benchmarks/run-benchmark DeferredBenchmark | ||
* | ||
* This will generate results in `benchmarks/results`. | ||
* | ||
* Or to run the benchmark from within sbt: | ||
* | ||
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DeferredBenchmark | ||
* | ||
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that | ||
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but | ||
* more is better. | ||
*/ | ||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class DeferredBenchmark { | ||
|
||
@Param(Array("10", "100", "1000")) | ||
var count: Int = _ | ||
|
||
@Benchmark | ||
def get(): Unit = { | ||
IO.deferred[Unit] | ||
.flatMap(d => d.complete(()) *> d.get.replicateA_(count)) | ||
.replicateA_(1000) | ||
.unsafeRunSync() | ||
} | ||
|
||
@Benchmark | ||
def complete(): Unit = { | ||
IO.deferred[Unit] | ||
.flatMap { d => d.get.parReplicateA_(count) &> d.complete(()) } | ||
.replicateA_(1000) | ||
.unsafeRunSync() | ||
} | ||
|
||
@Benchmark | ||
def cancel(): Unit = { | ||
IO.deferred[Unit] | ||
.flatMap { d => d.get.start.replicateA(count).flatMap(_.traverse(_.cancel)) } | ||
.replicateA_(1000) | ||
.unsafeRunSync() | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2020-2022 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 | ||
|
||
import cats.effect.syntax.all._ | ||
import cats.syntax.all._ | ||
import cats.~> | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
private final class IODeferred[A] extends Deferred[IO, A] { | ||
import IODeferred.Sentinel | ||
|
||
private[this] val cell = new AtomicReference[AnyRef](Sentinel) | ||
private[this] val callbacks = new CallbackStack[Right[Nothing, A]](null) | ||
|
||
def complete(a: A): IO[Boolean] = IO { | ||
if (cell.compareAndSet(Sentinel, a.asInstanceOf[AnyRef])) { | ||
val _ = callbacks(Right(a), false) | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
def get: IO[A] = IO defer { | ||
val back = cell.get() | ||
|
||
if (back eq Sentinel) { | ||
IO.cont[A, A](new Cont[IO, A, A] { | ||
def apply[G[_]: MonadCancelThrow] = { | ||
(cb: Either[Throwable, A] => Unit, get: G[A], lift: IO ~> G) => | ||
MonadCancel[G] uncancelable { poll => | ||
val gga = lift { | ||
IO { | ||
val handle = callbacks.push(cb) | ||
|
||
val back = cell.get() | ||
if (back eq Sentinel) { | ||
poll(get).onCancel(lift(IO(handle.clearCurrent()))) | ||
} else { | ||
handle.clearCurrent() | ||
back.asInstanceOf[A].pure[G] | ||
} | ||
} | ||
} | ||
|
||
gga.flatten | ||
} | ||
} | ||
}) | ||
} else { | ||
IO.pure(back.asInstanceOf[A]) | ||
} | ||
} | ||
|
||
def tryGet: IO[Option[A]] = IO { | ||
val back = cell.get() | ||
if (back eq Sentinel) | ||
None | ||
else | ||
Some(back.asInstanceOf[A]) | ||
} | ||
} | ||
|
||
private object IODeferred { | ||
private val Sentinel = new AnyRef | ||
} |
Oops, something went wrong.