Live data from Hacker News

Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

bitbucket.org

61–70 of 130 posts

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#61

It takes quite some amount of stamina to keep on reading after "The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable"

I can see why you'd say that that way but the author probably feels that way because of the domain they work in. I've encountered research types who just use bigint everywhere because it's safer and they don't worry about the perf.

When I was coming up as a game programmer no one would use boost shared_ptr and weak_ptr because of perceived issues with it. Everyone rolled their own handle system.

In fact there were a lot of people that considered languages beside C/C++ untouchable due to perf. They couldn't fathom there were domains where that perf just wasn't that important.

I now work a lot with Unity and find people feel the same about C# foreach because it used to trigger an allocation.

You'll find people often make the mistake of thinking their domain specific problem is a problem for everyone.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#62
Because of the field I work in I feel this much more keenly with floats. I have been bitten by their excentricities so many times I'd just much rather have a flexible fixed point type that let me choose how much precision I want on either side of the decimal for a given set of calculations.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#63
post #41
post #27

Earlier quoted context omitted.

But that performs very poorly on all architectures.

it does not have to. A smart compiler can narrow it down to machine size integers then possible.

In many situations yes, and C/C++ compilers miss a lot of opportunities for constant folding/propagation. But still, an infinitely smart compiler can’t prove anything about a value that comes from a file, user input or socket at runtime. If control flow depends on those (which is true of every useful real-life program) then you’re out of luck. GMP arbitrary-size integers are amazingly fast but still much slower than native instructions, can’t be vectorized, require heap allocation, etc.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#64

It takes quite some amount of stamina to keep on reading after "The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable"

I can see why you'd say that that way but the author probably feels that way because of the domain they work in. I've encountered research types who just use bigint everywhere because it's safer and they don't worry about the perf. When I was coming up as a game programmer no one would use boost shared_ptr and weak_ptr because of perceived issues with it. Everyone rolled their own handle system. In fact there were a…

[deleted]

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#65
post #30

At this point, when I see libraries like this all I can think is ”oh good, my compile times aren’t long enough, lets make EVERY INTEGER a template”. Also, while I can reasonably beliveve that most of the overhead goes away at -O2 or -O3, this has to just trash performance for debug builds, which is not unimportant.

I don't think you even looked at the library, but from I can tell, debug perf would not be impacted at all.

Of course it will be impacted. Libraries like these are "zero-cost" only because of very aggressive inlining (not generally available in debug builds), otherwise all its operations happen in subroutines. Operator overloading is the most obvious example: for regular integers, addition is basically a single instruction even in debug, but with a library like this, it has to make a subroutine call. A call which has who knows how many layers of abstraction that compiler optimizations hasn't shaken off.

In fact, the README says as much: it has only zero time/space overhead "assuming basic compiler optimizations like inlining".

And I did actually look at the code, but at a glance I couldn't get much sense of it. The main header imports like two dozen other headers, and the "detail" folder is filled to the brim with more headers. I tried cloning and using it, but I couldn't get it to compile (which is probably mostly my fault, but I really didn't want to spend too much time on it). I did run just the preprocessor though, and "#include " expanded to 48000 lines. 48000 extra lines to compile in order to use integers. For every file you use it in.

I don't want to be too harsh here, I'm sure it's an excellent library and it does what it says brilliantly, and if you need bounded integers I'm sure it's awesome. But language like "[integers in C++] are mostly unusable" rankles a bit, and the the implication in this file is that this is something you should regularly use instead of integer types.

I personally feel that libraries like this are taking C++ in the wrong direction ("ranges" is another obvious example) and making the language less and less usable in my profession.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#66
post #59
post #55

Earlier quoted context omitted.

Why not use a debug release build. Like O1 with debug symbols.

We absolutely do, but it’s significantly harder to debug. Functions are inlined, variables are eliminated, loops unrolled, etc. But yeah, very often it’s basically the only option, running an optimized build with debug symbols. This is why it’s such a problem with C++: debug builds are frequently pointless because the performance is so bad, which makes debugging a lot harder.

Yeah that makes sense. There's no good alternative in C++, thanks to the mess created by all the zero-cost abstractions that are very much not zero-cost without a the optimizations of a release build.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#67
post #18

Earlier quoted context omitted.

Or Lisp’s approach of just using ”big” ints by default (which is practically the same but with an implicit default of “infinity”). And trust the compiler to use fixints internally when the programmer has declared it safe to do so.

I just finished writing a custom FIFO allocator for an extremely high bandwidth and low latency data processing (and UI) system written in C/C++ (even std::deque was doing WAY too many heap allocations, not to mention the allocations within each object passing through the system, despite use of move semantics to minimize redundancy). Performance improved by 100x - 1000x . And it was already blazingly fast before, if…

If you are doing high-throughput, and you ever allocate anything after startup, you are Doing It Wrong. Any brand of FIFO loses.

What you need is a big-ass ring buffer, mmapped on a hugetlbfs, fed by a process on a NOHZ isolcpu core. Readers are separate processes.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#68
post #45
post #36

Earlier quoted context omitted.

Have you measured? Also, what do you mean by "this has to just trash performance for debug builds". Do you mean runtime performance?

Yes, that’s what I mean. And before you say ”debug performance doesn’t matter”: yes it does. I work in gamedev and it’s a huge problem that C++ has such awful debug performance, because it’s hard to debug a game if it’s running in single-digit framerates. It’s a large part of the reason why EASTL is so popular in gamedev.

I feel like that's mostly Microsoft's fault, their STL implementation is super aggressive about checks in debug builds to the point that it becomes sort of useless in heavy use/high performance scenarios. Between that and the lack of valgrind, I'm pretty glad I left that world.

Over in Linux/g++ land, it's not nearly so bad. Bounds errors are mostly handled by address sanitizer such that -O0 performance is decent. And if -O0 is too slow, there's -Og which takes slightly longer to compile and has less information for gdb.

I work almost exclusively in "g++ -O0" until it's time to release.

Clang is decent too, but I find the C++ compile times are atrocious, like almost double for my admittedly template-heavy code. And performance of the compiled output is no better.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#69

Because of the field I work in I feel this much more keenly with floats. I have been bitten by their excentricities so many times I'd just much rather have a flexible fixed point type that let me choose how much precision I want on either side of the decimal for a given set of calculations.

Doesn't COBOL do this?

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#70
post #20

Earlier quoted context omitted.

.... However, unless you use non portable intrinsics, you can't make the compiler use MMX. So in practice you get wrap on overflow. Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping.

> Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping. C89 (draft[1], because the actual spec is not public) section 3.1.2.5 states: > ... a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type. So, unless I'm misunderstanding, t…

signed and unsigned have different semantics.

The compiler is allowed to do literally anything on signed overflow, including launching the missiles, and not running the program. Or both.

Post reply on HN