Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Josh joshlemer

🏠
Working from home
  • Winnipeg, Canada
View GitHub Profile
View gen.clj
(ns gen
(:require
[cloroutine.core :refer [cr]]
[criterium.core :as crit]))
(deftype ^:private GenIter [;; The Cloroutine generator function
^:unsynchronized-mutable ^clojure.lang.IFn g
;; Trilean boolean logic:
;; 0 = certainly no more elements, `x` is nil
;; 1 = certainly there are more elemennts, and the next object is stored in `x`
View csp.js
// example 1: Channels
(async () => {
// make an unbuffered channel
const c = chan();
// in separate 'thread', push 3 values into it
(async () => {
send(c, 'ping');
@joshlemer
joshlemer / Bench.txt
Last active Jan 18, 2020
LHM benches
View Bench.txt
LEGEND:
* Suffixes: (Lhm = LinkedHashMap, Vm = VectorMap)
*
[info] Benchmark (size) Mode Cnt Score Error Units
[info] VectorMapBenchmark.buildLhm 5 avgt 20 399.458 ± 4.206 ns/op
[info] VectorMapBenchmark.buildLhm 10 avgt 20 764.065 ± 10.401 ns/op
@joshlemer
joshlemer / List.scala
Created Jan 4, 2020
List opt -- from vs prependedAll
View List.scala
// class List
override def prependedAll[B >: A](prefix: collection.IterableOnce[B]): List[B] = prefix match {
case xs: List[B] => xs ::: this
case _ =>
val iter = prefix.iterator
if (iter.hasNext) {
val result = new ::[B](iter.next(), null)
var curr = result
while (iter.hasNext) {
val temp = new ::[B](iter.next(), null)
@joshlemer
joshlemer / 2.13.x
Created Jan 1, 2020
List Optimization Benchmarks
View 2.13.x
[info] Benchmark (size) Mode Cnt Score Error Units
[info] ListBenchmark.colonColonColon 0 avgt 20 2.576 ± 0.154 ns/op
[info] ListBenchmark.colonColonColon:·gc.alloc.rate 0 avgt 20 0.001 ± 0.002 MB/sec
[info] ListBenchmark.colonColonColon:·gc.alloc.rate.norm 0 avgt 2010⁻⁵ B/op
[info] ListBenchmark.colonColonColon:·gc.count 0 avgt 200 counts
[info] ListBenchmark.colonColonColon 1 avgt 20 15.884 ± 1.988 ns/op
[info] ListBenchmark.colonColonColon:·gc.alloc.rate 1 avgt 20 1622.694 ± 155.062 MB/sec
[info] ListBenchmark.colonColonColon:·gc.alloc.rate.norm 1 avgt 20 40.000 ± 0.001 B/op
[info] ListBenchmark.colonColonColon:·gc.churn.PS_Eden_S
View UnsafeWrap.scala
package com.github.joshlemer
import scala.collection.Factory
import scala.collection.immutable._
package object clxn {
implicit final class IterableObjectExtension(private val c: Iterable.type) extends AnyVal {
def unsafeWrap[A](it: collection.Iterable[A]): Iterable[A] = it match {
case xs: Iterable[A] => xs
View Ex.scala
calculateFast(file: File, size: Int, hashes: Iterator[ByteString], bitfield: BitVector) = {
// zipping iterators together will mean that each element will allocate an additional instance of `Tuple2[Left, Right]`
// you could get by with iterating through `file.bytes.grouped(size)` and `hashes` at the same time, avoiding
// tuple allocations
val iter = file.bytes.grouped(size).zip(hashes)
val digest: MessageDigest = MessageDigest.getInstance("SHA-1")
val buf = new Array[Byte](size)
var bufIdx = 0
View Nel.scala
import scala.collection.mutable
import scala.collection.immutable.{AbstractSeq, LinearSeq, LinearSeqOps, StrictOptimizedSeqOps}
final class Nel[+A](override val head: A, override val tail: List[A])
extends AbstractSeq[A]
with LinearSeq[A]
with LinearSeqOps[A, LinearSeq, LinearSeq[A]]
with StrictOptimizedSeqOps[A, LinearSeq, LinearSeq[A]] {
override def toList: List[A] = head :: tail
View DMap.scala
package dmap
import scala.reflect.runtime.universe._
import DMap._
class DMap private(underlying: Map[Any, Entry]) {
def get[T](key: Any)(implicit tt: TypeTag[T]): Option[T] = {
underlying.get(key) match {