[llvm-dev] Improvements to std::find and std::count

Daniel Cooke via llvm-dev llvm-dev at lists.llvm.org
Sat Apr 29 07:13:02 PDT 2017


Hi,

I recently posted a question on StackOverflow regarding the performance of std::find and std::count when provided with char* input: http://stackoverflow.com/questions/43483378/why-arent-stdcount-and-stdfind-optimised-to-use-memchr <http://stackoverflow.com/questions/43483378/why-arent-stdcount-and-stdfind-optimised-to-use-memchr>.

I would propose adding overloads of these functions for char* and const char* inputs that delegate to memchr, something like:

inline const char* find(const char* first, const char* const last, const char value)
{
	const auto result = std::memchr(first, value, last - first);
	return result != nullptr ? static_cast<const char*>(result) : last;
}

inline typename std::iterator_traits<const char*>::difference_type
count(const char* first, const char* const last, const char value)
{
	typename std::iterator_traits<const char*>::difference_type result {0};
	while (first && first != last) {
		if ((first = static_cast<const char*>(std::memchr(first, value, last - first)))) {
			++result;
			++first;
		}
	}
	return result;
}

I’ve never contributed to LLVM, so I’m not sure how to proceed, if this is a change that is likely to be accepted?

Best,
Dan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170429/3c1f280c/attachment-0001.html>


More information about the llvm-dev mailing list