- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
Adjust successor iterators. #148157
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
Adjust successor iterators. #148157
Conversation
| 
           @bors try @rust-timer queue  | 
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
Adjust successor iterators.
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| 
           Finished benchmarking commit (07d7953): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with  @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy. 
 Max RSS (memory usage)Results (primary -0.5%, secondary 0.7%)A less reliable metric. May be of interest, but not used to determine the overall result above. 
 CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 473.751s -> 477.517s (0.79%)  | 
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| 
           The perf regression is fixed.  | 
    
| 
           The bootstrap regression looks weird, let's retry to see if it was noise or not. @bors try @rust-timer queue  | 
    
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
Adjust successor iterators.
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| 
           Finished benchmarking commit (fc81109): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy. 
 Max RSS (memory usage)Results (primary 0.1%, secondary -2.8%)A less reliable metric. May be of interest, but not used to determine the overall result above. 
 CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 475.144s -> 474.464s (-0.14%)  | 
    
| 
           Ok, looks like it was just noise after all.  | 
    
Because rust-lang#148054 was a slight perf regression. The problem was seemingly because this iterator structure: ``` slice_iter.chain(Option_iter.chain(Option_iter)) ``` changed to this: ``` slice_iter.chain(Option_iter).chain(Option_iter) ``` The commit also tweaks the `slice_iter` part, changing `into_iter` to `iter` and using `[]` instead of `(&[])`, for conciseness and consistency.
8ba2487    to
    d18b0b4      
    Compare
  
    
          
  | 
    
| 
           @bors r+ Do we have any idea why this version is better? The code here isn't particularly hot, so I fear that if you extract this situation to a standalone microbenchmark the difference will be massive.  | 
    
          
 Not really. I tried https://godbolt.org/z/YKoYx7sno but it wasn't very enlightening -- the version that's better in the compiler generates slightly longer code. I do know that chained iterators are slow in general, and the perf book recommends avoiding them if possible.  | 
    
          slice_iter.chain(Option_iter.chain(Option_iter))
//         ^outer_chain      ^inner_chain
// -> Chain<Slice, Chain<Opt, Opt>>In this version,  if let Some(slice_iter) = &mut outer_chain.a {
    if let Some(item) = slice_iter.next() {
        return Some(item);
    }
    outer_chain.a = None;
}
if let Some(inner_chain) = &mut outer_chain.b {
    if let Some(opt1) = &mut inner_chain.a {
        if let Some(item) = opt1.next() {
            return Some(item);
        }
        inner_chain.a = None;
    }
    if let Some(opt2) = &mut inner_chain.b {
        if let Some(item) = opt2.next() {
            return Some(item);
        }
    }
}
NoneNotice that the slice iterator is only one branch away, which is advantageous if that ends up providing multiple items. Compare that to the other version where it has to check two  slice_iter.chain(Option_iter).chain(Option_iter)
//         ^inner_chain       ^outer_chain
// -> Chain<Chain<Slice, Opt>, Opt>if let Some(inner_chain) = &mut outer_chain.a {
    if let Some(slice_iter) = &mut inner_chain.a {
        if let Some(item) = slice_iter.next() {
            return Some(item);
        }
        inner_chain.a = None;
    }
    if let Some(opt1) = &mut inner_chain.b {
        if let Some(item) = opt1.next() {
            return Some(item);
        }
    }
    outer_chain.a = None;
}
if let Some(opt2) = &mut outer_chain.b {
    if let Some(item) = opt2.next() {
        return Some(item);
    }
}
NoneHowever, the structure matters less if it's consumed by inner iteration --   | 
    
| 
           I fiddled around with a custom iterator but I couldn't get an outcome better than the chained one.  | 
    
| 
           ☀️ Test successful - checks-actions  | 
    
          What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing 647f153 (parent) -> 23c7bad (this PR) Test differencesShow 4 test diffs4 doctest diffs were found. These are ignored, as they are noisy. Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 23c7bad921fb7163de37ea680bed317deaa03fda --output-dir test-dashboardAnd then open  Job duration changes
 How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance  | 
    
| 
           Finished benchmarking commit (23c7bad): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy. 
 Max RSS (memory usage)Results (primary 1.7%, secondary 2.1%)A less reliable metric. May be of interest, but not used to determine the overall result above. 
 CyclesResults (secondary 0.4%)A less reliable metric. May be of interest, but not used to determine the overall result above. 
 Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 474.981s -> 473.971s (-0.21%)  | 
    
Because #148054 was a slight perf regression.
The problem was seemingly because this iterator structure:
changed to this:
The commit also tweaks the
slice_iterpart, changinginto_itertoiterand using[]instead of(&[]), for conciseness and consistency.r? @saethlin