Skip to content

Instantly share code, notes, and snippets.

Avatar

Pierre Jambet pjambet

View GitHub Profile
@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
View int_set.rb
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 Nov 3, 2020
Figuring out if a Float/BigDecimal is an integer
View bench.rb
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 }
View gist:9a8615ec3e14381b161693dc90d26cb6
irb(main):138:0> [256].pack('S') > [15].pack('S')
=> false
irb(main):139:0> [256].pack('S>') > [15].pack('S>')
=> true
View benchmark.rb
require 'benchmark'
require 'securerandom'
require_relative './dict'
RANDOM_BYTES = SecureRandom.bytes(16)
class MyHash
def initialize(pairs)
if pairs.length == 1
View bench.rb
require 'benchmark'
require 'securerandom'
require_relative './dict'
RANDOM_BYTES = SecureRandom.bytes(16)
def setup_dict(size)
dict = BYORedis::Dict.new
size.times { |i| dict[i.to_s] = i.to_s }
View client.rb
require 'socket'
socket = TCPSocket.new 'localhost', 2000
socket.write "request"
socket.close
View gist:718599bc26c2d743afb1d02655344a08
require 'socket'
# EPIPE, when the socket is closed by the time we try to read, we can read, but writing fails
# ECONNRESET, when the socket is closed by the time we try to write
def process_events(event)
p 'in proccess'
res = event.read_nonblock(1024, exception: false) # => YET THE FUCKING STACKTRACE POINTS HERE WHEN ECONNRESET
p res
event.puts "FUCK YOU puts" # => EPIPE
# p '==--=='
@pjambet
pjambet / notes.md
Created Aug 27, 2020
Notes about Redis
View notes.md

Notes

  • dbDictType used as argument to dictCreate in server.c (initServer)
  • Uses dictSdsHash, dictSdsKeyCompare, dictSdsKeyDestructor, dictObjectDestructor
  • dict struct:
    • dictType
    • privdata
    • ht (2 of them)
    • rehashidx
  • iterators