This has been moved to https://marcriera.github.io/ddgo-controller-docs/. Please update your bookmarks.
// What output does this yield? What should it? (try with either 2.12.x or 2.13.x) | |
// (spoiler: https://github.com/scala/bug/issues/10788) | |
object Test { | |
class Box[T](t: T) { | |
def foo: T = { | |
println("effect") | |
t | |
} | |
} |
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
TL;DR - Use fs2.time.sleep_[Task](delay) ++ Stream.eval(effect)
instead of Stream.eval(effect.schedule(delay))
.
FS2 never interrupts evaluation of an effect. This can lead to surprising behavior when using the schedule
method on Task
. Consider this test driver:
def testInterruption[A](effect: Stream[Task, A]): Stream[Task, A] = {
val logStart = Stream.eval_(Task.delay(println("Started: " + System.currentTimeMillis)))
val logFinished = Stream.eval_(Task.delay(println("Finished: " + System.currentTimeMillis)))
val interruptSoonAfterStart =
Stream.eval(async.signalOf[Task,Boolean](false)).flatMap { cancellationSignal =>
Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.
A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.
val square : Int => Int = x => x * x
package p | |
/** Super basic map/flatMap fusion ADT. | |
*/ | |
sealed trait View[A] { | |
def map[B](f: A => B): View[B] = CView(this, Mapped(f)) | |
def flatMap[B](f: A => View[B]): View[B] = CView(this, FlatMap(f)) | |
def foreach(f: A => Unit): Unit = this match { | |
case IdView(xs) => xs foreach f |
case class Refrigerated[T](thing: T) // this is just something holding things that have been refrigerated. | |
trait Refrigeratable[T] { // this is the typeclass | |
def refrigerate(thing: T): Refrigerated[T] | |
} | |
object Refrigerable { | |
@inline // this is just an annotation telling the JVM to inline this, reducing the amount of indirection occuring. | |
def apply[T](refrigerateMethod: T => Refrigerated[T]): Refrigerable[T] = | |
new Refrigerable { | |
def refrigerate(thing: T): Refrigerated[T] = refrigerateMethod(thing) |
object TypeclasseDemo { | |
// The parts of the type class pattern are: | |
// | |
// 1. the "type class" itself -- a trait with a single type parameter; | |
// | |
// 2. type class "instances" for each type we care about, | |
// each marked with the `implicit` keyword; | |
// | |
// 3. an "interface" to the type class -- one or more methods |
object MagnetExample extends App { | |
sealed trait AdditionMagnet { | |
type Result | |
def apply():Result | |
} | |
object AdditionMagnet { |
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import rapture.core._ | |
import rapture.core._ | |
scala> val str: String = alloc() // new String() | |
str: String = "" |