mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:42:02 +00:00

## Summary
A new CLI option (`-o`/`--output-file`) to write output to a file
instead of stdout.
Major change is to remove the lock acquired on stdout. The argument is
that the output is buffered and thus the lock is acquired only when
writing a block (8kb). As per the benchmark below there is a slight
performance penalty.
Reference:
https://rustmagazine.org/issue-3/javascript-compiler/#printing-is-slow
## Benchmarks
_Output is truncated to only contain useful information:_
Command: `check --isolated --no-cache --select=ALL --show-source
./test-repos/cpython"`
Latest HEAD (361d45f2b2
) with and without
the manual lock on stdout:
```console
Benchmark 1: With lock
Time (mean ± σ): 5.687 s ± 0.075 s [User: 17.110 s, System: 0.486 s]
Range (min … max): 5.615 s … 5.860 s 10 runs
Benchmark 2: Without lock
Time (mean ± σ): 5.719 s ± 0.064 s [User: 17.095 s, System: 0.491 s]
Range (min … max): 5.640 s … 5.865 s 10 runs
Summary
(1) ran 1.01 ± 0.02 times faster than (2)
```
This PR:
```console
Benchmark 1: This PR
Time (mean ± σ): 5.855 s ± 0.058 s [User: 17.197 s, System: 0.491 s]
Range (min … max): 5.786 s … 5.987 s 10 runs
Benchmark 2: Latest HEAD with lock
Time (mean ± σ): 5.645 s ± 0.033 s [User: 16.922 s, System: 0.495 s]
Range (min … max): 5.600 s … 5.712 s 10 runs
Summary
(2) ran 1.04 ± 0.01 times faster than (1)
```
## Test Plan
Run all of the commands which gives output with and without the
`--output-file=ruff.out` option:
* `--show-settings`
* `--show-files`
* `--show-fixes`
* `--diff`
* `--select=ALL`
* `--select=All --show-source`
* `--watch` (only stdout allowed)
resolves: #4754
39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::{bail, Result};
|
|
use itertools::Itertools;
|
|
|
|
use ruff::resolver;
|
|
use ruff::resolver::PyprojectConfig;
|
|
|
|
use crate::args::Overrides;
|
|
|
|
/// Print the user-facing configuration settings.
|
|
pub(crate) fn show_settings(
|
|
files: &[PathBuf],
|
|
pyproject_config: &PyprojectConfig,
|
|
overrides: &Overrides,
|
|
writer: &mut impl Write,
|
|
) -> Result<()> {
|
|
// Collect all files in the hierarchy.
|
|
let (paths, resolver) = resolver::python_files_in_path(files, pyproject_config, overrides)?;
|
|
|
|
// Print the list of files.
|
|
let Some(entry) = paths
|
|
.iter()
|
|
.flatten()
|
|
.sorted_by(|a, b| a.path().cmp(b.path())).next() else {
|
|
bail!("No files found under the given path");
|
|
};
|
|
let path = entry.path();
|
|
let settings = resolver.resolve(path, pyproject_config);
|
|
|
|
writeln!(writer, "Resolved settings for: {path:?}")?;
|
|
if let Some(settings_path) = pyproject_config.path.as_ref() {
|
|
writeln!(writer, "Settings path: {settings_path:?}")?;
|
|
}
|
|
writeln!(writer, "{settings:#?}")?;
|
|
|
|
Ok(())
|
|
}
|