Repository navigation
Optimize the newly-changed heap update operation - #63
Conversation
| // No items removed, so we haven't broken any heap invariants. | ||
| // If we don't have too many items to add, just heap.Push them | ||
| // and return. | ||
| threshold := newLen / bits.Len(uint(newLen)) |
There was a problem hiding this comment.
We could possibly avoid this computation, too, with:
| threshold := newLen / bits.Len(uint(newLen)) | |
| if len(newMap) == 0 { | |
| return | |
| } | |
| threshold := newLen / bits.Len(uint(newLen)) |
Thoughts?
FWIW, I haven't done any benchmarks. This code change is just from first principles, so maybe not worth adding (without actually benchmarking 🤷).
There was a problem hiding this comment.
Seems fine to me. I mean, as far as I can tell, it's trivially correct anyway. I don't think it makes this much worse.
There was a problem hiding this comment.
Thinking about this more, this is probably overkill.
Even if there were 1000s of backends and no subsetting, we're still only talking about infrequent (only when the resolver updates) added latency of a few microseconds, for RPCs waiting for this to complete so they can acquire the lock. I suspect lock contention would generally be a much bigger issue for throughput than this proposed micro-optimization, especially since we can't use a reader-writer lock -- all RPCs must modify the heap on acquisition.
¯\_(ツ)_/¯
There was a problem hiding this comment.
Definitely seems reasonable to me. I mean, I think the real answer is that it won't matter very much in practice because I don't think it's likely this heap will get very large, but avoiding a full re-heapify seems like the canonically right thing to do where possible.
I thought about mentioning this in the other PR, but you really could go either way IMO. Since you already did the work and it passes tests, I'm in favor of going for it. That said, it definitely re-affirms my desire for us to have load tests for all of the complicated stuff we are doing here. I think the odds we'll make the code so obviously correct that it never breaks when changing it is low; I'm kinda astounded that I didn't notice the heap panic issue in the first place, but it just goes to show you how easy it is to get something wrong and not notice. This change is relatively minor in the grand scheme of things and I don't think it makes the code vastly more complex.
Is this worth it? Maybe not. The thought here is that we avoid the heap re-init in many cases. WDYT?