Skip to content

Instantly share code, notes, and snippets.

View luikore's full-sized avatar

luikore luikore

View GitHub Profile
@luikore
luikore / compare.c
Created July 13, 2014 10:33
pool alloc is about 2.5 times faster and smaller
/*
benchmark with gtime -f "%M"
pool alloc:
time: 0.028571
22331392
malloc:
@luikore
luikore / readme.md
Last active August 29, 2015 14:01
repeatable reads in mysql and pg

Mysql doesn't auto lock or version lock records when select in repeatable read transactions. While PG can forbid concurrent updates.

The test table DDL in PG:

create table articles (
  "id" serial,
  "n" integer
);
@luikore
luikore / aes.java
Created December 2, 2013 09:17
AES, ruby vs java
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
class aes{
static byte[] ptext;
static SecretKeySpec key;
public static void main(String[] args) throws Exception {
@luikore
luikore / cmd.rb
Last active December 19, 2015 04:39
A textmate command to show model fields so model can be cleaner without annotates
#!/usr/bin/env ruby
name = File.basename ENV['TM_FILENAME']
name = name.dup
name[/\.rb$/] = ''
names = []
File.read(File.join(ENV['TM_PROJECT_DIRECTORY'], 'db/schema.rb')).scan(/( create_table "(\w+).*? end)/m) do |s, n|
names << [n.size, s] if n.start_with?(name)
end
def f n
half = n / 2
table = Hash.new 0
('0' * half).upto '9' * half do |s|
sum = s.chars.map(&:to_i).inject(:+)
table[sum] += 1
end
r = 0
table.each do |_, v|
r += v*v
@luikore
luikore / 1-y_render.rb
Last active December 18, 2015 13:48
Prove of concept: Nested layout render with Y-combinator.
# simulate template rendering
class Template < Struct.new(:name)
def render
puts "#{name}_begin"
r = "#{name}_begin\n"
r << yield if block_given?
puts "#{name}_end\n"
r << "#{name}_end\n"
end
end
@luikore
luikore / space_en_ch.rb
Created June 13, 2013 17:24
textmate command to add space between english and chinese
#! /usr/bin/env ruby
str = $stdin.read
if RUBY_VERSION < '1.9'
print str
exit
end
str.force_encoding 'utf-8'
str.gsub! /(\p{Han})([a-zA-Z0-9\(\)\[\]\{\}])/u do
"#$1 #$2"
@luikore
luikore / s.rb
Created May 4, 2013 07:02
Struct with cleaner inspect and pretty print
class S < Struct
def inspect
pretty_inspect # may be not defined yet, don't alias
end
def pretty_print(q)
q.group(1, sprintf("<%s", PP.mcall(self, Kernel, :class).name.split('::').last), '>') {
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
q.breakable
q.text member.to_s
@luikore
luikore / spec_helper.rb
Last active December 16, 2015 22:59
Fix puts/print/p/pp for rspec textmate formatter
RSpec.configure do |config|
if config.formatters.first.class.to_s =~ /TextMate/
def puts *xs
xs.each do |x|
$stdout.puts "<pre style='word-wrap:break-word;word-break:break-all;'>#{CGI.escape_html x.to_s}</pre>"
end
nil
end
def print *xs