Skip to content

Commit 4900352

Browse files
authored
Introduce ARM Neon and SSE2 SIMD. (#743)
See the pull request for the long development history: #743 ``` == Encoding activitypub.json (52595 bytes) ruby 3.4.2 (2025-02-15 revision 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 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 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 ```
1 parent b14250f commit 4900352

File tree

5 files changed

+602
-28
lines changed

5 files changed

+602
-28
lines changed

.github/workflows/ci.yml

+17-14
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,29 @@ jobs:
1414

1515
host:
1616
needs: ruby-versions
17-
name: ${{ matrix.os }} ${{ matrix.ruby }}
17+
name: ${{ matrix.os }} ${{ matrix.ruby }} ${{ matrix.env }}
1818
runs-on: ${{ matrix.os }}
1919
strategy:
2020
fail-fast: false
2121
matrix:
2222
os:
23-
- ubuntu-latest
24-
- macos-14
25-
- windows-latest
23+
- ubuntu-latest
24+
- macos-14
25+
- windows-latest
2626
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
27+
env:
28+
- ""
2729
include:
28-
- { os: ubuntu-24.04-arm, ruby: 3.4 }
29-
- { os: macos-13, ruby: 3.4 }
30-
- { os: windows-latest , ruby: mswin } # ruby/ruby windows CI
31-
- { os: ubuntu-latest , ruby: jruby-9.4 } # Ruby 3.1
32-
- { os: macos-latest , ruby: truffleruby-head }
33-
- { os: ubuntu-latest , ruby: truffleruby-head }
30+
- { os: ubuntu-24.04-arm, ruby: 3.4 }
31+
- { os: ubuntu-latest , ruby: 3.4, env: "JSON_DISABLE_SIMD=1" }
32+
- { os: macos-13, ruby: 3.4 }
33+
- { os: windows-latest , ruby: mswin } # ruby/ruby windows CI
34+
- { os: ubuntu-latest , ruby: jruby-9.4 } # Ruby 3.1
35+
- { os: macos-latest , ruby: truffleruby-head }
36+
- { os: ubuntu-latest , ruby: truffleruby-head }
3437
exclude:
35-
- { os: windows-latest, ruby: jruby }
36-
- { os: windows-latest, ruby: jruby-head }
38+
- { os: windows-latest, ruby: jruby }
39+
- { os: windows-latest, ruby: jruby-head }
3740

3841
steps:
3942
- uses: actions/checkout@v4
@@ -49,9 +52,9 @@ jobs:
4952
bundle config --without benchmark
5053
bundle install
5154
52-
- run: rake compile
55+
- run: rake compile ${{ matrix.env }}
5356

54-
- run: rake test JSON_COMPACT=1
57+
- run: rake test JSON_COMPACT=1 ${{ matrix.env }}
5558

5659
- run: rake build
5760

ext/json/ext/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)