Git Product home page Git Product logo

qrintf's Introduction

qrintf - sprintf accelerator

Build Status

The sprintf(3) family is a great set of functions for stringifying various kinds of data. The drawback is that they are slow. In certain applications, more than 10% of CPU time is consumed by the functions. The reason why it is slow is because it parses the given format at run-time.

qrintf is a preprocessor (and a set of runtime functions) that precompiles invocations of sprintf (and snprintf) with constant format strings into specialized forms.

The benchmark below shows the power of qrintf; converting IPv4 address to string becomes more than 10x faster when the preprocessor is applied to the source code.

$ gcc -O2 examples/ipv4addr.c
$ time ./a.out 1234567890
result: 73.150.2.210

real	0m2.514s
user	0m2.503s
sys	0m0.003s
$ qrintf gcc -O2 examples/ipv4addr.c
$ time ./a.out 1234567890
result: 73.150.2.210

real	0m0.175s
user	0m0.170s
sys	0m0.002s

INSTALL

make install PREFIX=/usr/local

COMMANDS

qrintf

qrintf is the command that wraps the C compiler (GCC or clang).

It preprocesses the source files using the preprocessor (by calling the compiler with -E option), applies qrintf-pp, and then once again invokes the compiler to compile the processed file.

qrintf-pp

qrintf-pp is the filter program that rewrites invocations of sprintf to optimized forms.

The command reads a C source file from the standard input, applies the necessary transformations, and prints the result to standard output.

FAQ

Q. License?

The software is provided under the MIT license.

Q. Why did you develop qrintf?

Because sprintf is the bottleneck in some of my applications. I plan to use it in H2O, an optimized HTTP server/library implementation with support for HTTP/1.x, HTTP/2, websocket.

Q. Which functions are optimized?

sprintf and snprintf.

Q. Is there a list of conversion specifiers that get optimized?

  • %c
  • %s
  • %d (modifiers: h, l, ll)
  • %u,%x,%X (modifiers: h, l, ll, z)

Field widths (including *) and 0 flag (for zero padding) are also recognized.

note: Invocations of sprintf using other conversion specifiers are left as is.

Q. How do I run the tests?

make test CC=gcc
make test CC=clang

Q. Shouldn't such feature be implemented in the compiler?

Agreed.

Q. Where can I find more information?

Please refer to the developer's weblog.

qrintf's People

Contributors

kazuho avatar imasahiro avatar drew-richardson avatar gfx avatar matsumotory avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.