Skip to content

Instantly share code, notes, and snippets.

@jamescook
Created April 12, 2011 03:56
Show Gist options
  • Save jamescook/914887 to your computer and use it in GitHub Desktop.
Save jamescook/914887 to your computer and use it in GitHub Desktop.
# Compile CityHash
#g++ -shared city.cc -o libcity.dylib
#
# Figure out the symbol names.. this doesn't look right. Probably user error.
# nm -g libcity.dylib
#
=begin
0000000000000db7 T __Z10CityHash64PKcm
0000000000001800 T __Z11CityHash128PKcm
...
0000000000001a55 T __ZSt9make_pairIyyESt4pairIT_T0_ES1_S2_
000000000000114e T __Z18CityHash64WithSeedPKcmy
000000000000117f T __Z19CityHash128WithSeedPKcmSt4pairIyyE
U ___gxx_personality_v0
U dyld_stub_binder
=end
module CityHash
require "rubygems"
require "ffi"
extend FFI::Library
ffi_lib "city"
attach_function :_Z10CityHash64PKcm, [:string, :size_t], :uint64
attach_function :_Z11CityHash128PKcm, [:string, :size_t], :ulong
attach_function :_Z18CityHash64WithSeedPKcmy, [:string, :size_t, :ulong], :ulong
end
if __FILE__ == $0
require "benchmark"
require "zlib"
str = ''
# You can preseed the CityHash functions
seed = 1_100_200_300_400.1
# Build a large-ish string
100.times{str << (("A".."Z").to_a + (0..9).to_a).flatten.join}
size = str.size
puts "=======TO HASH=========="
puts str
puts "=======TO HASH=========="
Benchmark.bmbm do |x|
x.report("cityhash64") { 10_000.times{CityHash._Z10CityHash64PKcm(str, size)} }
x.report("cityhash128") { 10_000.times{CityHash._Z11CityHash128PKcm(str, size)} }
x.report("cityhash64 (w/ seed)") { 10_000.times{CityHash._Z18CityHash64WithSeedPKcmy(str, size, seed)} }
x.report("string#hash") { 10_000.times{str.hash} }
x.report("Zlib#crc32") { 10_000.times{ Zlib.crc32(str) } }
end
puts
puts "Ruby Version is #{RUBY_VERSION}"
puts "City Hash (64): " + CityHash._Z10CityHash64PKcm(str, size).to_s
puts "City Hash (128): " + CityHash._Z11CityHash128PKcm(str, size).to_s
# the String#hash value changes everytime you run the script (???)
puts "String#hash: " + str.hash.to_s
puts "Zlib#crc32 " + Zlib.crc32(str).to_s
end
@nashby
Copy link

nashby commented Apr 17, 2011

Does _Z11CityHash128PKcm work ok? What ffi and ruby version are you using? Because it crushes for me :(

@jamescook
Copy link
Author

Ruby 1.9.2 and latest FFI version. Those weird method names are the result of running 'nm -g libcity.dylib ' and inspecting the dumped symbols. do you get the same symbols?

@nashby
Copy link

nashby commented Apr 18, 2011

Actually, I've added

extern "C"

before these functions and now can use it with normal names. But I've tried without extern "C' stuff too and it still crushes .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment