3 ms·
There is another alternative which may be significantly more efficient as it means the caller always gets back a direct iterator instead of an enum it must bran
by orlp 2y ago
There is another alternative which may be significantly more efficient as it means the caller always gets back a direct iterator instead of an enum it must branch on: make a dummy empty hashmap with a static lifetime you can refer to. Then the implementation becomes
static DUMMY: LazyLock<HashMap<N, (u128, u128)>> = LazyLock::new(HashMap::new());
fn neighbors_with_send_capacity_option(&self, a: N, capacity: u128) -> impl Iterator<Item=&N> {
let node = self.nodes.get(&a).unwrap_or_else(|| &*DUMMY);
node.keys().filter(move |b| self.get_send_capacity(&a, b) >= capacity)
}
Unfortunately we have to use a LazyLock for the static because HashMap::new isn't const. It only adds a single always-correctly-predicted branch that checks for initialization on access though, so it's fine.