News for hackers. Stuff that matters.
Re:Fast function to parse strings into double (binary64) flo... by voldacar on Tuesday March 10, 2020 @11:18PM (#22542652)
That's so true and honestly it kind of gives me a pit in my stomach just thinking about all the C++ programs in particular that use STL and end up being so much slower than they could be. Hopefully Rust and Zig won't end up making this kind of mistake
Re:Fast function to parse strings into double (binary64) flo... by pjscott on Wednesday March 11, 2020 @12:47AM (#22543070)
The most egregious offenders here IMO are C++'s map and unordered_map. The spec constrains the possible implementations, and it's hard to implement them as anything better than a red-black tree or a badly-unoptimized hash table, respectively. And really the unordered one should be the default with the nice short name.

It looks like Rust is doing well on this. HashMap (the one everybody uses) is essentially a clone of Google's heavily-optimized C++ hash table:

https://abseil.io/blog/20180927-swisstables

... and if you need the collection to be sorted, their BTreeMap has much lower overhead than the C++ std::map, plus a big prominent note in its documentation saying that HashMap is usually the one you want.

I'm impressed. (Haven't looked at Zig yet.)

Re:Fast function to parse strings into double (binary64) flo... by scottlamb on Wednesday March 11, 2020 @02:29AM (#22543565)
> It looks like Rust is doing well on this. HashMap (the one everybody uses) is essentially a clone of Google's heavily-optimized C++ hash table

It didn't start out this way, but their spec was not so constrained as C++, so they were able to switch to this implementation in a backward-compatible way. In particular, Rust's std::collections::HashMap doesn't guarantee that keys and values have stable addresses across mutations of unrelated entries, so they can use open addressing rather than chaining. (And they did use open addressing from the beginning; the SwissTables-like "hashbrown" implementation was just a refinement of that.)

I think a lot of this is just that newer languages can learn from the mistakes of previous languages. It will be interesting to see how Rust (and other newer languages) are able to evolve when their standard library is 20+ years old and some parts don't age so well. (Although actually C++'s std::unordered_map only goes back to C++11 and still sucks...)

One part of Rust's answer I think is to keep the standard library small. Less there, less to screw up. Make it easy to pull in crates instead. Crates can supply the same functionality but can bump their major version relatively easily if the interface has to change. There are advantages and disadvantages to this approach...

Re:Fast function to parse strings into double (binary64) flo... by saagarjha on Wednesday March 11, 2020 @09:04AM (#22545449)
The default hash map probably doesn't need the iterator invalidation constraints, but they are quite useful in certain cases.
Re:Fast function to parse strings into double (binary64) flo... by RossBencina on Wednesday March 11, 2020 @01:56AM (#22543399)
> anything better than a red-black tree

Is there an equivalent data structure that has better worst-case time complexity than red-black tree?

Re:Fast function to parse strings into double (binary64) flo... by Dylan16807 on Wednesday March 11, 2020 @02:09AM (#22543466)
There are plenty of algorithms that are also O(logn) that are much faster in practice, because of how caches work.
Re:Fast function to parse strings into double (binary64) flo... by RossBencina on Wednesday March 11, 2020 @04:16AM (#22544064)
Could you name some please? I'm genuinely interested in learning of data structures with this property.
Re:Fast function to parse strings into double (binary64) flo... by Dylan16807 on Wednesday March 11, 2020 @05:39AM (#22544383)
The simplest one is a B tree. It's like a self-balancing binary tree, except that you put more than two children in each node.

Same big-O, but faster by a big linear multiplier.

Re:Fast function to parse strings into double (binary64) flo... by vardump on Wednesday March 11, 2020 @06:04AM (#22544497)
If the data is small enough (up to about n=100 magnitude), linear scan O(n) often beats asymptotically faster algorithms.
Re:Fast function to parse strings into double (binary64) flo... by LessDmesg on Wednesday March 11, 2020 @02:50AM (#22543674)
What, C++'s spec constrains implementation of standard datastructures? This language has hit a new low for me.
Re:Fast function to parse strings into double (binary64) flo... by hoseja on Wednesday March 11, 2020 @03:43AM (#22543908)
Well it defines the big-O of the datastructure APIs. And to match all the requirements, the implementation is usually very constrained.
Re:Fast function to parse strings into double (binary64) flo... by leni536 on Wednesday March 11, 2020 @04:18AM (#22544073)
My impression is that it's typically the iterator/reference stability requirements that lock imlementations down, not the complexity requirements.
Re:Fast function to parse strings into double (binary64) flo... by hoseja on Wednesday March 11, 2020 @05:25AM (#22544320)
Oh yeah, that too.
Re:Fast function to parse strings into double (binary64) flo... by KMnO4 on Tuesday March 10, 2020 @11:41PM (#22542771)
I think Rust has an intrinsic advantage simply in its power. Standard libraries (and really most code) are written by humans. Giving those humans the confidence to implement complex data structures and algorithms without worrying about null pointers or memory leaks has ripping effects on performance. See C’s (“simple”) AVL tree vs Rust’s B-Tree[0].

[0]: http://dtrace.org/blogs/bmc/2018/09/28/the-relative-performa...