Skip to content

Instantly share code, notes, and snippets.

View rklaehn's full-sized avatar

Rüdiger Klaehn rklaehn

  • Independent hacker
  • Transylvania, Romania
View GitHub Profile
@rklaehn
rklaehn / ipfs-publish-remote.sh
Last active July 23, 2019 19:44
Publishing to ipfs/ipns by key
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "usage:"
echo " ipfs-publish-remote.sh <keyname> <dir> <host>"
echo " example: ipfs-publish-remote.sh keyname output/rootdir user@ipfs.company.net"
exit 1
fi
KEYNAME=$1
DIR=$2
HOST=$3
@rklaehn
rklaehn / match.ts
Last active February 23, 2018 21:14
type Protocol = {
foo: { x: string };
bar: { y: number };
}
type Fold<T, U> = {
readonly [P in keyof T]: (value: T[P]) => U
}
type WithTag<T> = {
[TKey in keyof T]: { type: TKey } & T[TKey]
}
const enum Test { a = 'a' }
type Protocol = {
foo: { x: string };
bar: { y: number };
}
type WithTag<T> = {
[TKey in keyof T]: { type: TKey } & T[TKey]
}
type ToDiscriminatedUnion<P> = WithTag<P>[keyof P]
type Constructor<T> = {
@rklaehn
rklaehn / Results
Last active October 8, 2018 10:14
CBOR encoding performance
$ node benchmark2.js
= Encoding Time (ms) =
msgp.encode 9172.339491000399
borc.encode 20400.477496000007
json.stringify 9445.365290999413
= Decoding Time (ms) =
msgp.decode 121.99922000057995
borc.decodeFirst 383.22095499932766
json.parse 48.20755399949849
let msgpackLite = require('msgpack-lite');
let borc = require('borc');
let _ = require('lodash');
let { performance } = require('perf_hooks')
let arrays = _.times(100, (n) => {
return _.times(n, i => ({ i, a: [String(i * 1234), String(i * 1234), String(i * 1234), String(i * 1234)] }))
});
let encodedBORC = arrays.map(array => borc.encode(array));
@rklaehn
rklaehn / merge_sorted.rs
Last active October 30, 2019 08:32
Merge 2 sorted iterators
use std::iter::{Iterator, Peekable};
pub struct MergeSorted<A: Iterator, B: Iterator> {
a: Peekable<A>,
b: Peekable<B>,
}
impl<A: Iterator, B: Iterator> MergeSorted<A, B> {
fn new(a: A, b: B) -> MergeSorted<A, B> {
MergeSorted {
@rklaehn
rklaehn / code.rs
Last active March 4, 2020 13:22
How the select! macro expands
use futures::{future::FutureExt, select};
async fn a() {}
async fn b() {}
async fn test() {
select! {
a = a().fuse() => println!("a"),
b = b().fuse() => println!("b"),
@rklaehn
rklaehn / fut.rs
Created July 17, 2020 09:31
stream life extension hack
use futures::prelude::*;
use std::{rc::Rc, task::{Context, Poll}, cell::RefCell, pin::Pin};
fn foo<'a>(c: &'a mut u64) -> impl Stream<Item = u64> + 'a {
stream::unfold(10u64, move |max| {
future::ready(if *c < max {
*c += 1;
Some((*c, max))
} else {
None
Jul 24 13:25:22.534 DEBUG libp2p_gossipsub::behaviour: Subscribing to topic: discovery
Jul 24 13:25:22.534 DEBUG libp2p_gossipsub::behaviour: Running JOIN for topic: TopicHash { hash: "discovery" }
Jul 24 13:25:22.534 DEBUG libp2p_gossipsub::behaviour: RANDOM PEERS: Got 0 peers
Jul 24 13:25:22.534 DEBUG libp2p_gossipsub::behaviour: JOIN: Inserting 0 random peers into the mesh
Jul 24 13:25:22.535 DEBUG libp2p_gossipsub::behaviour: Completed JOIN for topic: TopicHash { hash: "discovery" }
Jul 24 13:25:22.535 INFO libp2p_gossipsub::behaviour: Subscribed to topic: discovery
Jul 24 13:25:22.537 DEBUG libp2p_gossipsub::behaviour: Subscribing to topic: /klk/prod/2020-01-09
Jul 24 13:25:22.537 DEBUG libp2p_gossipsub::behaviour: Running JOIN for topic: TopicHash { hash: "/klk/prod/2020-01-09" }
Jul 24 13:25:22.537 DEBUG libp2p_gossipsub::behaviour: RANDOM PEERS: Got 0 peers
Jul 24 13:25:22.537 DEBUG libp2p_gossipsub::behaviour: JOIN: Inserting 0 random peers into the mesh
/// This abomination only works if the underlying bytes are known in number (Sized),
/// can be moved around in memory (Unpin) and do no hold on to other things than the
/// underlying bytes ('static).
#[cfg(feature = "dataflow")]
impl<T: Abomonation + 'static + Sized + Unpin> Abomonation for ArcVal<T> {
unsafe fn entomb<W: std::io::Write>(&self, write: &mut W) -> std::io::Result<()> {
/* Since the value T has not yet been seen by abomonate (it is behind a pointer)
* we need to fully encode it.
*/
abomonation::encode(self.deref(), write)