Skip to content

Instantly share code, notes, and snippets.

Avatar

Pierre Jambet pjambet

View GitHub Profile
View stateful.go
package main
import (
"fmt"
"math/rand"
// "sync/atomic"
"bufio"
"net"
// "strconv"
View bench-large.rb
require 'benchmark/ips'
require 'set'
def bench(size)
Benchmark.ips do |x|
set = Set.new
array = []
hash = {}
size.times do |i|
View bench-large.js
var Benchmark = require('benchmark');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var set = new Set();
var array = [];
View overflow.c
#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 Dec 8, 2020
A sequence diagram
View mermaid
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 Nov 22, 2020
select/pselect in Rust
View select.rs
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 Nov 17, 2020
String commands, without bit stuff
View string_cmd.md
  • APPEND
  • DECR
  • DECRBY
  • GET
  • GETRANGE
  • GETSET
  • INCR
  • INCRBY
  • INCRBYFLOAT
  • MGET
@pjambet
pjambet / popcount.c
Created Nov 12, 2020
Playing with the C code from Redis in bitops.c
View popcount.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 Nov 11, 2020
A contrived sorted array
View sorted_array.rb
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)
View skiplist.rb
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