This is an archive of the discontinued LLVM Phabricator instance.

[libc++][format] Use to_chars internals.
AbandonedPublic

Authored by Mordante on Jul 16 2022, 4:14 AM.

Details

Reviewers
ldionne
vitaut
Group Reviewers
Restricted Project
Summary

The external to_chars interface does some validations already done in
format. So instead of doing the same validations again, directly call
the internals of to_chars.

This has a small size benefit

  text	   data	    bss	    dec	    hex	filename
718800	  12472	    488	 731760	  b2a70	./formatter_int.libcxx.out-baseline
714915	  12472	    488	 727875	  b1b43	./formatter_int.libcxx.out

And a small performance benefit for simple formatting:

Comparing ./formatter_int.libcxx.out-baseline to ./formatter_int.libcxx.out
Benchmark                                                               Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------------------------
BM_Basic<uint32_t>                                                   -0.1139         -0.1125            61            54            61            54
BM_Basic<int32_t>                                                    -0.0265         -0.0250            62            60            62            60
BM_Basic<uint64_t>                                                   +0.0051         +0.0069            82            83            82            83
BM_Basic<int64_t>                                                    +0.0084         +0.0101            82            83            82            83
BM_BasicLow<__uint128_t>                                             -0.0588         -0.0572            87            82            87            82
BM_BasicLow<__int128_t>                                              -0.0548         -0.0532            88            83            88            83
BM_Basic<__uint128_t>                                                -0.0253         -0.0238           239           233           238           233
BM_Basic<__int128_t>                                                 -0.0346         -0.0331           222           214           222           214

Diff Detail

Event Timeline

Mordante created this revision.Jul 16 2022, 4:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 4:14 AM
Mordante requested review of this revision.Jul 16 2022, 4:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 4:14 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
ldionne requested changes to this revision.Aug 2 2022, 9:37 AM

Thanks for the patch! I think the improvement is nice, but I have a few concerns about duplicating logic with to_chars.

libcxx/include/__format/formatter_integral.h
257–271
293

Why is this required? Wouldn't it be possible to instead call a single __to_chars_base function that contains this logic (but it would live inside the implementation of to_chars).

I think what I'm saying is that I don't understand why __to_chars_integral doesn't handle the various bases itself. IMO it should be possible to easily bypass the checks you're trying to avoid in to_chars without reimplementing this base dispatching.

311

Would it be possible to handle base 10 in __format_integer_base with if constexpr instead? I'm trying to find ways to make the switch below look more regular.

This revision now requires changes to proceed.Aug 2 2022, 9:37 AM
Mordante abandoned this revision.Aug 31 2023, 10:43 AM

Based on the review comments I think this as little merit to pursue.