Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
package brc
import monix.eval.Task
import monix.execution
import monix.reactive.Observable
import java.io.{BufferedReader, FileInputStream, InputStreamReader}
import java.text.DecimalFormat
object UsingMonix:
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.Exchanger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
// with changes inspired by Exchanger
public class Rendezvous2 {
private volatile Thread waiting;
private volatile int data = -1; // together with `consumed`, used to transmit data if t1 wins the race (and waits for t2)
import cats.effect.{Deferred, IO, Ref}
import cats.effect.std.Queue
import cats.effect.unsafe.implicits.global
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
// max times rendezvous using cats-effect's synchronous queue
def rendezvousUsingCatsEffect(): Unit =
val max = 10_000_000
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
public class Rendezvous {
private final int spinIterations;
private final int yieldIterations;
private final AtomicReference<ThreadAndCell> waiting = new AtomicReference<>();
public Rendezvous(int spinIterations, int yieldIterations) {
this.spinIterations = spinIterations;
import java.util.concurrent.Exchanger;
import java.util.concurrent.SynchronousQueue;
public class RendezvousUsingExchanger {
public static void test() throws Exception {
long startTime = System.currentTimeMillis();
final int max = 10_000_000;
Exchanger<Integer> data = new Exchanger<>();
Thread t1 = Thread.ofVirtual().start(() -> {
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
// -Djdk.virtualThreadScheduler.parallelism=1 -Djdk.virtualThreadScheduler.maxPoolSize=1 -Djdk.virtualThreadScheduler.minRunnable=1
public class Rendezvous {
private final int spinIterations;
private final int yieldIterations;
private final AtomicReference<ThreadAndCell> waiting = new AtomicReference<>();
public Rendezvous(int spinIterations, int yieldIterations) {
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
public class Rendezvous {
private final boolean yieldOnFirstIteration;
private final AtomicReference<ThreadAndCell> waiting = new AtomicReference<>();
public Rendezvous(boolean yieldOnFirstIteration) {
this.yieldOnFirstIteration = yieldOnFirstIteration;
}
@adamw
adamw / gen.scala
Created September 6, 2022 07:15
How to generate an exhaustive match for a sealed trait in a Scala 3 macro?
// TestMacro.scala
import scala.quoted.*
object TestMacro {
inline def name[E](e: E): String = ${ nameImpl[E]('e) }
def nameImpl[E: Type](e: Expr[E])(using Quotes): Expr[String] = {
import quotes.reflect.*
//> using dep com.softwaremill.ox::core:0.0.7
import ox.*
import ox.channels.*
import scala.annotation.tailrec
scoped {
// fast producer
val c = Channel[Int]()
fork {
.libv1> builtins.merge
test1: Nat -> Nat
test1 n = n + n