Skip to content

Commit 0e60e05

Browse files
radiospielbyroot
authored andcommitted
Faster integer formatting
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.
1 parent 00e3e28 commit 0e60e05

File tree

5 files changed

+310
-17
lines changed

5 files changed

+310
-17
lines changed

ext/json/ext/fbuffer/fbuffer.h

+39-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ruby.h"
55
#include "ruby/encoding.h"
6+
#include "../vendor/jeaiii-ltoa.h"
67

78
/* shims */
89
/* This is the fallback definition from Ruby 3.4 */
@@ -150,6 +151,13 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
150151
}
151152
}
152153

154+
/* Appends a character into a buffer. The buffer needs to have sufficient capacity, via fbuffer_inc_capa(...). */
155+
static inline void fbuffer_append_reserved_char(FBuffer *fb, char chr)
156+
{
157+
fb->ptr[fb->len] = chr;
158+
fb->len += 1;
159+
}
160+
153161
static void fbuffer_append_str(FBuffer *fb, VALUE str)
154162
{
155163
const char *newstr = StringValuePtr(str);
@@ -167,25 +175,39 @@ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
167175
fb->len++;
168176
}
169177

170-
static long fltoa(long number, char *buf)
171-
{
172-
static const char digits[] = "0123456789";
173-
long sign = number;
174-
char* tmp = buf;
175-
176-
if (sign < 0) number = -number;
177-
do *tmp-- = digits[number % 10]; while (number /= 10);
178-
if (sign < 0) *tmp-- = '-';
179-
return buf - tmp;
180-
}
181-
182-
#define LONG_BUFFER_SIZE 20
178+
/*
179+
* Appends the decimal string representation of \a number into the buffer.
180+
*/
183181
static void fbuffer_append_long(FBuffer *fb, long number)
184182
{
185-
char buf[LONG_BUFFER_SIZE];
186-
char *buffer_end = buf + LONG_BUFFER_SIZE;
187-
long len = fltoa(number, buffer_end - 1);
188-
fbuffer_append(fb, buffer_end - len, len);
183+
/*
184+
* The to_text_from_ulong() function produces digits left-to-right,
185+
* allowing us to write directly into the buffer, but we don't know
186+
* the number of resulting characters.
187+
*
188+
* We do know, however, that the `number` argument is always in the
189+
* range 0xc000000000000000 to 0x3fffffffffffffff, or, in decimal,
190+
* -4611686018427387904 to 4611686018427387903. The max number of chars
191+
* generated is therefore 20 (including a potential sign character).
192+
*/
193+
194+
static const int MAX_CHARS_FOR_LONG = 20;
195+
196+
fbuffer_inc_capa(fb, MAX_CHARS_FOR_LONG);
197+
198+
if (number < 0) {
199+
fbuffer_append_reserved_char(fb, '-');
200+
201+
/*
202+
* Since number is always > LONG_MIN, `-number` will not overflow
203+
* and is always the positive abs() value.
204+
*/
205+
number = -number;
206+
}
207+
208+
char* d = fb->ptr + fb->len;
209+
char* end = to_text_from_ulong(d, number);
210+
fb->len += end - d;
189211
}
190212

191213
static VALUE fbuffer_finalize(FBuffer *fb)

ext/json/ext/generator/depend

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
generator.o: generator.c $(srcdir)/../fbuffer/fbuffer.h
22
generator.o: generator.c $(srcdir)/../vendor/fpconv.c
3+
generator.o: generator.c $(srcdir)/../vendor/jeaiii-ltoa.h

ext/json/ext/generator/generator.c

+1
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ void Init_generator(void)
17101710
cFragment = rb_const_get(mJSON, rb_intern("Fragment"));
17111711

17121712
VALUE mExt = rb_define_module_under(mJSON, "Ext");
1713+
17131714
VALUE mGenerator = rb_define_module_under(mExt, "Generator");
17141715

17151716
rb_global_variable(&eGeneratorError);

ext/json/ext/vendor/jeaiii-ltoa.h

+257
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/*
2+
3+
This file is released under the terms of the MIT License. It is based on the
4+
work of James Edward Anhalt III, with the original license listed below.
5+
6+
MIT License
7+
8+
Copyright (c) 2024,2025 Enrico Thierbach - https://github.com/radiospiel
9+
Copyright (c) 2022 James Edward Anhalt III - https://github.com/jeaiii/itoa
10+
11+
Permission is hereby granted, free of charge, to any person obtaining a copy
12+
of this software and associated documentation files (the "Software"), to deal
13+
in the Software without restriction, including without limitation the rights
14+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
copies of the Software, and to permit persons to whom the Software is
16+
furnished to do so, subject to the following conditions:
17+
18+
The above copyright notice and this permission notice shall be included in all
19+
copies or substantial portions of the Software.
20+
21+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27+
SOFTWARE.
28+
*/
29+
30+
#ifndef JEAIII_TO_TEXT_H_
31+
#define JEAIII_TO_TEXT_H_
32+
33+
#include <stdint.h>
34+
35+
typedef uint_fast32_t u32_t;
36+
typedef uint_fast64_t u64_t;
37+
38+
#pragma clang diagnostic push
39+
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
40+
41+
#pragma GCC diagnostic push
42+
#pragma GCC diagnostic ignored "-Wmissing-braces"
43+
44+
#define u32(x) ((u32_t)(x))
45+
#define u64(x) ((u64_t)(x))
46+
47+
struct pair
48+
{
49+
char dd[2];
50+
};
51+
52+
#define cast_to_pair_ptr(b) ((struct pair*)(void*)(b))
53+
54+
static struct pair digits_dd[100] =
55+
{
56+
{ '0', '0' }, { '0', '1' }, { '0', '2' }, { '0', '3' }, { '0', '4' }, { '0', '5' }, { '0', '6' }, { '0', '7' }, { '0', '8' }, { '0', '9' },
57+
{ '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' },
58+
{ '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' },
59+
{ '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' },
60+
{ '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' },
61+
{ '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' },
62+
{ '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' },
63+
{ '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' },
64+
{ '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' },
65+
{ '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' },
66+
};
67+
68+
#define NUL 'x'
69+
70+
static struct pair digits_fd[100] =
71+
{
72+
{ '0', NUL }, { '1', NUL }, { '2', NUL }, { '3', NUL }, { '4', NUL }, { '5', NUL }, { '6', NUL }, { '7', NUL }, { '8', NUL }, { '9', NUL },
73+
{ '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' },
74+
{ '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' },
75+
{ '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' },
76+
{ '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' },
77+
{ '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' },
78+
{ '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' },
79+
{ '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' },
80+
{ '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' },
81+
{ '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' },
82+
};
83+
84+
#undef NUL
85+
86+
static u64_t mask24 = (u64(1) << 24) - 1;
87+
static u64_t mask32 = (u64(1) << 32) - 1;
88+
static u64_t mask57 = (u64(1) << 57) - 1;
89+
90+
static
91+
char* to_text_from_ulong(char* b, u64_t n) {
92+
if (n < u32(1e2))
93+
{
94+
*cast_to_pair_ptr(b) = digits_fd[n];
95+
return n < 10 ? b + 1 : b + 2;
96+
}
97+
if (n < u32(1e6))
98+
{
99+
if (n < u32(1e4))
100+
{
101+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * n;
102+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
103+
b -= n < u32(1e3);
104+
u32_t f2 = (f0 & mask24) * 100;
105+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
106+
return b + 4;
107+
}
108+
u64_t f0 = u64(10 * (1ull << 32ull)/ 1e5 + 1) * n;
109+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
110+
b -= n < u32(1e5);
111+
u64_t f2 = (f0 & mask32) * 100;
112+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
113+
u64_t f4 = (f2 & mask32) * 100;
114+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
115+
return b + 6;
116+
}
117+
if (n < u64(1ull << 32ull))
118+
{
119+
if (n < u32(1e8))
120+
{
121+
u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * n >> 16;
122+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
123+
b -= n < u32(1e7);
124+
u64_t f2 = (f0 & mask32) * 100;
125+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
126+
u64_t f4 = (f2 & mask32) * 100;
127+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
128+
u64_t f6 = (f4 & mask32) * 100;
129+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
130+
return b + 8;
131+
}
132+
u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * n;
133+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 57];
134+
b -= n < u32(1e9);
135+
u64_t f2 = (f0 & mask57) * 100;
136+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57];
137+
u64_t f4 = (f2 & mask57) * 100;
138+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57];
139+
u64_t f6 = (f4 & mask57) * 100;
140+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57];
141+
u64_t f8 = (f6 & mask57) * 100;
142+
*cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57];
143+
return b + 10;
144+
}
145+
146+
// if we get here U must be u64 but some compilers don't know that, so reassign n to a u64 to avoid warnings
147+
u32_t z = n % u32(1e8);
148+
u64_t u = n / u32(1e8);
149+
150+
if (u < u32(1e2))
151+
{
152+
// u can't be 1 digit (if u < 10 it would have been handled above as a 9 digit 32bit number)
153+
*cast_to_pair_ptr(b) = digits_dd[u];
154+
b += 2;
155+
}
156+
else if (u < u32(1e6))
157+
{
158+
if (u < u32(1e4))
159+
{
160+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u;
161+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
162+
b -= u < u32(1e3);
163+
u32_t f2 = (f0 & mask24) * 100;
164+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
165+
b += 4;
166+
}
167+
else
168+
{
169+
u64_t f0 = u64(10 * (1ull << 32ull) / 1e5 + 1) * u;
170+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
171+
b -= u < u32(1e5);
172+
u64_t f2 = (f0 & mask32) * 100;
173+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
174+
u64_t f4 = (f2 & mask32) * 100;
175+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
176+
b += 6;
177+
}
178+
}
179+
else if (u < u32(1e8))
180+
{
181+
u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * u >> 16;
182+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
183+
b -= u < u32(1e7);
184+
u64_t f2 = (f0 & mask32) * 100;
185+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
186+
u64_t f4 = (f2 & mask32) * 100;
187+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
188+
u64_t f6 = (f4 & mask32) * 100;
189+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
190+
b += 8;
191+
}
192+
else if (u < u64(1ull << 32ull))
193+
{
194+
u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * u;
195+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 57];
196+
b -= u < u32(1e9);
197+
u64_t f2 = (f0 & mask57) * 100;
198+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57];
199+
u64_t f4 = (f2 & mask57) * 100;
200+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57];
201+
u64_t f6 = (f4 & mask57) * 100;
202+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57];
203+
u64_t f8 = (f6 & mask57) * 100;
204+
*cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57];
205+
b += 10;
206+
}
207+
else
208+
{
209+
u32_t y = u % u32(1e8);
210+
u /= u32(1e8);
211+
212+
// u is 2, 3, or 4 digits (if u < 10 it would have been handled above)
213+
if (u < u32(1e2))
214+
{
215+
*cast_to_pair_ptr(b) = digits_dd[u];
216+
b += 2;
217+
}
218+
else
219+
{
220+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u;
221+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
222+
b -= u < u32(1e3);
223+
u32_t f2 = (f0 & mask24) * 100;
224+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
225+
b += 4;
226+
}
227+
// do 8 digits
228+
u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * y >> 16) + 1;
229+
*cast_to_pair_ptr(b) = digits_dd[f0 >> 32];
230+
u64_t f2 = (f0 & mask32) * 100;
231+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
232+
u64_t f4 = (f2 & mask32) * 100;
233+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
234+
u64_t f6 = (f4 & mask32) * 100;
235+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
236+
b += 8;
237+
}
238+
// do 8 digits
239+
u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * z >> 16) + 1;
240+
*cast_to_pair_ptr(b) = digits_dd[f0 >> 32];
241+
u64_t f2 = (f0 & mask32) * 100;
242+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
243+
u64_t f4 = (f2 & mask32) * 100;
244+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
245+
u64_t f6 = (f4 & mask32) * 100;
246+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
247+
return b + 8;
248+
}
249+
250+
#undef u32
251+
#undef u64
252+
253+
#pragma clang diagnostic pop
254+
#pragma GCC diagnostic pop
255+
256+
#endif // JEAIII_TO_TEXT_H_
257+

test/json/json_generator_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,16 @@ def test_json_generate_float
707707
assert_equal expected, value.to_json
708708
end
709709
end
710+
711+
def test_numbers_of_various_sizes
712+
numbers = [
713+
0, 1, -1, 9, -9, 13, -13, 91, -91, 513, -513, 7513, -7513,
714+
17591, -17591, -4611686018427387904, 4611686018427387903,
715+
2**62, 2**63, 2**64, -(2**62), -(2**63), -(2**64)
716+
]
717+
718+
numbers.each do |number|
719+
assert_equal "[#{number}]", JSON.generate([number])
720+
end
721+
end
710722
end

0 commit comments

Comments
 (0)