Skip to content

Parse the temperature in reverse#11

Open
irobot wants to merge 5 commits intojonhoo:mainfrom
irobot:parse-temp-reverse
Open

Parse the temperature in reverse#11
irobot wants to merge 5 commits intojonhoo:mainfrom
irobot:parse-temp-reverse

Conversation

@irobot
Copy link
Copy Markdown

@irobot irobot commented Dec 6, 2025

The observation is that the temperature component is ~fixed length (between 3 and 5 characters).
Therefore having found the end of the line, scanning from the end, we obtain both the temperature and the location of the semicolon.

With this change, Hyperfine reports a ~1.50 faster finish time.
Tested on a 12 core / 24 thread AMD Ryzen 5900x.

Benchmark 1: target/release/brrr-org
Time (mean ± σ): 2.205 s ± 0.020 s [User: 42.833 s, System: 1.119 s]
Range (min … max): 2.194 s … 2.260 s 10 runs

Benchmark 2: target/release/brrr
Time (mean ± σ): 1.473 s ± 0.021 s [User: 25.448 s, System: 1.108 s]
Range (min … max): 1.452 s … 1.529 s 10 runs

Summary
target/release/brrr ran
1.50 ± 0.03 times faster than target/release/brrr-org

Starting from the end of the line, take the digits one at a time.
For example the following line:
> Seattle;-12.3

* Temperature * 10 (T) is encoded as an `i16`.
* Starting from the end, the first digit is 3. T = 3
* Skip over the dot, T = 3
* Next is 2. T = 2 * 10 + 3 = 23
* Next character could be a digit, but it could also be - or ;
  In this case it is 1, so T = 1 * 100 + T = 123
* One last time, we need to check for a -, which we have, so T = -T =
  -123
* Next character is guaranteed to be ; thus we have also obtained the length
  of the station name.

With this change, Hyperfine reports a ~1.50 faster finish time.
Tested on a 12 core / 24 thread AMD Ryzen 5900x.

> Benchmark 1: target/release/brrr-org
  Time (mean ± σ):      2.205 s ±  0.020 s    [User: 42.833 s, System: 1.119 s]
  Range (min … max):    2.194 s …  2.260 s    10 runs

Benchmark 2: target/release/brrr
  Time (mean ± σ):      1.473 s ±  0.021 s    [User: 25.448 s, System: 1.108 s]
  Range (min … max):    1.452 s …  1.529 s    10 runs

Summary
  target/release/brrr ran
    1.50 ± 0.03 times faster than target/release/brrr-org
@jonhoo
Copy link
Copy Markdown
Owner

jonhoo commented Dec 6, 2025

Oh interesting! Give it a try after the optimizations from #2, which already sped us up a decent amount and see if it still improves things.

@irobot
Copy link
Copy Markdown
Author

irobot commented Dec 6, 2025

Yes, you're right! After merging in the latest improvements, speed up is now only ~5%. My feeling is that most of the gains were in minimizing/removing the search for the semicolon.

+ move temperature parsing code to parse_temperature
+ restore temp parsing unit tests
+ replace usize decrement with unchecked_sub() just in case, though it
  doesn't seem to make any meaningful difference perf-wise.

After merging in the latest improvements, the speed up offered by this
change is no longer as significant.

```
Benchmark 1: target/release/brrr-main
  Time (mean ± σ):      1.196 s ±  0.006 s    [User: 19.190 s, System: 1.133 s]
  Range (min … max):    1.186 s …  1.207 s    10 runs

Benchmark 2: target/release/brrr
  Time (mean ± σ):      1.143 s ±  0.005 s    [User: 17.915 s, System: 1.150 s]
  Range (min … max):    1.135 s …  1.150 s    10 runs

Summary
  target/release/brrr ran
    1.05 ± 0.01 times faster than target/release/brrr-main
```
Wondering if CI failing has to do with rustfmt using a different style
edition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants