more: replace InputType enum with Input struct; cache file_size#12225
Merged
Conversation
InputType used a 2-variant enum to dispatch read_line/len; len() returned Result<Option<u64>> because metadata() was called on every invocation. Replace with a single Input struct holding Box<dyn BufRead> plus a cached file_size, computed once at construction. Pager::new no longer needs to return UResult since the only fallible call is gone. Drive-by: rename page_resize -> resize_page (verb-first matches every other Pager method); upgrade MULTI_FILE_TOP_PROMPT comment to a doc comment.
4d11bd2 to
416989c
Compare
|
GNU testsuite comparison: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The pager dispatched between
BufReader<File>andStdinvia a 2-variant enum, and calledmetadata()every time someone asked for the file size — even though the answer never changes during the run.Changes
enum InputTypewith astruct Inputholding aBox<dyn BufRead>plus a cachedfile_size: Option<u64>, populated once at construction.Input::from_file(readsmetadata().len()) andInput::from_stdin(size unknown).Input::len()now returnsOption<u64>instead ofResult<Option<u64>>— the only fallible call (metadata()) is gone.Pager::file_sizefield;draw_status_barreads fromself.input.len()directly.Pager::newno longer returnsUResult<Self>— nothing inside it can fail anymore.Drive-by
page_resize→resize_page(verb-first matches every otherPager::*method).MULTI_FILE_TOP_PROMPTblock comment to a doc comment.Carved out from #10667 per the request to split.