priroda: Source rendering is really slow

It is responsible for more than ~90% of cpu time during single stepping through liballoc.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19

Commits related to this issue

Most upvoted comments

When the cache is hit, source rendering now requires ~3ms 😃

Yay nice! This project looks pretty cool, good luck with it!

I’ll try and remember to tag you when I add the helper to syntect so you can remove the code from your project if you want, not that it would make any difference. Thanks for the digging about extend, I’ll switch to that.

Oh lol I wrote one almost immediately after I wrote that comment but didn’t submit it since I hadn’t written tests yet, sorry. Looks like we did it in almost exactly the same way, although I haven’t tested if mine compiles or works yet:

/// Split a highlighted line at a byte index in the line into a before and
/// after component. It's just a helper that does the somewhat tricky logic
/// including splitting a span if the index lies on a boundary.
///
/// This can be used to extract a chunk of the line out for special treatment
/// like wrapping it in an HTML tag for extra styling.
pub fn split_at(mut v: &[(Style, &str)], mut split_i: usize) -> (Vec<(Style, &str)>, Vec<(Style, &str)>) {
  // Consume all tokens before the split
  let before = Vec::new();
  for tok in v {
    if tok.1.len() > split_i {
      break;
    }
    before.push(tok.clone());
    split_i -= tok.1.len();
  }
  v = &v[before.len()..];

  let after = Vec::new();
  // If necessary, split the token the split falls inside
  if v.len() > 0 && split_i > 0 {
    let (sa, sb) = v[0].1.split_at(split_i);
    before.push((v[0].0.clone(), sa));
    after.push((v[0].0.clone(), sb));
    v = &v[1..];
  }

  after.extend_from_slice(v);

  return (before, after);
}

Anyway, I’m going to try to get around to contributing it in a PR to syntect soon, along with some tests and a higher level helper that uses it to apply a StyleModifier to a range of a line.