1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

perf: O(n) update of position

This commit is contained in:
Kevin Amado 2022-02-27 17:44:37 -05:00
parent 8498ce5a4b
commit 4bcf3acef5
3 changed files with 23 additions and 16 deletions

View file

@ -82,6 +82,8 @@ Types of changes
- A `--threads` flag, so you can pick how many formatting threads to spawn.
Defaults to the number of logical CPUs in your system.
- Position counters were improved to offer an extra 1.13x speedup.
## [0.6.0] - 2022-02-25
### Added

View file

@ -203,15 +203,22 @@ Please see: [CHANGELOG.md](./CHANGELOG.md).
- BogoMips: 7599.80
- Cache Size: 16384 KB
Using:
```bash
# x86_64-unknown-linux-gnu
$ time alejandra --threads $threads /path/to/nixpkgs
```
Results:
| Logical Cores | Seconds |
| :-----------: | :-----: |
| 1 | 15.1 |
| 2 | 7.9 |
| 4 | 5.4 |
| 8 | 4.1 |
| 16 | 3.6 |
| $threads | Seconds |
| :------: | :-----: |
| 1 | 13.4 |
| 2 | 6.9 |
| 4 | 3.6 |
| 8 | 2.6 |
| 16 | 3.1 |
[^semantic-changes]: The methodology to claim this is:

View file

@ -12,15 +12,13 @@ impl Default for Position {
impl Position {
pub fn update(&mut self, text: &str) {
let chars: Vec<char> = text.chars().collect();
let newlines = chars.iter().filter(|&c| *c == '\n').count();
self.line += newlines;
if newlines > 0 {
self.column = 0
for char in text.chars() {
if char == '\n' {
self.line += 1;
self.column = 0;
} else {
self.column += 1;
}
}
self.column += match chars.iter().rposition(|c| *c == '\n') {
Some(pos) => chars.len() - pos - 1,
None => chars.len(),
};
}
}