Skip to content

Commit 960a275

Browse files
samyronbyroot
authored andcommitted
[ruby/json] Introduce ARM Neon and SSE2 SIMD.
(ruby/json#743) See the pull request for the long development history: ruby/json#743 ``` == Encoding activitypub.json (52595 bytes) ruby 3.4.2 (2025-02-15 revision ruby/json@d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 2.913k i/100ms Calculating ------------------------------------- after 29.377k (± 2.0%) i/s (34.04 μs/i) - 148.563k in 5.059169s Comparison: before: 23314.1 i/s after: 29377.3 i/s - 1.26x faster == Encoding citm_catalog.json (500298 bytes) ruby 3.4.2 (2025-02-15 revision ruby/json@d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 152.000 i/100ms Calculating ------------------------------------- after 1.569k (± 0.8%) i/s (637.49 μs/i) - 7.904k in 5.039001s Comparison: before: 1485.6 i/s after: 1568.7 i/s - 1.06x faster == Encoding twitter.json (466906 bytes) ruby 3.4.2 (2025-02-15 revision ruby/json@d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 309.000 i/100ms Calculating ------------------------------------- after 3.115k (± 3.1%) i/s (321.01 μs/i) - 15.759k in 5.063776s Comparison: before: 2508.3 i/s after: 3115.2 i/s - 1.24x faster ``` ruby/json@49003523da
1 parent 721283c commit 960a275

File tree

4 files changed

+585
-14
lines changed

4 files changed

+585
-14
lines changed

ext/json/generator/extconf.rb

+31
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,36 @@
66
else
77
append_cflags("-std=c99")
88
$defs << "-DJSON_GENERATOR"
9+
10+
if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
11+
if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
12+
# Try to compile a small program using NEON instructions
13+
if have_header('arm_neon.h')
14+
have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
15+
#include <arm_neon.h>
16+
int main() {
17+
uint8x16_t test = vdupq_n_u8(32);
18+
return 0;
19+
}
20+
SRC
21+
$defs.push("-DENABLE_SIMD")
22+
end
23+
end
24+
25+
if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC', opt='-msse2')
26+
#include <x86intrin.h>
27+
int main() {
28+
__m128i test = _mm_set1_epi8(32);
29+
return 0;
30+
}
31+
SRC
32+
$defs.push("-DENABLE_SIMD")
33+
end
34+
35+
have_header('cpuid.h')
36+
end
37+
38+
create_header
39+
940
create_makefile 'json/ext/generator'
1041
end

0 commit comments

Comments
 (0)