Thread pools on the JVM should usually be divided into the following three categories:
- CPU-bound
- Blocking IO
- Non-blocking IO polling
Each of these categories has a different optimal configuration and usage pattern.
This note outlines a principled way to meta-programming in Scala. It tries to combine the best ideas from LMS and Scala macros in a minimalistic design.
LMS: Types matter. Inputs, outputs and transformations should all be statically typed.
Macros: Quotations are ultimately more easy to deal with than implicit-based type-lifting
LMS: Some of the most interesting and powerful applications of meta-programming
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
There exist several DI frameworks / libraries
in the Scala
ecosystem. But the more functional code you write the more you'll realize there's no need to use any of them.
A few of the most claimed benefits are the following:
// Based on https://github.com/http4s/contrib/blob/master/aws/src/main/scala/org/http4s/contrib/aws/AwsSigner.scala | |
import java.util.Date | |
import cats.data.Kleisli | |
import cats.effect.{Effect, Sync} | |
import cats.implicits._ | |
import fs2.Stream | |
import org.http4s.client.Client | |
import org.http4s.{Header, Request} |
package teikametrics | |
import scala.collection.SortedMap | |
/** | |
* Immutable implementation of an LRU cache. | |
* | |
* @author Twitter | |
* | |
* Copy pasted from the version previously in twitter-util v19.1.0 at |
package teikametrics | |
import java.nio.charset.{Charset, StandardCharsets} | |
import akka.util.ByteString | |
import fs2.{Chunk, Pipe, RaiseThrowable, Stream} | |
import teikametrics.ByteEncoding._ | |
/** Streamed bytes claiming to be encoded under `BE` | |
* | |
* @param chunkSize A chunk size to use for streaming operations, in order to use a consistent value and minimize array copying |
cats-effect Resource
is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:
trait Resource[F[_], A] {
/** - Acquire resource
* - Run f
* - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
import java.sql.Types | |
import scala.annotation.tailrec | |
import scala.concurrent.Await | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration.Duration | |
import scala.meta._ | |
import slick.dbio.DBIO | |
import slick.jdbc.meta.{MColumn, MQName, MTable} |