1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +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. - A `--threads` flag, so you can pick how many formatting threads to spawn.
Defaults to the number of logical CPUs in your system. 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 ## [0.6.0] - 2022-02-25
### Added ### Added

View file

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

View file

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