Avoid `Box<dyn Read>` to avoid unnecessary overhead
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 24.1k
- Forks
- 2k
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 365
Description
Box<dyn Read> provides flexibility, but it comes with inherent costs:
- Virtual dispatch on every read call
- A heap allocation to store the trait object
- An additional level of indirection
This can introduce avoidable overhead, particularly when working with in-memory sources or buffered input where the cost of I/O is not dominant.
In many utilities, the set of input sources is limited and known in advance (e.g., Stdin and File). In such cases, type erasure via Box<dyn Read> is not strictly necessary. A concrete enum can eliminate both heap allocation and dynamic dispatch.
A pattern already used elsewhere in the codebase (e.g., comm.rs) demonstrates this approach:
enum Input {
Stdin(std::io::Stdin),
File(std::fs::File),
}
impl Read for Input {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Input::Stdin(s) => s.read(buf),
Input::File(f) => f.read(buf),
}
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by searching the repository for uses of Box and read the existing pattern referenced in comm.rs. Identify which utilities have a fixed set of input sources, then determine the relevant tests for each affected utility. Done means replacing suitable type erasure without changing supported inputs or behavior, with the existing tests still passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- cli, performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 45/100