Skip to content

Instantly share code, notes, and snippets.

View pjambet's full-sized avatar

Pierre Jambet pjambet

View GitHub Profile
var Benchmark = require('benchmark');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var set = new Set();
var array = [];
#include <stdio.h>
#include <stdint.h>
int main() {
long long l = -2;
printf("%lld\n", l);
printf("%llu\n", (unsigned long long) l);
printf("%llu\n", (uint64_t) l);
return 0;
}
@pjambet
pjambet / mermaid
Created December 8, 2020 17:19
A sequence diagram
sequenceDiagram
participant Frontend
participant Backend
Frontend->>+Frontend: Persist intent record
Frontend->>+Backend: Create resource request
Backend-->>-Frontend: Newly created resource
Note right of Backend: Resource must include a unique identifier
Frontend->>+Frontend: Update intent record with the unique identifier
@pjambet
pjambet / select.rs
Created November 22, 2020 22:02
select/pselect in Rust
extern crate libc;
use std::{io,mem,ptr,time};
use std::os::unix::io::RawFd;
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
// use std::io::Read;
pub struct FdSet(libc::fd_set);
@pjambet
pjambet / string_cmd.md
Last active November 17, 2020 20:29
String commands, without bit stuff
  • APPEND
  • DECR
  • DECRBY
  • GET
  • GETRANGE
  • GETSET
  • INCR
  • INCRBY
  • INCRBYFLOAT
  • MGET
@pjambet
pjambet / popcount.c
Created November 12, 2020 13:37
Playing with the C code from Redis in bitops.c
#include <stdio.h>
#include <inttypes.h>
size_t redisPopcount(void *s, long count) {
size_t bits = 0;
unsigned char *p = s;
uint32_t *p4;
static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
/* Count initial bytes not aligned to 32 bit. */
@pjambet
pjambet / sorted_array.rb
Created November 11, 2020 20:53
A contrived sorted array
require 'forwardable'
module BYORedis
class SortedArray
extend Forwardable
def_delegators :@underlying, :[], :delete_if, :size, :each, :delete_at, :shift,
:bsearch_index, :map, :each_with_index, :pop
def initialize(*fields)
module BYORedis
class SkipList
MAX_LEVEL = 32 # 32 is what Redis uses
P = 0.25
Node = Struct.new(:member, :score, :backward, :levels, keyword_init: true)
Level = Struct.new(:forward, :span, keyword_init: true)
attr_reader :length
module BYORedis
class IntSet
INT16_MIN = -2**15 # -32,768
INT16_MAX = 2**15 - 1 # 32,767
INT32_MIN = -2**31 # -2,147,483,648
INT32_MAX = 2**31 - 1 # 2,147,483,647
INT64_MIN = -2**63 # -9,223,372,036,854,775,808
INT64_MAX = 2**63 - 1 # 9,223,372,036,854,775,807
@pjambet
pjambet / bench.rb
Created November 3, 2020 16:36
Figuring out if a Float/BigDecimal is an integer
require 'benchmark/ips'
require 'bigdecimal'
Benchmark.ips do |x|
x.report('BD: % 1') { num = BigDecimal(rand(1_000_000_000)); num % 1 == 0 }
x.report('BD: == truncate') { num = BigDecimal(rand(1_000_000_000)); num.truncate == num }
x.report('BD: frac == 0') { num = BigDecimal(rand(1_000_000_000)); num.frac == 0 }
x.report('Float: % 1') { num = rand(1_000_000_000).to_f; num % 1 == 0 }