[cfe-dev] Applying custom filters after preprocessing (and compile-time sprintf optimization)

Kazuho Oku kazuhooku at gmail.com
Mon Oct 6 22:41:35 PDT 2014


2014-10-03 11:41 GMT+09:00 Richard Smith <richard at metafoo.co.uk>:

> On 2 Oct 2014 18:56, "Kazuho Oku" <kazuhooku at gmail.com> wrote:
> >
> >
> >
> > 2014-10-03 9:31 GMT+09:00 Richard Smith <richard at metafoo.co.uk>:
> >>
> >> On Thu, Oct 2, 2014 at 5:18 PM, Kazuho Oku <kazuhooku at gmail.com> wrote:
> >>>
> >>> Hi,
> >>>
> >>> Recently I have started writing a preprocessor that wraps the C
> preprocessor for optimizing calls to sprintf, since it is the one of the
> bottlenecks of my application.
> >>> https://github.com/kazuho/qrintf
> >>>
> >>> For GCC, it was easy for me to override the default C preprocessor, by
> using its `-no-integrated-cpp` and `-wrapper` options (how I have done it
> can be found in the `qrinf-gcc` and `qrintf-gcc-wrapper` files included in
> the above repository).
> >>>
> >>> However I have so far been unable to find a similar command-line
> option for Clang.  Is there any way to post-process the output of the
> preprocessor in Clang?  Or do I need to modify the source code of Clang
> itself?
> >>>
> >>> Thank you in advance.
> >>>
> >>> PS. BTW it would be great if compile-time optimization of sprintf goes
> into Clang.  Is there any chance of such possibility?
> >>
> >>
> >> In the general case, it's not possible to optimize sprintf fully,
> because its behavior depends on the current locale (this affects %d and %u,
> which it looks like you optimize) and it might have libc-specific
> extensions, but LLVM does optimize in some simple cases: for instance, a
> sprintf containing no % is optimized into memcpy. I expect patches would be
> accepted to implement further cases, such as handling %s and %c.
> >
> >
> > Thank you for the response.  I am delighted to hear that.
> >
> > I had thought that %d and %u weren't affected by the locale (since the
> digits 0..9 and non-monetary use of positive/negative signs are IMO not
> locale-dependent).
>
> Sorry, yes, I think you're right: they aren't affected by locale by
> default, only if you use the ' or I flags.
>

Thank you for confirmation.

FYI I have polished up the implementation (as a preprocessor wrapper) and
have taken a benchmark using a HTTP server that I develop.  It gained 20%
performance boost by preprocessing calls to sprintf when using GCC, which
might be a proof that such an optimization does have real impact.  The
details can be found here:
http://blog.kazuhooku.com/2014/10/announcing-qrintf-and-qrintf-gcc.html

-- 
Kazuho Oku
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141007/4941d84e/attachment.html>


More information about the cfe-dev mailing list