Operation | Input | Result | Notes |
---|---|---|---|
map | F[A] , A => B |
F[B] |
Functor |
apply | F[A] , F[A => B] |
F[B] |
Applicative |
(fa, fb, ...).mapN | (F[A], F[B], ...) , (A, B, ...) => C |
F[C] |
Applicative |
(fa, fb, ...).tupled | (F[A], F[B], ...) |
F[(A, B, ...)] |
Applicative |
flatMap | F[A] , A => F[B] |
F[B] |
Monad |
traverse | F[A] , A => G[B] |
G[F[A]] |
Traversable; fa.traverse(f) == fa.map(f).sequence ; "foreach with effects" |
sequence | F[G[A]] |
G[F[A]] |
Same as fga.traverse(identity) |
attempt | F[A] |
F[Either[E, A]] |
Given ApplicativeError[F, E] |
-- first query all the users | |
WITH offsets AS (SELECT a.*, | |
EXTRACT(hour FROM ptn.utc_offset) AS utc_offset | |
FROM bootcamp.attendees a | |
JOIN pg_timezone_names ptn ON a.timezone = ptn.name | |
WHERE a.bootcamp_version = 3 | |
AND a.timezone IS NOT NULL | |
AND a.content_delivery = 'Live'::text | |
), | |
-- then aggregate the users by track and offset, we want matching timezones to fill up first |
import shapeless._ | |
import shapeless.ops.record.Keys | |
import shapeless.ops.hlist.Selector | |
import shapeless.tag._ | |
/** | |
* Gets the name of a case class's field, in a type-safe way. | |
* If the class does not have a field with the given name, the program won't compile. | |
* | |
* Kinda similar to C#'s `nameof` operator, but not bolted onto the language |
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
See my working application (and additional notes) here:
https://github.com/integralist/simple-rpm
Other information that led to the above repository, can be found below
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
import scala.concurrent.{Await, Future} | |
val futures: List[Future[List[Int]]] = | |
List.fill(10)(Future { | |
Thread.sleep(100) | |
List(1, 2, 3) | |
}) |
/** | |
* Let's say I can read things from a "Store" | |
*/ | |
trait Store[F] { | |
def read(path: Path): F[String] | |
} | |
/** | |
* Now I want to read more specific things, like a Configuration | |
* |
# Assuming an Ubuntu Docker image | |
$ docker run -it <image> /bin/bash |
Latency Comparison Numbers (~2012) | |
---------------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |