Better unsigned to string conversion
authorAndrei Alexandrescu <aalexandre@fb.com>
Tue, 10 Jul 2012 18:42:34 +0000 (11:42 -0700)
committerJordan DeLong <jdelong@fb.com>
Fri, 12 Oct 2012 04:33:36 +0000 (21:33 -0700)
commit43bdc5d7d0bee944c1f437f3e159f32d4c57f3cd
treedade35c157f83b977ff816f54d6d48f9503c6045
parent3a5767e72de649744ede7f616e61864958ae715f
Better unsigned to string conversion

Summary:
In https://phabricator.fb.com/D511928 Brian mentioned the current API for string append is insufficient for appending to a buffer. That made me curious about the relative performance of classic and table-based number to ASCII conversions.

The results were interesting as on the average (over all digit lengths) the table-based conversion was faster, but performance was lackluster (in the worst case half as fast as the classic implementation) for large numbers, I presume due to the cache misses incurred by the tables.

This diff proposes an improved unsigned-to-ASCII primitive that is much faster than both table-based (previous Folly) and classic primitive. The key is a fast digits10() implementation that precomputes the space required by the conversion. After that, the digits are issued in place, no more reverse required. The new routine is up to 14x faster than the classic implementation, depending on the number of digits (benchmarks in comments).

Adding a few people who may be interested in the matter. Brian, thanks for bringing this matter up; if this gets in you may want to use the folly routine in proxygen.

Test Plan: unittest and benchmarks.

Reviewed By: simpkins@fb.com

FB internal diff: D515572
folly/Benchmark.h
folly/Conv.h
folly/Format-inl.h
folly/test/ConvTest.cpp