Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline the common case of link for Set and Map #1092

Open
meooow25 opened this issue Jan 14, 2025 · 1 comment
Open

Inline the common case of link for Set and Map #1092

meooow25 opened this issue Jan 14, 2025 · 1 comment

Comments

@meooow25
Copy link
Contributor

I'm seeing some decent improvements in union, difference, etc. on inlining the no-rebalance Bin-Bin case of link

link :: k -> a -> Map k a -> Map k a -> Map k a
link kx x Tip r = insertMin kx x r
link kx x l Tip = insertMax kx x l
link kx x l@(Bin sizeL ky y ly ry) r@(Bin sizeR kz z lz rz)
| delta*sizeL < sizeR = balanceL kz z (link kx x l lz) rz
| delta*sizeR < sizeL = balanceR ky y ly (link kx x ry r)
| otherwise = bin kx x l r

This is just like #1053, that the common case is one where no balancing is required, and making sure that's fast pays off.

I'll prepare a PR for it.

While I'm here, I'm going to change link to delegate to left-only and right-only variants of link. This better represents what's going on, i.e. link never switches directions. I've thought about this but didn't have a good reason to touch the code before.

@meooow25
Copy link
Contributor Author

This isn't a big win as I first thought.

There are significant improvements (20-30%) but mostly in benchmarks where the two trees don't have a large intersection (and thus the operation is pretty fast anyway), and less (3-5%) otherwise. I'll try to understand this better before making a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant