Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make highlight_line() and parse_line() return Result #426

Merged

Conversation

Enselic
Copy link
Collaborator

@Enselic Enselic commented Mar 1, 2022

And also adapt most other related code to this change, and add a Result to another html function.

  • In examples/ and benches/ I simply do .unwrap()

  • In tests I do .expect("#[cfg(test)]") to make reviewing the diff easy

  • In deprecated methods I simply do .expect("helpful hint about new method")

  • In uses_backrefs = uses_backrefs || proto_ids.iter().any(|id|syntax_set.get_context(id).unwrap().uses_backrefs); I do an .unwrap() because ? does not work inside of .any(). But that can be fixed later if necessary without semver breakage.

  • To keep the scope of the PR manageable I do .unwrap() in ContextReference::resolve() and impl<'a> Iterator for MatchIter<'a> to not have to propagate an error further. That will be done in a follow-up PR.

The impact on the public API is as follows:

% cargo install cargo-public-items
% git checkout parse-and-highlight-line-with-result
% cargo public-items > /tmp/parse-and-highlight-line-with-result
% git ch origin/master
% cargo public-items > /tmp/master
% diff -U0 /tmp/master /tmp/parse-and-highlight-line-with-result
+pub enum syntect::parsing::ParsingError
+pub enum variant syntect::parsing::ParsingError::MissingContext(ContextId)
+pub fn syntect::parsing::ParsingError::fmt(&self, __formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+pub fn syntect::parsing::ParsingError::fmt(&self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
+pub enum variant syntect::Error::ParsingError(crate::parsing::ParsingError)
+pub fn syntect::Error::from(source: crate::parsing::ParsingError) -> Self
-pub fn syntect::easy::HighlightLines::highlight_line<'b>(&mut self, line: &'b str, syntax_set: &SyntaxSet) -> Vec<(Style, &'b str)>
+pub fn syntect::easy::HighlightLines::highlight_line<'b>(&mut self, line: &'b str, syntax_set: &SyntaxSet) -> Result<Vec<(Style, &'b str)>, Error>
-pub fn syntect::html::ClassedHTMLGenerator::parse_html_for_line_which_includes_newline(&mut self, line: &str)
+pub fn syntect::html::ClassedHTMLGenerator::parse_html_for_line_which_includes_newline(&mut self, line: &str) -> Result<(), Error>
-pub fn syntect::parsing::ParseState::parse_line(&mut self, line: &str, syntax_set: &SyntaxSet) -> Vec<(usize, ScopeStackOp)>
+pub fn syntect::parsing::ParseState::parse_line(&mut self, line: &str, syntax_set: &SyntaxSet) -> Result<Vec<(usize, ScopeStackOp)>, ParsingError>

Performance-wise I can not detect any significant difference on my low-end desktop

Detailed benchmark numbers with master as baseline
/home/martin/src/syntect
22bccfa Make `highlight_line()` and `parse  parse-and-highlight-line-with-result   (origin/parse-and-highlight-line-with-result)
% cargo bench -- --baseline master-8e1dac8-2                                              
   Compiling syntect v4.7.1 (/home/martin/src/syntect)
    Finished bench [optimized] target(s) in 50.11s
     Running unittests (target/release/deps/highlighting-796b26013708fde3)
stack_matching          time:   [84.523 ns 84.566 ns 84.606 ns]                          
                        change: [-0.4863% -0.0499% +0.3400%] (p = 0.83 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe

highlight_html          time:   [444.85 ms 445.46 ms 446.06 ms]                         
                        change: [-2.1037% -1.6964% -1.3659%] (p = 0.00 < 0.05)
                        Performance has improved.

highlight/"highlight_test.erb"                                                                            
                        time:   [1.7938 ms 1.8068 ms 1.8394 ms]
                        change: [-8.1140% -1.0209% +6.4469%] (p = 0.80 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe
highlight/"InspiredGitHub.tmTheme"                                                                           
                        time:   [25.837 ms 26.086 ms 26.227 ms]
                        change: [-0.2408% +0.8110% +1.8373%] (p = 0.17 > 0.05)
                        No change in performance detected.
Found 3 outliers among 10 measurements (30.00%)
  1 (10.00%) low severe
  1 (10.00%) low mild
  1 (10.00%) high mild
highlight/"Ruby.sublime-syntax"                                                                           
                        time:   [81.408 ms 81.545 ms 81.643 ms]
                        change: [+0.6270% +1.0745% +1.4963%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high severe
Benchmarking highlight/"jquery.js": Warming up for 3.0000 s
Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 6.5s.
highlight/"jquery.js"   time:   [657.62 ms 658.48 ms 659.52 ms]                                
                        change: [-0.8001% -0.5042% -0.2173%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 10 measurements (20.00%)
  2 (20.00%) high mild
highlight/"parser.rs"   time:   [432.96 ms 433.37 ms 433.79 ms]                                
                        change: [-1.6067% -1.2167% -0.8924%] (p = 0.00 < 0.05)
                        Change within noise threshold.
highlight/"scope.rs"    time:   [41.394 ms 41.476 ms 41.656 ms]                                
                        change: [-2.9460% -1.3003% +0.3028%] (p = 0.18 > 0.05)
                        No change in performance detected.
Found 1 outliers among 10 measurements (10.00%)
  1 (10.00%) high mild

     Running unittests (target/release/deps/load_and_highlight-ce9210706bcf7470)
load_and_highlight/"highlight_test.erb"                                                                           
                        time:   [32.436 ms 32.467 ms 32.504 ms]
                        change: [-0.6338% -0.4986% -0.3580%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 50 measurements (6.00%)
  1 (2.00%) high mild
  2 (4.00%) high severe
load_and_highlight/"InspiredGitHub.tmTheme"                                                                           
                        time:   [30.556 ms 30.590 ms 30.625 ms]
                        change: [-1.2091% -1.0046% -0.8205%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 50 measurements (4.00%)
  1 (2.00%) low mild
  1 (2.00%) high severe
load_and_highlight/"Ruby.sublime-syntax"                                                                           
                        time:   [85.098 ms 85.151 ms 85.212 ms]
                        change: [+0.5870% +0.6919% +0.7899%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 50 measurements (6.00%)
  1 (2.00%) high mild
  2 (4.00%) high severe
Benchmarking load_and_highlight/"parser.rs": Warming up for 3.0000 s
Warning: Unable to complete 50 samples in 5.0s. You may wish to increase target time to 22.2s, or reduce sample count to 10.
load_and_highlight/"parser.rs"                                                                          
                        time:   [443.27 ms 443.84 ms 444.44 ms]
                        change: [-1.2440% -1.0908% -0.9243%] (p = 0.00 < 0.05)
                        Change within noise threshold.

     Running unittests (target/release/deps/loading-e67ac9fa9c16eca0)
load_internal_dump      time:   [1.3314 ms 1.3327 ms 1.3339 ms]                               
                        change: [+4.7655% +5.1574% +5.5272%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 4 outliers among 50 measurements (8.00%)
  4 (8.00%) high severe

load_internal_themes    time:   [1.5770 ms 1.5779 ms 1.5789 ms]                                 
                        change: [-0.6704% -0.3949% -0.1488%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 5 outliers among 50 measurements (10.00%)
  1 (2.00%) high mild
  4 (8.00%) high severe

load_theme              time:   [1.9622 ms 1.9630 ms 1.9639 ms]                       
                        change: [+0.6787% +0.9176% +1.1745%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 4 outliers among 50 measurements (8.00%)
  1 (2.00%) high mild
  3 (6.00%) high severe

Benchmarking add_from_folder: Warming up for 3.0000 s
Warning: Unable to complete 50 samples in 5.0s. You may wish to increase target time to 28.3s, or reduce sample count to 10.
add_from_folder         time:   [563.75 ms 563.92 ms 564.11 ms]                          
                        change: [-0.0335% +0.0110% +0.0561%] (p = 0.65 > 0.05)
                        No change in performance detected.
Found 3 outliers among 50 measurements (6.00%)
  2 (4.00%) high mild
  1 (2.00%) high severe

Benchmarking link_syntaxes: Warming up for 3.0000 s
Warning: Unable to complete 50 samples in 5.0s. You may wish to increase target time to 9.8s, or reduce sample count to 20.
link_syntaxes           time:   [195.18 ms 195.29 ms 195.42 ms]                        
                        change: [-0.3727% -0.2915% -0.2134%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 50 measurements (4.00%)
  1 (2.00%) high mild
  1 (2.00%) high severe

from_dump_file          time:   [1.4554 ms 1.4559 ms 1.4566 ms]                           
                        change: [-0.2638% +0.0265% +0.3184%] (p = 0.87 > 0.05)
                        No change in performance detected.
Found 7 outliers among 50 measurements (14.00%)
  2 (4.00%) high mild
  5 (10.00%) high severe

     Running unittests (target/release/deps/parsing-253403872c2ab027)
parse/"highlight_test.erb"                                                                            
                        time:   [1.7797 ms 1.8028 ms 1.8364 ms]
                        change: [-23.191% -2.6599% +24.693%] (p = 0.83 > 0.05)
                        No change in performance detected.
Found 6 outliers among 50 measurements (12.00%)
  2 (4.00%) high mild
  4 (8.00%) high severe
parse/"InspiredGitHub.tmTheme"                                                                           
                        time:   [18.551 ms 18.609 ms 18.670 ms]
                        change: [-0.7899% -0.3712% +0.0706%] (p = 0.10 > 0.05)
                        No change in performance detected.
Found 5 outliers among 50 measurements (10.00%)
  5 (10.00%) high mild
parse/"Ruby.sublime-syntax"                                                                           
                        time:   [77.605 ms 77.653 ms 77.707 ms]
                        change: [-0.3625% -0.2387% -0.1146%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 2 outliers among 50 measurements (4.00%)
  1 (2.00%) high mild
  1 (2.00%) high severe
Benchmarking parse/"jquery.js": Warming up for 30.000 s
Warning: Unable to complete 50 samples in 5.0s. You may wish to increase target time to 29.6s, or reduce sample count to 10.
parse/"jquery.js"       time:   [604.46 ms 604.96 ms 605.45 ms]                            
                        change: [-3.2934% -3.1695% -3.0487%] (p = 0.00 < 0.05)
                        Performance has improved.
Benchmarking parse/"parser.rs": Warming up for 30.000 s
Warning: Unable to complete 50 samples in 5.0s. You may wish to increase target time to 19.7s, or reduce sample count to 10.
parse/"parser.rs"       time:   [400.78 ms 401.24 ms 401.74 ms]                            
                        change: [-4.5650% -4.3539% -4.1440%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 50 measurements (10.00%)
  2 (4.00%) high mild
  3 (6.00%) high severe
parse/"scope.rs"        time:   [39.973 ms 40.007 ms 40.045 ms]                            
                        change: [-4.9928% -4.6824% -4.3744%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 50 measurements (4.00%)
  2 (4.00%) high mild

Finally a note on chaining many PRs together: I do that sometimes, but normally I have not written or at least not polished the code that comes next. Stacking many PRs on top of each other risks creating quite gnarly conflicts in case other PRs are merged in between or if you have code review comments on the first PR in a set, that other PRs depend on. So to be as efficient as possible in terms of man-hours (rather than calendar-hours) I prefer to take one step at a time in the right direction.

So if it is OK with you to keep our current pace, that is very much OK with me too.

This is one step towards resolving #98

* In `examples/` and `benches/` I simply do .unwrap()

* In tests I do `.expect("#[cfg(test)]")` to make reviewing the diff
  easy

* In deprecated methods I do .expect("<deprecation message>")

* In `uses_backrefs = uses_backrefs || proto_ids.iter().any(|id|
  syntax_set.get_context(id).unwrap().uses_backrefs);` I do an
  `.unwrap()` because `?` does not work inside of `.any()`. But that can
  be fixed later if necessary without semver breakage.

* To keep the scope of the PR manageable I do `.unwrap()` in
  `ContextReference::resolve()` and `impl<'a> Iterator for
  MatchIter<'a>` to not have to propagate an error further. That will be
  done in a follow-up commit.
@Enselic Enselic force-pushed the parse-and-highlight-line-with-result branch from 85a1a4e to 71e1b78 Compare March 13, 2022 09:37
@Enselic Enselic changed the base branch from master to plain-text-fallback March 13, 2022 09:39
@Enselic
Copy link
Collaborator Author

Enselic commented Mar 13, 2022

I rebased this on top of #427 since merging them independently will not cause merge conflicts but will fail the build (since the other PR introduces a new usage of parse_line() but this PR changes the return type of parse_line()), so don't forget to change target branch to master before merging this PR when you have had time to review it.

@trishume trishume changed the base branch from plain-text-fallback to master March 13, 2022 20:45
Copy link
Owner

@trishume trishume left a comment

Choose a reason for hiding this comment

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

Nice! And yah no problem if you don't want to stack things up, just want to make sure I'm not slowing you down more than you'd like.

@trishume trishume merged commit ce1ba5b into trishume:master Mar 13, 2022
@Enselic Enselic deleted the parse-and-highlight-line-with-result branch March 14, 2022 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants