@@ -63,37 +63,18 @@ def benchmark_encoding(benchmark_name, ruby_obj, check_expected: true, except: [
6363 puts
6464end
6565
66- # NB: Notes are based on ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [arm64-darwin23]
67-
68- # On the first two micro benchmarks, the limiting factor is the fixed cost of initializing the
69- # generator state. Since `JSON.generate` now lazily allocate the `State` object we're now ~10-20% faster
70- # than `Oj.dump`.
7166benchmark_encoding "small mixed" , [ 1 , "string" , { a : 1 , b : 2 } , [ 3 , 4 , 5 ] ]
7267benchmark_encoding "small nested array" , [ [ 1 , 2 , 3 , 4 , 5 ] ] *10
7368benchmark_encoding "small hash" , { "username" => "jhawthorn" , "id" => 123 , "event" => "wrote json serializer" }
7469
75- # On string encoding we're ~20% faster when dealing with mostly ASCII, but ~50% slower when dealing
76- # with mostly multi-byte characters. There's likely some gains left to be had in multi-byte handling.
7770benchmark_encoding "mixed utf8" , ( [ ( "a" * 5000 ) + "€" + ( "a" * 5000 ) ] * 500 )
7871benchmark_encoding "mostly utf8" , ( [ ( "€" * 3333 ) ] * 500 )
7972
80- # On these benchmarks we perform well, we're on par or a bit better.
8173benchmark_encoding "integers" , ( 1_000_000 ..1_001_000 ) . to_a , except : %i( json_state )
8274benchmark_encoding "activitypub.json" , JSON . load_file ( "#{ __dir__ } /data/activitypub.json" )
8375benchmark_encoding "citm_catalog.json" , JSON . load_file ( "#{ __dir__ } /data/citm_catalog.json" )
8476benchmark_encoding "twitter.json" , JSON . load_file ( "#{ __dir__ } /data/twitter.json" )
8577
86- # This benchmark spent the overwhelming majority of its time in `ruby_dtoa`. We rely on Ruby's implementation
87- # which uses a relatively old version of dtoa.c from David M. Gay.
88- # Oj in `compat` mode is ~10% slower than `json`, but in its default mode is noticeably faster here because
89- # it limits the precision of floats, breaking roundtripping. That's not something we should emulate.
90- #
91- # Since a few years there are now much faster float to string implementations such as Ryu, Dragonbox, etc,
92- # but all these are implemented in C++11 or newer, making it hard if not impossible to include them.
93- # Short of a pure C99 implementation of these newer algorithms, there isn't much that can be done to match
94- # Oj speed without losing precision.
9578benchmark_encoding "canada.json" , JSON . load_file ( "#{ __dir__ } /data/canada.json" ) , check_expected : false
9679
97- # We're about 10% faster when `to_json` calls are involved, but this wasn't particularly optimized, there might be
98- # opportunities here.
9980benchmark_encoding "many #to_json calls" , [ { object : Object . new , int : 12 , float : 54.3 , class : Float , time : Time . now , date : Date . today } ] * 20
0 commit comments