Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pgdog/src/backend/pool/lb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ impl LoadBalancer {
async fn get_internal(&self, request: &Request) -> Result<Guard, Error> {
use LoadBalancingStrategy::*;
use ReadWriteSplit::*;
use smallvec::SmallVec;

let mut candidates: Vec<&Target> = self
let mut candidates: SmallVec<[&Target; 32]> = self
.targets
.iter()
.filter(|target| !target.pool.config().resharding_only) // Don't let reads on resharding-only replicas.
Expand Down Expand Up @@ -384,7 +385,7 @@ impl LoadBalancer {
Random => candidates.shuffle(&mut rand::rng()),
RoundRobin => {
let first = self.round_robin.fetch_add(1, Ordering::Relaxed) % candidates.len();
let mut reshuffled = vec![];
let mut reshuffled = SmallVec::with_capacity(candidates.len());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Literally had to look up the code to understand that it infers the size of the new vec from the type of the original. This is legit a cool lib and a really cool use of the type system.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's wild that they managed to make this work before const generics (2.0 uses const generics but it's still alpha)

reshuffled.extend_from_slice(&candidates[first..]);
reshuffled.extend_from_slice(&candidates[..first]);
candidates = reshuffled;
Expand Down
Loading