Skip to content

Optimize the newly-changed heap update operation - #63

Merged
jhump merged 1 commit into
mainfrom
jh/optimize
Apr 19, 2024
Merged

jhump merged 1 commit into
mainfrom
jh/optimize

Conversation

@jhump

@jhump jhump commented Apr 19, 2024

Copy link
Copy Markdown
Member

Is this worth it? Maybe not. The thought here is that we avoid the heap re-init in many cases. WDYT?

@jhump
jhump requested a review from a user April 19, 2024 14:12
Comment thread picker/leastloaded.go
// 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))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could possibly avoid this computation, too, with:

Suggested change
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 🤷).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jhump jhump Apr 19, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

¯\_(ツ)_/¯

@ghost ghost left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jhump
jhump merged commit e1514ae into main Apr 19, 2024
@jhump
jhump deleted the jh/optimize branch April 19, 2024 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant