Skip to content

Printer: Reserve buffer upfront#6550

Merged
MichaReiser merged 1 commit intomainfrom
printer-reserve-buffer-upfront
Aug 14, 2023
Merged

Printer: Reserve buffer upfront#6550
MichaReiser merged 1 commit intomainfrom
printer-reserve-buffer-upfront

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Aug 14, 2023

Summary

I did some profiling on the formatter and noticed that we end up growing the Printer buffer multiple times. This PR tries to reduce the number of times that we need to grow the formatted string buffer by allocating a buffer of the same size as the unformatted document.
This should be a good default because most files in a project are already formatted (we neither allocate too much nor too little).

Test Plan

This improved the formatter time by about 2%

cargo test

@MichaReiser
Copy link
Member Author

MichaReiser commented Aug 14, 2023

@MichaReiser MichaReiser force-pushed the formatter-remove-allow-pedantic branch from 01d0c1d to 008773a Compare August 14, 2023 07:10
@MichaReiser MichaReiser added the formatter Related to the formatter label Aug 14, 2023
@MichaReiser MichaReiser force-pushed the printer-reserve-buffer-upfront branch from e755341 to 222bf7d Compare August 14, 2023 07:11
@MichaReiser
Copy link
Member Author

It may be interesting to see if we can come up with a good heuristic for the VecBuffer size. E.g. is it always around ~0.7 * the source code (@konstin ;))

@github-actions
Copy link
Contributor

github-actions bot commented Aug 14, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      3.9±0.02ms    10.3 MB/sec    1.00      3.9±0.03ms    10.4 MB/sec
formatter/numpy/ctypeslib.py               1.00    719.0±8.14µs    23.2 MB/sec    1.00    720.6±5.97µs    23.1 MB/sec
formatter/numpy/globals.py                 1.00     71.5±0.29µs    41.3 MB/sec    1.00     71.3±0.31µs    41.4 MB/sec
formatter/pydantic/types.py                1.00  1569.0±39.62µs    16.3 MB/sec    1.00  1573.4±37.73µs    16.2 MB/sec
linter/all-rules/large/dataset.py          1.00     10.6±0.04ms     3.8 MB/sec    1.01     10.7±0.04ms     3.8 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      2.9±0.01ms     5.7 MB/sec    1.00      2.9±0.01ms     5.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    330.4±2.27µs     8.9 MB/sec    1.00    329.5±1.44µs     9.0 MB/sec
linter/all-rules/pydantic/types.py         1.00      5.5±0.02ms     4.7 MB/sec    1.00      5.5±0.04ms     4.6 MB/sec
linter/default-rules/large/dataset.py      1.00      5.6±0.02ms     7.2 MB/sec    1.00      5.6±0.03ms     7.2 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1190.0±2.92µs    14.0 MB/sec    1.00   1187.8±3.12µs    14.0 MB/sec
linter/default-rules/numpy/globals.py      1.00    124.7±0.30µs    23.7 MB/sec    1.00    124.6±0.34µs    23.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      2.5±0.01ms    10.2 MB/sec    1.00      2.5±0.01ms    10.1 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      4.3±0.15ms     9.4 MB/sec    1.00      4.3±0.02ms     9.5 MB/sec
formatter/numpy/ctypeslib.py               1.00    781.8±7.25µs    21.3 MB/sec    1.01    787.8±8.73µs    21.1 MB/sec
formatter/numpy/globals.py                 1.00     78.6±0.67µs    37.5 MB/sec    1.01     79.4±1.22µs    37.2 MB/sec
formatter/pydantic/types.py                1.00  1759.5±19.87µs    14.5 MB/sec    1.00  1767.5±24.28µs    14.4 MB/sec
linter/all-rules/large/dataset.py          1.00     12.7±0.19ms     3.2 MB/sec    1.00     12.7±0.12ms     3.2 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.5±0.01ms     4.8 MB/sec    1.00      3.5±0.02ms     4.8 MB/sec
linter/all-rules/numpy/globals.py          1.01    362.4±5.35µs     8.1 MB/sec    1.00    357.8±4.79µs     8.2 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.5±0.05ms     3.9 MB/sec    1.00      6.5±0.05ms     3.9 MB/sec
linter/default-rules/large/dataset.py      1.00      6.9±0.03ms     5.9 MB/sec    1.01      6.9±0.05ms     5.9 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1427.9±9.89µs    11.7 MB/sec    1.00   1431.2±9.36µs    11.6 MB/sec
linter/default-rules/numpy/globals.py      1.00    142.7±1.16µs    20.7 MB/sec    1.00    142.7±1.93µs    20.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.1±0.02ms     8.3 MB/sec    1.00      3.1±0.02ms     8.3 MB/sec

Copy link
Member

@konstin konstin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

what i really want to benchmark is how many % extra we need to cover >95% reformattings too

@MichaReiser MichaReiser force-pushed the formatter-remove-allow-pedantic branch from 008773a to 70c5ff5 Compare August 14, 2023 08:21
@MichaReiser MichaReiser force-pushed the printer-reserve-buffer-upfront branch from 222bf7d to a97a59a Compare August 14, 2023 08:21
@MichaReiser MichaReiser mentioned this pull request Aug 14, 2023
@MichaReiser MichaReiser force-pushed the formatter-remove-allow-pedantic branch from 70c5ff5 to 206b837 Compare August 14, 2023 08:26
@MichaReiser MichaReiser force-pushed the printer-reserve-buffer-upfront branch from a97a59a to 3076d97 Compare August 14, 2023 08:26
@MichaReiser MichaReiser force-pushed the formatter-remove-allow-pedantic branch from 206b837 to 301fe5c Compare August 14, 2023 09:59
@MichaReiser MichaReiser force-pushed the printer-reserve-buffer-upfront branch from 3076d97 to 2db43a5 Compare August 14, 2023 09:59
Base automatically changed from formatter-remove-allow-pedantic to main August 14, 2023 12:02
@MichaReiser MichaReiser force-pushed the printer-reserve-buffer-upfront branch from 2db43a5 to 493a3b6 Compare August 14, 2023 12:03
@MichaReiser MichaReiser enabled auto-merge (squash) August 14, 2023 12:04
@MichaReiser MichaReiser merged commit 910dbbd into main Aug 14, 2023
@MichaReiser MichaReiser deleted the printer-reserve-buffer-upfront branch August 14, 2023 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants