Skip to content

Add utility functions for string to integer conversions #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 19, 2024

Conversation

chqrlie
Copy link
Collaborator

@chqrlie chqrlie commented Apr 9, 2024

  • add is_upper_ascii() and to_upper_ascii()
  • add u32toa(), i32toa(), u64toa(), i64toa() u32toa_radix(), u64toa_radix(), i64toa_radix()
  • use direct converters instead of snprintf()
  • copy NaN and Infinity directly in js_dtoa1()
  • optimize js_number_toString() for small ints
  • use JS_NewStringLen() instead of JS_NewString() when possible
  • add more precise conversion tests in microbench.js

- add `is_upper_ascii()` and `to_upper_ascii()`
- add `u32toa()`, `i32toa()`, `u64toa()`, `i64toa()`
  `u32toa_radix()`, `u64toa_radix()`, `i64toa_radix()`
- use direct converters instead of `snprintf()`
- copy NaN and Infinity directly in `js_dtoa1()`
- optimize `js_number_toString()` for small ints
- use `JS_NewStringLen()` instead of `JS_NewString()` when possible
- add more precise conversion tests in microbench.js
Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some compilers are not happy...

@chqrlie
Copy link
Collaborator Author

chqrlie commented Apr 11, 2024

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 added 6 commits April 14, 2024 01:06
- move `is_be()` to cutils.h
- use register shift variant for `u32toa` and `u64toa`
- use length_loop variant for `u32toa_radix` and `u64toa_radix`
- add extensive benchmark for integer conversion variants in tests/test_conv.c
@chqrlie
Copy link
Collaborator Author

chqrlie commented Apr 14, 2024

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.

@chqrlie chqrlie changed the title Add utility functions Add utility functions for string to integer conversions Apr 16, 2024
Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

@chqrlie chqrlie merged commit 83726bb into quickjs-ng:master Apr 19, 2024
@chqrlie chqrlie deleted the integer-converters branch April 19, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants