Skip to content

Instantly share code, notes, and snippets.

View djspiewak's full-sized avatar

Daniel Spiewak djspiewak

View GitHub Profile

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

Quick Tips for Fast Code on the JVM

I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So… here's my word of mouth.

This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea

Understanding Comparative Benchmarks

I'm going to do something that I don't normally do, which is to say I'm going to talk about comparative benchmarks. In general, I try to confine performance discussion to absolute metrics as much as possible, or comparisons to other well-defined neutral reference points. This is precisely why Cats Effect's readme mentions a comparison to a fixed thread pool, rather doing comparisons with other asynchronous runtimes like Akka or ZIO. Comparisons in general devolve very quickly into emotional marketing.

But, just once, today we're going to talk about the emotional marketing. In particular, we're going to look at Cats Effect 3 and ZIO 2. Now, for context, as of this writing ZIO 2 has released their first milestone; they have not released a final 2.0 version. This implies straight off the bat that we're comparing apples to oranges a bit, since Cats Effect 3 has been out and in production for months. However, there has been a post going around which cites various compar

@djspiewak
djspiewak / streams-tutorial.md
Created March 22, 2015 19:55
Introduction to scalaz-stream

Introduction to scalaz-stream

Every application ever written can be viewed as some sort of transformation on data. Data can come from different sources, such as a network or a file or user input or the Large Hadron Collider. It can come from many sources all at once to be merged and aggregated in interesting ways, and it can be produced into many different output sinks, such as a network or files or graphical user interfaces. You might produce your output all at once, as a big data dump at the end of the world (right before your program shuts down), or you might produce it more incrementally. Every application fits into this model.

The scalaz-stream project is an attempt to make it easy to construct, test and scale programs that fit within this model (which is to say, everything). It does this by providing an abstraction around a "stream" of data, which is really just this notion of some number of data being sequentially pulled out of some unspecified data source. On top of this abstraction, sca

java.lang.NullPointerException
java.lang.NullPointerException
java.lang.NullPointerExceptionte 46s
at java.lang.Throwable.<init>(Throwables.scala:11)
at java.lang.Exception.<init>(Throwables.scala:383)
java.lang.NullPointerException
at java.lang.Throwable.<init>(Throwables.scala:11)
java.lang.NullPointerException
at java.lang.Throwable.<init>(Throwables.scala:11)
at java.lang.Throwable.<init>(Throwables.scala:11)
diff --git a/core/src/main/scala-3 b/core/src/main/scala-3
deleted file mode 120000
index 609602e..0000000
--- a/core/src/main/scala-3
+++ /dev/null
@@ -1 +0,0 @@
-scala-2.13
\ No newline at end of file
diff --git a/core/src/main/scala/cats/mtl/Handle.scala b/core/src/main/scala/cats/mtl/Handle.scala
index e8bb1f7..e304b58 100644

Explaining Miles's Magic

Miles Sabin recently opened a pull request fixing the infamous SI-2712. First off, this is remarkable and, if merged, will make everyone's life enormously easier. This is a bug that a lot of people hit often without even realizing it, and they just assume that either they did something wrong or the compiler is broken in some weird way. It is especially common for users of scalaz or cats.

But that's not what I wanted to write about. What I want to write about is the exact semantics of Miles's fix, because it does impose some very specific assumptions about the way that type constructors work, and understanding those assumptions is the key to getting the most of it his fix.

For starters, here is the sort of thing that SI-2712 affects:

def foo[F[_], A](fa: F[A]): String = fa.toString
package me.katze
import cats.effect.*
import cats.effect.std.Dispatcher
import org.lwjgl.glfw.*
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL11.*
import org.lwjgl.opengl.{GL, GLUtil}
import org.lwjgl.system.MemoryStack.stackPush
import org.lwjgl.system.MemoryUtil.NULL
package me.katze
import cats.effect.*
import cats.effect.std.Dispatcher
import org.lwjgl.glfw.*
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL11.*
import org.lwjgl.opengl.{GL, GLUtil}
import org.lwjgl.system.MemoryStack.stackPush
import org.lwjgl.system.MemoryUtil.NULL
* thread #13
* frame #0: 0x0000000105004c58 cats-effect-tests-test`Synchronizer_acquire at Synchronizer.c:300:9
frame #1: 0x00000001050033b4 cats-effect-tests-test`Heap_Collect(heap=0x00000001085a51a0, stack=0x00000001085a5238) at Heap.c:170:10
frame #2: 0x0000000105001148 cats-effect-tests-test`Allocator_allocSlow(allocator=0x000000014c604e70, heap=0x00000001085a51a0, size=48) at Allocator.c:225:9
frame #3: 0x000000010500129c cats-effect-tests-test`Allocator_Alloc(heap=0x00000001085a51a0, size=48) at Allocator.c:251:16
frame #4: 0x0000000105004548 cats-effect-tests-test`scalanative_GC_alloc_small(info=0x00000001085a17c0, size=48) at ImmixGC.c:56:31
frame #5: 0x0000000104fa0d48 cats-effect-tests-test`M33scala.collection.immutable.Range$D5applyiiL42scala.collection.immutable.Range$ExclusiveEO(this=<unavailable>, start=0, end=0) at Range.scala:568:54
frame #6: 0x000000010474caec cats-effect-tests-test`M22scala.runtime.RichInt$D15until$extensioniiL32scala.collection.immutable.RangeEO(th