Skip to content

Commit 3cdaf06

Browse files
committed
Switch between old and new itoa method based on optimization level + digit size
1 parent 1ea5990 commit 3cdaf06

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

core/math/big/radix.odin

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,15 @@ int_itoa_raw :: proc(a: ^Int, radix: i8, buffer: []u8, size := int(-1), zero_ter
185185
/*
186186
Fast path for radixes that are a power of two.
187187
*/
188+
count := count_bits(a) or_return
189+
188190
if is_power_of_two(int(radix)) {
189191
if zero_terminate {
190192
available -= 1
191193
buffer[available] = 0
192194
}
193195

194-
shift, count: int
195-
// mask := _WORD(radix - 1);
196-
shift, err = log(DIGIT(radix), 2)
197-
count, err = count_bits(a)
196+
shift := log(DIGIT(radix), 2) or_return
198197
digit: _WORD
199198

200199
for offset := 0; offset < count; offset += shift {
@@ -224,7 +223,17 @@ int_itoa_raw :: proc(a: ^Int, radix: i8, buffer: []u8, size := int(-1), zero_ter
224223
return written, nil
225224
}
226225

227-
return _itoa_raw_full(a, radix, buffer, zero_terminate)
226+
// NOTE(Jeroen): The new method is faster for an `Int` up to ~32768 bits in size with optimizations.
227+
// At `.None` or `.Minimal`, it appears to always be faster.
228+
// If we optimize `itoa` further, this needs to be evaluated.
229+
itoa_method := _itoa_raw_full
230+
231+
when ODIN_OPTIMIZATION_MODE >= .Size {
232+
if count >= 32768 {
233+
itoa_method = _itoa_raw_old
234+
}
235+
}
236+
return itoa_method(a, radix, buffer, zero_terminate)
228237
}
229238

230239
itoa :: proc{int_itoa_string, int_itoa_raw}

0 commit comments

Comments
 (0)