Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP ARM Neon/AVX2 SIMD implementation. #730

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gemfile.lock
.DS_Store
*/**/Makefile
*/**/*.o
*/**/extconf.h
*/**/*.class
*/**/*.jar
.byebug_history
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end

file EXT_GENERATOR_DL => EXT_GENERATOR_SRC do
cd EXT_GENERATOR_DIR do
ruby 'extconf.rb'
ruby "extconf.rb #{ENV['JSON_GENERATOR_CONFIGURE_OPTS']}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably a better way to accomplish this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, also I cleaned up the Rakefile the other day, so this will cause a merge conflict.

sh MAKE
end
cp "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
Expand Down
58 changes: 58 additions & 0 deletions benchmark/encoder-simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "benchmark/ips"
require "json"
require "date"
require "oj"

Oj.default_options = Oj.default_options.merge(mode: :compat)

if ENV["ONLY"]
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
RUN.default = false
elsif ENV["EXCEPT"]
RUN = ENV["EXCEPT"].split(/[,: ]/).map{|x| [x.to_sym, false] }.to_h
RUN.default = true
else
RUN = Hash.new(true)
end

def implementations(ruby_obj)
state = JSON::State.new(JSON.dump_default_options)
{
json: ["json", proc { JSON.generate(ruby_obj) }],
oj: ["oj", proc { Oj.dump(ruby_obj) }],
}
end

def benchmark_encoding(benchmark_name, ruby_obj, check_expected: true, except: [])
json_output = JSON.dump(ruby_obj)
puts "== Encoding #{benchmark_name} (#{json_output.bytesize} bytes)"

impls = implementations(ruby_obj).select { |name| RUN[name] }
except.each { |i| impls.delete(i) }

Benchmark.ips do |x|
expected = ::JSON.dump(ruby_obj) if check_expected
impls.values.each do |name, block|
begin
result = block.call
if check_expected && expected != result
puts "#{name} does not match expected output. Skipping"
puts "Expected:" + '-' * 40
puts expected
puts "Actual:" + '-' * 40
puts result
puts '-' * 40
next
end
rescue => error
puts "#{name} unsupported (#{error})"
next
end
x.report(name, &block)
end
x.compare!(order: :baseline)
end
puts
end

benchmark_encoding "long string", (["this is a test of the emergency broadcast system."*5]*500)
50 changes: 50 additions & 0 deletions ext/json/ext/generator/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,55 @@
else
append_cflags("-std=c99")
$defs << "-DJSON_GENERATOR"

if enable_config('use-simd', default=true)
if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
$defs.push("-DENABLE_SIMD")

# Try to compile a small program using NEON instructions
if have_header('arm_neon.h')
have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
#include <arm_neon.h>
int main() {
uint8x16_t test = vdupq_n_u8(32);
return 0;
}
SRC

have_type('uint8x8_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
#include <arm_neon.h>
int main() {
uint8x8_t test = vdup_n_u8(32);
return 0;
}
SRC
end
elsif have_header('x86intrin.h')

if have_type('__m256i', headers=['x86intrin.h']) && try_compile(<<~'SRC', opt='-mavx2')
#include <x86intrin.h>
int main() {
__m256i test = _mm256_set1_epi8(32);
return 0;
}
SRC
$defs.push("-DENABLE_SIMD")
end

if have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC', opt='-mavx2')
#include <x86intrin.h>
int main() {
__m128i test = _mm_set1_epi8(32);
return 0;
}
SRC
$defs.push("-DENABLE_SIMD") unless $defs.include?('-DENABLE_SIMD')
end
end

have_header('cpuid.h')
end

create_header
create_makefile 'json/ext/generator'
end
Loading