From d1f3c8125854bb0976b08dbcbda3524d8ea3e3fe Mon Sep 17 00:00:00 2001 From: eno Date: Sun, 16 Mar 2025 19:51:59 +0100 Subject: Faster integer formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit provides an alternative implementation for a long → decimal conversion. The main difference is that it uses an algorithm pulled from https://github.com/jeaiii/itoa. The source there is C++, it was converted by hand to C for inclusion with this gem. jeaiii's algorithm is covered by the MIT License, see source code. On addition this version now also generates the string directly into the fbuffer, foregoing the need to run a separate memory copy. As a result, I see a speedup of 32% on Apple Silicon M1 for an integer set of benchmarks. --- test/json/json_generator_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test/json') diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index bf4796a6bf..9cca447e2a 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -707,4 +707,16 @@ class JSONGeneratorTest < Test::Unit::TestCase assert_equal expected, value.to_json end end + + def test_numbers_of_various_sizes + numbers = [ + 0, 1, -1, 9, -9, 13, -13, 91, -91, 513, -513, 7513, -7513, + 17591, -17591, -4611686018427387904, 4611686018427387903, + 2**62, 2**63, 2**64, -(2**62), -(2**63), -(2**64) + ] + + numbers.each do |number| + assert_equal "[#{number}]", JSON.generate([number]) + end + end end -- cgit v1.2.3