eliaskosunen/scnlib

Improvements for embedded targets

cjvaughter opened this issue · 1 comments

Problem

This is a great library, but there are a few changes that would drastically improve support for embedded targets.
These ideas are mostly based on what has been done for fmt.

  • Conditional locale support would remove an enormous amount of code.
  • Conditional float, double, and long double support would provide another substantial reduction. Especially for targets without a floating-point unit.
  • Making the fast_float -> std::from_chars -> strtod fallback optional would help as well.
  • The current version of ARM GCC has partial support for std::from_chars (no floating-point), so SCN_HAS_FLOAT_CHARCONV misbehaves.
  • The bundled fast_float is missing two PRs that fix ARM GCC support (#122 & #123).

Testing

Targeting a Cortex-M4 and optimizing for size, any reference to scn increased my code size from ~78K to ~284K (+206K)!
After taking an ax to some of the code, removing locale and long double, it reduced to ~151K (+73K).
Removing the fallback got it down to ~136K (+58K).
A total reduction of 148K.

I believe that's about as good as it's going to get, and I'm happy to accept that size given it makes my application far simpler.
It's about double the size of fmt, but that comes with the territory.

Proposal

  • Equivalents to FMT_USE_FLOAT, FMT_USE_DOUBLE, and FMT_USE_LONG_DOUBLE
  • SCN_DEFAULT_LOCALE_* instead of hard-coded values for scn::v1::detail::locale_defaults so the user can override
  • SCN_USE_STATIC_LOCALE
    • Skip localization
    • Reject 'L' and 'n' format string flags at compile time, fail at runtime, or ignore
  • SCN_SKIP_FROM_CHARS
    • Skip std::from_chars and fallback directly to strtod
  • SCN_SKIP_STRTOD
    • Fail at runtime when strtod fallback would have occurred
  • I'm not sure how to detect partial std::from_chars support. There doesn't seem to be any indication except the documentation. SCN_SKIP_FROM_CHARS may be sufficient for now.
  • Update fast_float

I could potentially do a PR if I have time. Let me know your thoughts.

Thank you for your efforts! Good to hear that this works also in embedded environments.

Both PRs merged with some modifications. I'll also need to do something similar for v2.