about summary refs log tree commit diff
path: root/src/librustdoc/html/static/js/source-script.js
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-18 04:42:06 +0100
committerGitHub <noreply@github.com>2022-01-18 04:42:06 +0100
commit04b2073c84b71a0a8c30e628f942ac4d593f72fe (patch)
tree69c7babc48f7f47756b94fcaf99549c4519ef065 /src/librustdoc/html/static/js/source-script.js
parentbe3d25bd7800b67bb7675a3d74445de305fcff3d (diff)
parent7b5b3cf82cd406e0fa2e95c2c3e762d85decb40d (diff)
downloadrust-04b2073c84b71a0a8c30e628f942ac4d593f72fe.tar.gz
rust-04b2073c84b71a0a8c30e628f942ac4d593f72fe.zip
Rollup merge of #92923 - dtolnay:ringbuffer, r=petrochenkov
Abstract the pretty printer's ringbuffer to be infinitely sized

This PR backports https://github.com/dtolnay/prettyplease/commit/8e5e83c3ff7faaa272115a81b76f07a1d4a91969 from the `prettyplease` crate into `rustc_ast_pretty`.

Using a dedicated RingBuffer type with non-wrapping indices, instead of manually `%`-ing indices into a capped sized buffer, unlocks a number of simplifications to the pretty printing algorithm implementation in followup commits such as https://github.com/dtolnay/prettyplease/commit/fcb5968b1e1357497c7d7e205d22bb4e8c1dbd38 and https://github.com/dtolnay/prettyplease/commit/4427cedcb80294e89b823f923fdb16544c737bd9.

This change also greatly reduces memory overhead of the pretty printer. The old implementation always grows its buffer to 205920 bytes even for files without deeply nested code, because it only wraps its indices when they hit the maximum tolerable size of the ring buffer (the size after which the pretty printer will crash if there are that many tokens buffered). In contrast, the new implementation uses memory proportional to the peak number of simultaneously buffered tokens only, not the number of tokens that have ever been in the buffer.

Speaking of crashing the pretty printer and "maximum tolerable size", the constant used for that in the old implementation is a lie:

https://github.com/rust-lang/rust/blob/de9b573eedaaa6d6e7c00c986cccbee802f9287b/compiler/rustc_ast_pretty/src/pp.rs#L227-L228

It was raised from 3 to 55 in https://github.com/rust-lang/rust/pull/33934 because that was empirically the size that avoided crashing on one particular test crate, but according to https://github.com/rust-lang/rust/pull/33934#issuecomment-226700470 other syntax trees still crash at that size. There is no reason to believe that any particular size is good enough for arbitrary code, and using a large number like 55 adds overhead to inputs that never need close to that much of a buffer. The new implementation eliminates this tradeoff.
Diffstat (limited to 'src/librustdoc/html/static/js/source-script.js')
0 files changed, 0 insertions, 0 deletions