Skip to content

Instantly share code, notes, and snippets.

I want you to act as a senior {language} programmer.
The question will be a about the {language} programming language.
I want you to answer only with the fenced code block.
I want you to add an language identifier to the fenced code block.
Do not write explanations.
@mkmik
mkmik / jumphash.jsonnet
Last active October 26, 2023 08:58
Jumphash consistent hashing implementation in jsonnet
// A Fast, Minimal Memory, Consistent Hash Algorithm: https://arxiv.org/pdf/1406.2294.pdf
local MAX_INT = 281474976710656; // 48-bit integers are ok in jsonnet.
local NIBBLES = 12; // 12 hex digits in a 48-bit number.
// "random numbers should not be generated with a method chosen at random. -- D. Knuth
// And yet, due to lack of 64-bit integer arithmetic in jsonnet, this seems to work decently.
local nextRandom(n) = std.parseHex(std.slice(std.md5(std.toString(n)), 0, NIBBLES, 1));
{
@mkmik
mkmik / jumphost.k
Last active October 26, 2023 01:19
/ jumphost https://arxiv.org/pdf/1406.2294.pdf
/ usage: jh[k;n]
/ k: 64-bit key; n: num_buckets
/ Targeting the K6 and K8 dialects. Tested with ngn/k and shakti 2021.07.10
rsh:{$[x<0;1073741823+_(x-9223372036854775808)%8589934592;_x%8589934592]}
jhr:{[b;j;k;n] b:j;k:1+k*2862933555777941757;j:_(b+1)*2147483648%1+rsh[k]; $[j<n;jhr[b;j;k;n];b]}
jh:jhr[-1;0]
/ k doesn't have unsigned integers nor does have bitshifts
/ the rsh function shifts right by dividing after manually clearing the MSB
// "hide" a field with a given field name or matching a given predicate.
// Optionally, change the value of the field being mapped.
//
// This is useful until jsonnet gains the ability of using "::" in object comprehension, see
// https://github.com/google/jsonnet/issues/312#issuecomment-1580533298
local hide(obj, field=null, pred=function(name) (name == field), map=function(value) value) =
std.foldl(
function(acc, f)
if pred(f.key) then
acc { [f.key]:: map(f.value) }
{
"additionalProperties": false,
"properties": {
"apiVersion": {
"type": "string"
},
"kind": {
"type": "string"
},
"metadata": {
local to_overlay = import 'to_overlay.libsonnet';
{
a: {
b: {
c: 1,
d: self.c,
h:: 42,
},
},
} + to_overlay.toOverlay(std.parseYaml(|||
@mkmik
mkmik / tags
Created September 27, 2021 13:36
cf83a325
15644f7d
bf5d0d01
c427f848
18941fcb
155d5a5c
72646f9b
691f7836
54bf3d84
b4654140
#!/usr/bin/perl -w
#
# stackcollapse-perf.pl collapse perf samples into single lines.
#
# Parses a list of multiline stacks generated by "perf script", and
# outputs a semicolon separated stack followed by a space and a count.
# If memory addresses (+0xd) are present, they are stripped, and resulting
# identical stacks are colased with their counts summed.
#
# USAGE: ./stackcollapse-perf.pl [options] infile > outfile
@mkmik
mkmik / main.rs
Last active April 21, 2021 15:14
#![allow(dead_code)]
use rendezvous_hash::RendezvousNodes;
use rendezvous_hash::{Capacity, WeightedNode};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher};
const JUMP: u64 = 1 << 31;
/// Takes a 64 bit key and the number of buckets, outputs a bucket number `0..buckets`.
#![allow(dead_code)]
use rendezvous_hash::RendezvousNodes;
use rendezvous_hash::{Capacity, WeightedNode};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::BuildHasherDefault;
type MyBuildHasher = BuildHasherDefault<DefaultHasher>;
fn main() {