quickjs-ng / quickjs

QuickJS, the Next Generation: a mighty JavaScript engine
MIT License
691 stars 66 forks source link

Add utility functions for string to integer conversions #366

Closed chqrlie closed 2 months ago

chqrlie commented 2 months ago
chqrlie commented 2 months ago

Some compilers are not happy...

Yes, gcc 32-bit optimizes the naive copy loop beyond sanity (uses SIMD instructions) which causes reads beyond the array boundaries. I am investigating other approaches. WIP

chqrlie commented 2 months ago

I ran extensive benchmarks to test various methods to convert integers to strings in base 10 or others. You can run make testconv on your system to test with different compilers and options. I changed the variant used in cutils.c for better performance and to avoid some nasty over-optimisations by gcc on some CI targets. The variant selected shifts 1 to 7 digits into a 64-bit register and writes them as an 8-byte block including the null terminator. This is quite fast, albeit not as fast as composing the string 2 digits at a time, which uses more memory and causes gcc to output warnings for what seems to be an optimisation issue.