News for hackers. Stuff that matters.
Re:Let’s Invent B(+)-Trees by afc on Monday April 27, 2020 @08:45PM (#23002047)
Interesting. :-)

I recently posted a brief document I wrote about a similar structure, immutable AVL trees. I find AVL trees a very nifty structure, I think they don't get as much credits as they deserve: logn (which in practice is about the same as "constant" for most values of n one encounters in practice) insertion, lookup, deletion and even append (for position-based trees, with some assumptions). Incredibly simple implementation. And, if immutable (based on shared memory), allows trivial snapshotting, having multiple "concurrent" trees sharing most of their memory.

Anyhow, it's here: https://github.com/alefore/weblog/blob/master/immutable-avl-...

I have a small implementation here (which I use, among other things, to hold all lines in a file, in my text editor): https://github.com/alefore/edge/blob/master/src/const_tree.h

Re:Let’s Invent B(+)-Trees by jiggawatts on Monday April 27, 2020 @09:05PM (#23002183)
All node-based trees with a small branching factor of 2-3 have the same performance problems on modern hardware, irrespective of the theoretical big-O algorithmic complexity.

They all have poor memory locality, high overheads, and high indirection. This is kryptonite for typical x86 or ARM CPUs. Unless you're only developing for some tiny embedded CPU with no cache, no branch predictor, and low frequency, such algorithms belong in the dustbin of history.

Practically always, you get better actual performance when using array-based structures such as hashtables, B-Trees, or the like.

Features like snapshotting can be implemented using virtual memory tricks, but is a gimmick rarely used outside of pure functional languages. You might find that simply copying an array when needed for a snapshot is faster than a fancy tree with sharing as a native capability. The exception would be certain dynamic programming scenarios where cloning is very common.

Re:Let’s Invent B(+)-Trees by magicalhippo on Monday April 27, 2020 @10:17PM (#23002632)
The traditional ZFS implementation(s) use AVL trees as the in-memory data structure for allocating space (ie finding free disk space).

However the performance limitations of them have started to show, so there's been work done to switch to B-trees[1].

[1]: https://www.youtube.com/watch?v=LZpaTGNvalE

Re:Let’s Invent B(+)-Trees by afc on Monday April 27, 2020 @09:31PM (#23002348)
Thank you for your reply. Interesting points.

I guess I'll implement an analogous immutable B-Tree structure and run some benchmarks. I suspect you're probably right. I'll probably experiment with different branching factors and tree sizes.

I rely a lot on the trivial (i.e., zero cost) snapshotting for feeding work to background threads. For my workload, having to do deep copies constantly would be prohibitively expensive (and I'd rather not deal with the complexity of explicit locking). That said, I'm now curious to see whether using immutable B-Trees will yield significantly better performance. I suspect they likely will. Exciting. :-)

Thanks again!

Re:Let’s Invent B(+)-Trees by kccqzy on Monday April 27, 2020 @09:12PM (#23002226)
I don't know about you, but when I started learning data structures, my professor told me AVL trees lost the popularity battle with red-black trees, and people tended to use red-black trees more. He didn't mention AVL trees besides a footnote. Nowadays even red-black trees aren't favored due to practical concerns like caches, so I assume AVL trees are very niche.
Re:Let’s Invent B(+)-Trees by why-el on Monday April 27, 2020 @09:20PM (#23002270)
> Nowadays even red-black trees aren't favored due to practical concerns like caches

Not sure if that's true. In addition to the main linux trunk using them, Java 8 included them also as an improvement to their HashMap (I found this by googling), so I don't they are not favored anymore.

Edit: language.

Re:Let’s Invent B(+)-Trees by revertts on Monday April 27, 2020 @09:43PM (#23002416)
There are cases where you need them, but it's relatively rare, they definitely shouldn't be your first choice.

I'm fuzzy on all the current Linux use cases, but do remember one of the main users of rb trees was CFS. It's a neat scheduling algorithm.

Java 8 specifically: HashMap is implemented with linked list chaining. This is already not very performant, but you can only do so much with Java being a reference heavy language. RB trees are used if the bucket chain grows excessively long - so it's addressing an edge case, speeding up some worst case scenarios.

Dense hash tables based on open addressing outperform bucketed chaining. Look also at abseil's swiss table or folly's f14 if you want to see how they've further advanced to take advantage of the hardware.

In general: flat, dense, linear structures are king for performance.

Re:Let’s Invent B(+)-Trees by xxs on Tuesday April 28, 2020 @02:24AM (#23004042)
>you can only do so much with Java being a reference heavy language.

This is definitely not true, implementing array based hashtable is trivial and outperforms in most cases java.utl.HashMap. It's just the decision to have LinkedHashMap (in java 1.2) extending HashMap crippled the latter. The issue has been discussed quite a few times in java core mailing list.

Re:Let’s Invent B(+)-Trees by revertts on Tuesday April 28, 2020 @08:07AM (#23006195)
I agree, HashMap is unnecessarily hamstrung because the API requirements push it into a bucket chained implementation (C++ also made this mistake and is one of the downsides of std::unordered_map). And you can definitely write a faster implementation with an array based hashtable.

But I still stand by "you can only do so much being a reference heavy language." Unless you stick to purely primitive types, implement the hash table off heap, or Project Valhalla bears fruit, it's hard to get the data layout you'd want for a really good implementation. So I agree it can be better, but it's going to be hard to get to best - hence my comment.

Re:Let’s Invent B(+)-Trees by xxs on Tuesday April 28, 2020 @08:32AM (#23006343)
I do agree data layout is hard - pretty much direct byte buffers (off heap) and some poor man's memory manager (plus serialization/desirilization code).

Project Valhalla and structs is something people have been asking for since Java 1.4 or so. Still nowhere near. And indeed, "best" would take a custom implementation, case by case. It's doable, but usually very far from pretty.

Re:Let’s Invent B(+)-Trees by why-el on Tuesday April 28, 2020 @12:12AM (#23003281)
I was not commenting on whether one should use it or not, just that it is still used and picked as recently as Java 8. We are in agreement for the rest of your post. :+1:
Re:Let’s Invent B(+)-Trees by revertts on Tuesday April 28, 2020 @08:11AM (#23006220)
Ah! I'm sorry, I had misinterpreted your original comment.
Re:Let’s Invent B(+)-Trees by xxs on Tuesday April 28, 2020 @02:22AM (#23004032)
The improvement of hashmap applies only to high collision nodes that also implement comparable. It's a very special case. java.util.TreeMap has always been a red/black one.

On a flip note: HashMap in Java is sort of jack of trades, it's node based which means it has poor memory/caching characteristics - around 36bytes per standard node (compared to ~10 for array backed one) - and Nodes+array tend to be top3 objects in heap dumps. The iteration is sort of 'random' (unlikely LinkedHashMap) which has been a source of lots of issues not manifesting during testing. However, it doesn't degrade in virtually any use case. Normally, I don't use it - either a custom one (CompactHashMap), LinkedHashMap (being the go to hashtable), or ConcurrentHahshMap.

Re:Let’s Invent B(+)-Trees by pseudoramble on Monday April 27, 2020 @10:18PM (#23002647)
Thanks for sharing! I don’t remember C++ well at all anymore, but it’s cool to see an immutable AVL tree in it since it seems like it would be less common. I wrote one in F# a while back for fun/curiosity since I had no idea how it would be done at the time. Mine was purely key based though. The indexing idea is neat too!