#Help me optimize this loop

25 messages · Page 1 of 1 (latest)

pseudo panther
#
  1. how are you measuring it
  2. how can you tell that's slow?
#

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

leaden kestrel
#

I am pedantic af but I'd add a linebreak before the if let

pseudo panther
#

yes, I would too

#

it copied wrong

leaden kestrel
#

Discord 💢

pseudo panther
#

nah, it's playground

#

with block select

leaden kestrel
#

it's fun to blame discord for everything though right?

quaint tiger
#

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. ferrisSmirk

pseudo panther
#

like the ```rs
}
true

leaden kestrel
#

I'd put a break there too

pseudo panther
#

great thread that's totally on-topic

leaden kestrel
#

sorry

pseudo panther
#

well, OP hasn't yet responded, so it's not a big deal

opaque slate
#

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

leaden kestrel
#

oh yeah printing to console slows shit down an insane amount

opaque slate
#

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

pseudo panther
#

at least some kind of synchronization is required