Live data from Hacker News

How We Beat C++ STL Binary Search

realm.io

41–50 of 52 posts

Re: How We Beat C++ STL Binary Search

#41
post #10

Earlier quoted context omitted.

Not if low and high are both unsigned (size_t is unsigned.) Even if it overflows, the result will be correct. See: http://googleresearch.blogspot.com/2006/06/extra-extra-read-...

That blog is wrong. The program won't be undefined( unsigned wrap is defined ), but the offset will not be correct.

So the article isn't wrong, but its misleading you into thinking this should be correct for the wrong reasons.

They are using signed integers as their indices, which means that the signed bit is always 0. Thus the addition after casting to unsigned will never overflow, and you can divide by two (shift by 1) and then recast to a signed integer, no harm no foul.

Re: How We Beat C++ STL Binary Search

#42
post #23
post #7

There is no mention of the fact that the STL implementation is very generic, it only assumes operator++ and operator* on the iteration, and operator The "optimized" versions here all make more assumptions on the iterator, starting from operator+(int) in Version 1, so it no longer works on iterators with just "forward_iterator_tag". Further versions even restrict vector sizes (albeit to a very high number) and assign…

Just a nitpick: Assigning a negative value to an unsigned integer is perfectly safe and well defined (i.e. integer overflow for unsigned integers is well defined in the C++ spec). Using -1 for size_t is sometimes even prefered to assign it's maximum value, since there is no cross-platform #define for it (most use either SIZE_T_MAX or SIZE_MAX). In fact llvm's libc++ uses it for it's std::numeric_limits implementation…

~0u ?

Re: How We Beat C++ STL Binary Search

#43
This is nice but what are the use cases for such an optimization ? My understanding is that such an optimization will not matter anyway compared to all the I/O and other expensive operations. And if a program is really too slow then the true optimization is to scale with threads or distributed computing.

Re: How We Beat C++ STL Binary Search

#44

Earlier quoted context omitted.

That blog is wrong. The program won't be undefined( unsigned wrap is defined ), but the offset will not be correct.

That's not true. Unsigned wrap is well-defined and wraps around to 0. Signed wrap is undefined.

It is not true that unsigned wrap is defined? Did you even read my comment, here is the relevant part: unsigned wrap is defined

Re: How We Beat C++ STL Binary Search

#45
post #41

Earlier quoted context omitted.

That blog is wrong. The program won't be undefined( unsigned wrap is defined ), but the offset will not be correct.

So the article isn't wrong, but its misleading you into thinking this should be correct for the wrong reasons. They are using signed integers as their indices, which means that the signed bit is always 0. Thus the addition after casting to unsigned will never overflow, and you can divide by two (shift by 1) and then recast to a signed integer, no harm no foul.

Actually it is misleading by them( and you ) to assume that in C, an unsigned int can represent values larger than the largest signed int value.

In other words, C allows that UINT_MAX == INT_MAX, in which case you will overflow.

If they made that assumption, they should explicitly mention it, but they didn't.

> Update 17 Feb 2008:... ...Now that we've made this change, we know that the program is correct;)

It seems the article is aware of the irony. Another update would be in order.

Re: How We Beat C++ STL Binary Search

#47
post #41

Earlier quoted context omitted.

So the article isn't wrong, but its misleading you into thinking this should be correct for the wrong reasons. They are using signed integers as their indices, which means that the signed bit is always 0. Thus the addition after casting to unsigned will never overflow, and you can divide by two (shift by 1) and then recast to a signed integer, no harm no foul.

Actually it is misleading by them( and you ) to assume that in C, an unsigned int can represent values larger than the largest signed int value. In other words, C allows that UINT_MAX == INT_MAX, in which case you will overflow. If they made that assumption, they should explicitly mention it, but they didn't. > Update 17 Feb 2008:... ...Now that we've made this change, we know that the program is correct;) It seems t…

[deleted]

Re: How We Beat C++ STL Binary Search

#48
post #32
post #23

Earlier quoted context omitted.

Just a nitpick: Assigning a negative value to an unsigned integer is perfectly safe and well defined (i.e. integer overflow for unsigned integers is well defined in the C++ spec). Using -1 for size_t is sometimes even prefered to assign it's maximum value, since there is no cross-platform #define for it (most use either SIZE_T_MAX or SIZE_MAX). In fact llvm's libc++ uses it for it's std::numeric_limits implementation…

I believe there is a standard cross-platform maximum value, and you mention it in your last sentence: std::numeric_limits!

Uhm... Using "-1" to assign the maximum value is in fact as standard and cross-platform as using std::numeric_limits, which was basically my entire point.

The difference between those is only "textual solution" VS "mathematical solution" and thus a matter of personal preference.

Re: How We Beat C++ STL Binary Search

#50
post #41

Earlier quoted context omitted.

So the article isn't wrong, but its misleading you into thinking this should be correct for the wrong reasons. They are using signed integers as their indices, which means that the signed bit is always 0. Thus the addition after casting to unsigned will never overflow, and you can divide by two (shift by 1) and then recast to a signed integer, no harm no foul.

Actually it is misleading by them( and you ) to assume that in C, an unsigned int can represent values larger than the largest signed int value. In other words, C allows that UINT_MAX == INT_MAX, in which case you will overflow. If they made that assumption, they should explicitly mention it, but they didn't. > Update 17 Feb 2008:... ...Now that we've made this change, we know that the program is correct;) It seems t…

Sure, and if you expect your software to run on such a platform with any degree of confidence then you're right to consider it. Even better, tell us about a conforming implementation that you've used in product recently that has UINT_MAX == INT_MAX.

Also I'm not trying to mislead people into thinking that its a good way to implement this. The confounding bit from the article is they started in Java and ended up in C. If you were indexing with signed ints in C, C++, or any language that has unsigned integers then you already have a bug with or without the bad mean check.

Post reply on HN