Skip to content

Instantly share code, notes, and snippets.

View funny-falcon's full-sized avatar

Sokolov Yura funny-falcon

View GitHub Profile
@funny-falcon
funny-falcon / gist:3347272
Created August 14, 2012 07:47
Simple int32 hash with relatively good avalance
/* it has avalance ~ 33.3 measured by http://baagoe.org/en/w/index.php/Avalanche_on_integer_hash_functions */
/* it is not the best, but i think it is good enough */
uint32_t hash(uint32_t n) {
n ^= n >> 16;
n *= 0xd5b25599;
return n + (n >> 16);
}
@funny-falcon
funny-falcon / java.java
Created August 13, 2012 15:12
Consistent hash from Google Guava Core libraries
/**
* Assigns to {@code input} a "bucket" in the range {@code [0, buckets)}, in a uniform
* manner that minimizes the need for remapping as {@code buckets} grows. That is,
* {@code consistentHash(h, n)} equals:
*
* <ul>
* <li>{@code n - 1}, with approximate probability {@code 1/n}
* <li>{@code consistentHash(h, n - 1)}, otherwise (probability {@code 1 - 1/n})
* </ul>
*
@funny-falcon
funny-falcon / a copyright
Created July 31, 2012 09:52
Writing a minimalistic web-server using event machine
@funny-falcon
funny-falcon / test_finalizer.rb
Created July 4, 2012 06:21
Test different kind of finalizers
#GC.stress = true
STDOUT.sync = true
$stdout.sync = true
KIND = ARGV[0] || 'inner'
def finalizer(i)
proc{|id| puts "HI #{i} #{id}"}
end
@funny-falcon
funny-falcon / test 1.9.2
Created June 28, 2012 08:19
Object finalizer broken in ruby 1.9.3
$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-linux]
$ # Ruby 1.9.2 fails to run finalizer until program exit
$ ruby test_finalizer.rb outter
assigned
deassigned
stop
HI 9472020
$ ruby test_finalizer.rb inner
assigned
@funny-falcon
funny-falcon / ary-queue.diff
Created June 24, 2012 06:26
Test patch for increasing "array as queue" speed
diff --git a/array.c b/array.c
index 64647c3..76fac8a 100644
--- a/array.c
+++ b/array.c
@@ -255,15 +255,24 @@ rb_ary_modify(VALUE ary)
rb_ary_modify_check(ary);
if (ARY_SHARED_P(ary)) {
long len = RARRAY_LEN(ary);
+ VALUE shared = ARY_SHARED(ary);
if (len <= RARRAY_EMBED_LEN_MAX) {
@funny-falcon
funny-falcon / latency.txt
Created June 1, 2012 11:10 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 1 MB sequentially from memory 250,000 ns 0.25 ms
Round trip within same datacenter 500,000 ns 0.5 ms
Read 1 MB sequentially from SSD 1,000,000 ns 1 ms 4X memory
@funny-falcon
funny-falcon / test.rb
Created June 1, 2012 09:16
Script for testing kgio_*writev
require 'kgio'
ip = '127.0.0.1' # better write here real IP address of your network card
prc = fork do
srv = Kgio::TCPServer.new(ip, 8000)
sock = srv.kgio_accept(Kgio::TCPSocket)
count = 0
b = ''
while s = sock.kgio_read(1024*1024, b)
@funny-falcon
funny-falcon / em_patch.rb
Created May 28, 2012 12:21
EM finer grained deffered callbacks
def EM.run_deferred_callbacks
until (@resultqueue ||= []).empty?
result,cback = @resultqueue.pop
cback.call result if cback
end
# Capture the size at the start of this tick...
time = Time.now
size = @next_tick_queue.size
cur_ticks = @next_tick_mutex.synchronize{ @next_tick_queue.slice!(0, size) }
@funny-falcon
funny-falcon / fiber_pool.rb
Created May 24, 2012 10:03
Simple fiber pool without extra possibilities
# simple fiber pool for EM::Synchrony (or same)
class FiberPool
def initialize(count)
@queue = EM::Queue.new
@count = count
count.times do
fib = Fiber.new do
fiber_loop(fib)
end
fib.resume