This is an archive of the discontinued LLVM Phabricator instance.

Make BinaryStreamReader::readCString a bit faster.
ClosedPublic

Authored by zturner on May 24 2017, 9:38 AM.

Details

Summary

Previously it would do a character by character search for a null terminator, to account for the fact that an arbitrary stream need not store its data contiguously so you couldn't just do a memchr. However, the stream API has a function which will return the longest contiguous chunk without doing a copy, and by using this function we can do a memchr on the individual chunks. For certain types of streams like data from object files etc, this is guaranteed to find the null terminator with only a single memchr, but even with discontiguous streams such as MappedBlockStream, it's rare that any given string will cross a block boundary, so even those will almost always be satisfied with a single memchr.

This optimization is worth a 10-12% reduction in link time (4.2 seconds -> 3.75 seconds)

Diff Detail

Event Timeline

zturner created this revision.May 24 2017, 9:38 AM
This revision was automatically updated to reflect the committed changes.
rnk edited edge metadata.May 25 2017, 2:32 PM

Hm, did my comment not make it through?

llvm/lib/Support/BinaryStreamReader.cpp
52–54

How about S = S.take_front(strnlen(S.data(), S.size()))? There are some neat tricks for making strlen fast that don't work for memchr.