Skip to content

Instantly share code, notes, and snippets.

View guizmaii's full-sized avatar
🏄‍♂️

Jules Ivanic guizmaii

🏄‍♂️
View GitHub Profile
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import scala.util.Random
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)

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

@guizmaii
guizmaii / empty_cognito_user_pool.sc
Last active September 29, 2017 15:02
Ammonite (http://ammonite.io/) script to remove all identities in a AWS Cognito Identity Pool
#!/usr/local/bin/amm
import sys.process._
import collection.mutable
import ammonite.ops._
import ammonite.ops.ImplicitWd._
import $ivy.`com.amazonaws:aws-java-sdk:1.11.204`
import com.amazonaws.services.cognitoidp._
@ferhtaydn
ferhtaydn / nel_serialize.md
Last active July 25, 2022 14:20
How to serialize a model class has NonEmptyList attributes like normal List

How to use NonEmptyList in a model class

Let's say you are implementing an API and decide to return a base error response in common error situations such as BadRequest, NotFound etc.

final case class Error(code: String, description: String)

If you are returning an error response, your assumption would probably be that such responses should contain at least one error.

@nmagee
nmagee / retrieve-ec2-instance-types.sh
Last active November 25, 2019 12:26
Query the AWS Pricing API to get all currently available EC2 instance types
#!/bin/bash
curl https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.json | jq -r '.products[].attributes["instanceType"]' | sort -u | grep '\.'
@atamborrino
atamborrino / ExecutionContextMonitor.scala
Last active April 14, 2022 09:10
Monitor Scala's ExecutionContext / Akka Dispatcher lag (number of tasks in waiting queues)
import java.util.concurrent._
import akka.dispatch.{Dispatcher, ExecutorServiceDelegate}
import config.Config
import helpers.ScalaLogger
class ExecutionContextMonitor()(implicit metricsService: MetricsClient, config: Config) {
private val log = ScalaLogger.get(this.getClass)
private val scheduler = Executors.newSingleThreadScheduledExecutor()
// How to install it:
// ------------------
//
// 1. Copy this function into your .zshrc or .bashrc file
// 2. Done!
//
// How to use it:
// --------------
//
// You want to merge `develop` branch in your `my_functionnality` branch.
import cats.instances.future._
import cats.instances.option._
import cats.syntax.traverse._
import cats.syntax.cartesian._
import scala.concurrent.Future
trait Guizmaii {
import scala.concurrent.ExecutionContext.Implicits.global

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA