Skip to content

Instantly share code, notes, and snippets.

View Ichoran's full-sized avatar
💭
Ichoran is diffracting imperceptibly (more, with JPEG artifacts)

Ichoran

💭
Ichoran is diffracting imperceptibly (more, with JPEG artifacts)
View GitHub Profile
@Ichoran
Ichoran / ThreadLocalLoom.scala
Created October 16, 2024 06:29
Benchmark to test whether ThreadLocal creation (via DynamicVariable) really impacts Loom threads
//> using scala 3.5.0
//> using dep com.github.ichoran::kse3-basics:0.3.11
//> using dep com.github.ichoran::kse3-flow:0.3.11
// Run with scala-cli --power --jmh --jvm=21 ThreadLocalLoom.scala
// If you change the classes that have to be benchmarked, you may have to rm -r .scala_build
package threadlocalloom.bench
@Ichoran
Ichoran / ParallelGzipOutputStream.scala
Created October 9, 2024 06:25
A reimplementation of parallel gzip to use Java threads directly, since there seems to be some weird bug on JVM 8 that impacts the original Scala Future version
// Begin Gist Information
// Original code by Stefan Zieger (see https://github.com/szeiger/zinc/blob/1d296b2fbeaae1cf14e4c00db0bbc2203f9783a4/internal/zinc-persist/src/main/scala/sbt/internal/inc/consistent/ParallelGzipOutputStream.scala)
// Modified by Rex Kerr to use Java threads directly rather than Future
// Probably Apache Commons License? Whatever the original one was is fine.
// NOT HEAVILY TESTED! (I did verify binary-identical results on one test data file.)
// End Gist Information
import java.io.{ ByteArrayOutputStream, FilterOutputStream, OutputStream }
import java.util.zip.{ CRC32, Deflater, DeflaterOutputStream }
@Ichoran
Ichoran / BenchHashVsAnyRef.scala
Created September 2, 2024 22:58
Benchmarking of Scala collections' mutable.HashMap vs mutable.AnyRefMap
//> using scala 3.5.0
// Run with scala-cli --power --jmh --jvm=21 BenchHashVsAnyRef.scala
// If you change the classes that have to be benchmarked, you may have to rm -r .scala_build
package bhvar
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
@Ichoran
Ichoran / MutexBench.scala
Created August 8, 2024 16:52
Benchmarking Cats Effect 3 mutex vs. Java Loom with java.util.concurrent.Semaphore
/*
* Copyright 2020-2024 Typelevel
*
* 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
@Ichoran
Ichoran / Simple.scala plus bytecode
Created February 18, 2023 00:48
Early return converted to break
class Simple {
import scala.util.boundary
import kse.flow.{given, _}
def exists[T](xs: Iterable[T], fn: T => Boolean) =
boundary:
iFor(xs.iterator): (x, _) =>
if fn(x) then boundary.break(true)
false
}
@Ichoran
Ichoran / Bench.scala
Created June 12, 2018 22:06
Thyme benchmark of tap and pipe vs manual inlining
package bench
import scala.language.implicitConversions
import ichi.bench._
object Implicits {
final class ChainingOps[A](private val self: A) extends AnyVal {
@inline def tap[U](f: A => U): A = { f(self); self }
@inline def pipe[B](f: A => B): B = f(self)
@Ichoran
Ichoran / Weighted_Sampling_No_Replace.scala
Created June 10, 2018 00:46
Sketch of a reasonably efficient algorithm for weighted sampling without replacement
def cuml(wt: Array[Double]) = {
val l = wt.length
val base = if ((l & (l-1)) == 0) l else java.lang.Integer.highestOneBit(l)*2
val tree = new Array[Double](base*2)
System.arraycopy(wt, 0, tree, 0, wt.length)
var in = 0
var out = base
var n = base
while (in + 1 < out) {
while (in + 1 < n) {
@Ichoran
Ichoran / Test_Imm_List_Str.scala
Created October 20, 2017 03:48
Example of what generated code looks like with collection-laws rewrite
// Autogenerated test for collection List with element type String
package laws
class Test_Imm_List_Str(numb: Numbers, oper: Ops[String, Option[String]], inst: Instance[String, scala.collection.immutable.List[String]], lln: Int)
extends StrTest[scala.collection.immutable.List[String], Test_Imm_List_Str](numb, oper, inst, lln) {
import Test.ComparesTo
import Test.Logic
def renumber(numb: Numbers) = new Test_Imm_List_Str(numb, ops, instance, lawLine)
def reinstance(inst: Instance[String, scala.collection.immutable.List[String]]) = new Test_Imm_List_Str(num, ops, inst, lawLine)
@Ichoran
Ichoran / openworm_wcon_iwm_2017.md
Created March 16, 2017 16:40
Abstract of IWM 2017 for OpenWorm WCON project

A Common Format for Worm Tracking Data

Michael Currie (1a), Rex A. Kerr (2), Chee Wai Lee (1b), Jim Hokanson(1c), and Andrew E.X. Brown (3).

  1. OpenWorm (a. Somewhere; b. Somewhere Else; c. Somewhere Yet Again)
  2. Calico Life Sciences
  3. Imperial College London

Many labs, including ours, have built a wide variety of worm trackers.

@Ichoran
Ichoran / Ranged.scala
Created January 25, 2017 04:42
Quick and dirty benchmark of different approaches to Range specialization
package test
class Super[+A] {
def foreach[U](f: A => U): Unit = ???
}
final class Ranged(i0: Int, iN: Int) extends Super[Int] {
override def foreach[@specialized(Unit) U](f: Int => U) {
var i = i0
while (i < iN) {