This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] speedup to_string for floating point numbers
AbandonedPublic

Authored by ivafanas on Jul 8 2019, 7:49 AM.

Details

Summary

Proposal to speedup to_string / to_wstring for floating points numbers by reducing sprintf calls and allocations using stack buffer.

Benchmark results:
http://quick-bench.com/vS-2RuYzsZrBNRbhsliN4o8M8B4

  • to_string is improved in 1.3x times for small values (tested on zero)
  • to_wstring is not changed for small values
  • to_string is improved in 2.1x times for -DBL_MAX
  • to_wstring is improved in 5.2x times for -DBL_MAX

Proposed implementation fallbacks to the previous one for some long double values and non-standard floats / doubles so conversion tests were added for numeric_limits<T>::max / min / lowest.

Diff Detail

Event Timeline

ivafanas created this revision.Jul 8 2019, 7:49 AM

I would like to hold this for a while; because in a few weeks, we should have an implementation of to_chars for floating point - which we may be able to use in to_string (like we did for the integral types).

I would like to hold this for a while; because in a few weeks, we should have an implementation of to_chars for floating point - which we may be able to use in to_string (like we did for the integral types).

Hi Marshall,

I'm not sure there is a way to reuse to_chars for floating point numbers inside to_string function due to locale issues:

  • to_string must produce the same output as sprintf with %f or %Lf format specifiers (N. 7, 8, 9 and note 3)
  • sprintf is locale-dependent. I suppose there is a difference between comma and point character for locales. Maybe something more.
  • to_chars must produce the same result as sprintf in the default ("C") locale (N. 2)

If there is a tricky way to get around problem with locale dependency it would be nice.

ivafanas abandoned this revision.May 15 2022, 9:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2022, 9:28 PM