Skip to content

Instantly share code, notes, and snippets.

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

@mandubian
mandubian / cat_functor_rec.scala
Last active February 14, 2017 20:24
Implementation of Kind-Polymorphism Category, Functor & Rec for generic (un)folding
// WARNING... NOT FOR THE FAINT HEARTED
// Scala Conversion of haskell code in http://blog.functorial.com/posts/2012-02-02-Polykinded-Folds.html
// to study more samples of kind polymorphism in Scala
//
// It provides the implementation of Kind-Polymorphism Category, Functor & Rec for generic folding
// HASKELL CODE
// class Category hom where
// ident :: hom a a
// compose :: hom a b -> hom b c -> hom a c
@mandubian
mandubian / typeable.scala
Created December 31, 2016 14:09
Draft implementation of Kind-Polymorphic Typeclass for type-safe type representation & cast operation
import scala.language.higherKinds
import scala.reflect.ClassTag
import scala.collection.{ GenMap, GenTraversable }
/** Draft implmementation of Polykinded Safe Type Representation & Cast */
object Test extends App {
/** Representation of the type of a value
* T is the type constructor of the type of the value (For example Int => T = Int, List[Int] => T = List)
* TAbs (for T abstracted) is a well-formed monomorphic type derived from T (For example List => TAbs = List[Any])

Actor Queue Notes

The first thing to understand is that the head field inside of scalaz.concurrent.Actor is not the "head" of the message queue in any traditional sense of the word. A better description would be "last". The there are no pointers to the head of the queue, which one of the very clever things about this implementation.

Empty Actor

Consider the case where the actor has no outstanding messages. This new message will go into the following code:

 def !(a: A): Unit = {
@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

@joelkuiper
joelkuiper / TextHighlight
Last active October 21, 2020 07:25
A example class for adding highlights to PDFs based on a Pattern or String
/*
* Copyright 2014 Joël Kuiper
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@joelkuiper
joelkuiper / gist:9eb52555e02edb653dcf
Last active March 22, 2021 12:58
highlight a pattern with PDFBox
The PDFTextAnnotator will accept a PDF and a pattern, it will highlight all occurances of that pattern in the document.
It inherits from the PDFTextStripper (so things like start end end page should still be configurable)
See the App file for a basic usage example