#Help me optimize this loop
25 messages · Page 1 of 1 (latest)
I tried rewriting it in more idiomatic rust: ```rs
pub fn check_history2(&mut self, cid: u64, status: STATUS) -> bool {
let record = self
.history
.iter_mut()
// .filter_map(|record| record.as_mut())
.find(|record| {
record
.as_ref()
.map(|record| record.cid == cid)
.unwrap_or(false)
});
if let Some(record) = record {
if status == STATUS::A || status == STATUS::B {
*record = None;
} else {
record.as_mut().unwrap().status = STATUS::C;
self.cancel_cid(cid);
}
true
} else {
false
}
}``` it almost works with a simple filter_map, but the *record = None doesn't allow that
🤌
I am pedantic af but I'd add a linebreak before the if let
Discord 💢
it's fun to blame discord for everything though right?
The playground only copies a few lines when I try to copy the entire file, unless I switch to the basic editor first.
I prefer paragraph breaks like that in my code too, though it leads to needless wondering about where not to put them in. 
like the ```rs
}
true
I'd put a break there too
sorry
well, OP hasn't yet responded, so it's not a big deal
hmm, it might be my logging library
I tried my implementation on playground and got around 200ns for the loop to complete, which is roughly what i was expecting
oh yeah printing to console slows shit down an insane amount
i'm using the tracing crate, which should spawn its own write thread for each print statement so it shouldn't affect the timing too much
but i guess may be there's some kind of locking that happens, every time i print
that causes the slowdown
at least some kind of synchronization is required
