-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
incr.comp.: Improve caching efficiency by handling spans in a more robust way #47389
Comments
Would it make sense to introduce the concept of scoped spans? Given
|
@estebank IMO the first step would be to make spans file-local (including treating macro expansions as "files"), which would already get rid of a lot of the span-related churn. For intra-file deltas, I'd rather adjust all the spans when decoding them (taking into account whether some things were accessing the line/column information from spans), but this scheme only works really good if you do get a delta from e.g. the IDE that is using the compiler. |
Note though that file-local spans are not enough to make a difference here. In fact, we are already hashing spans as |
@michaelwoerister I have my own agenda here, which is to drive incremental recompilation from incremental reparsing (with a GLL parser, hopefully, not a hand-written one), instead of HIR. |
I don't quite see how that would help here. Debuginfo (and the various datastructures that thread span info to it) is what this issue is about. |
@michaelwoerister I might be getting confused. Is the proposal to bypass most of the transformations that would depend on the spans in question, by specifically keeping the EDIT: if we can provide enough operations to not require observing |
Yes.
This is basically what the approach about proposes, only that the above renames |
Okay I found the approach I prefer in the original post:
However, I think we can do this with the current |
How would you detect changed spans with a dependency edge to somewhere? |
@michaelwoerister I'd expect all of the observable |
The span information is part of the input to the compilation process, so we need to always hash it (in order to detect changes) and then the query unpacking the span must add an edge to the |
Don't we have queries that only take |
What do you mean exactly? |
Nevermind, I think I see, |
[do not merge] Benchmark ignoring span hashes during incr. comp. for getting a lower bound for #47389 I figured that ignoring changes to span values might give a useful lower bound on compile times with the optimizations described in #47389 applied. Keep in mind that any improvements shown with this PR could only be achieved if we could update object files in place, which would be the "stretch goal" for #47389.
In #56287 I did a preliminary benchmark that shows what kind of improvements can be expected at best from the optimization/refactoring described here: How to read the results: Since the benchmark was done by completely ignoring spans during incr. comp. hashing, the numbers show the best case scenario (and then some). This best case scenario could only be achieved by the The
As always with incr. comp., improvements depend entirely on the actual changeset, so take with a grain of salt. But, in my opinion, the numbers still show that there's quite a bit of potential in an optimization like this. Also note: The |
This is a great, well-written summary issue. Is this still considered the right path forward? I'm interested in taking a crack at it. |
Thanks for your interest, @johnbcoughlin. I think there is consensus that absolute source location information should be moved out of as many internal data structures as possible. But it's not clear how to exactly go about it. I know @eddyb has ideas and @matklad already goes about the problem in a smarter way in rust-analyzer. I personally don't have the bandwidth to drive the needed design effort at the moment, unfortunately. |
@michaelwoerister could you explain this smarter way that @matklad is going? How does rust-analyzer approach it? |
@est31 rust-analyzer uses the same approach as C#'s Roslyn compiler- syntax tree nodes do not store positions (absolute or otherwise), but only their size. Positions are only reconstructed on-demand as part of walking the tree, by accumulating the sizes of previous nodes. This makes the actual data stored in the nodes position-invariant. |
Oh wow that's an incredibly smart solution. Thanks! |
Hmmm now I'm not so sure any more. Because this only works for spans embedded into the AST and while you are running a visitor on that AST that keeps record on the current position whether there is an error or not (you can't start keeping the record when you know you want yield the error, you need it from the very beginning, as you can't re-iterate over the AST without a way to reidentify the span). It also doesn't work when you store stuff in tables, e.g. you want to print "Defined here" things because those spans are embedded in tables instead of the AST and you get the spans by lookups instead of AST walks. Rust's elaborate errors contain a lot of those "third party" spans. Maybe some ID system could help, where each span contains an unique id that AST walking code can recognize when encountering, but it can't be purely content based (say hash of content) because otherwise duplicating the same code say in different functions of the same file would yield the same spans. This happens more often than you think, after all how often do you have the same identifier occur again and again? |
I tried implementing both approaches in two draft PRs. #72015 attempts to move the #84373 attempts to make |
The Problem
Source location information (which, for simplicity, I'll just call "spans" from here on out) are the bane of incremental compilation's existence. Why is that? Unlike most other kinds of frequent changes done to source code, changing spans has (seemingly) non-local effects.
As an example, let's first consider a "regular" change of a program, like turning a
+
in an expression into a-
. This change means that the function containing the expression has to be re-analyzed and the object file it was instantiated in has to be re-compiled. So far, so expected. Now, in contrast, consider a change that affects the span of a function, like adding a line with a comment to it. At first glance, it looks like we haven't really changed anything -- we just added a comment after all -- but that's not true. Spans are part of the HIR, the MIR, ScopeTrees, and (via debuginfo and panic messages) even LLVM IR and object files. So, adding a comment to a function will legitimately cause the function and its containing object file to be re-compiled. That's a bit unexpected and sad, but how is it "non-local"?Remember that we added a new line with a comment to a function, thus changing the span of the function. What I didn't explicitly mention was that by adding this line, we shifted down everything following that line in the same source file, thus changing not only one function but potentially dozens of functions and type definitions. That's what I described as "non-local" effects (or rather "seemingly" non-local because shifting everything by a line is a legitimate, real change to everything that has been shifted, it's just easy to overlook).
"That's horrific!", you say, "We have to do something about it!" Well, I'm glad you think so too.
What can we do?
As stated above, the changes and the invalidation throughout the incr.comp. cache that they cause are legitimate. They are not false positives, since changing the source location of a function really changes (for example) the MIR of that function. So we cannot just be smarter about tracking spans or ignore them altogether. However, what we can do is refactoring the representation of HIR, MIR, etc, so that they don't actually contain spans anymore. The span information has to be somewhere, and we still have to be able to map various things in HIR, MIR, etc to spans, but spans can be split out into separate tables. As a consequence, HIR, MIR, and ScopeTrees will be represented in a way that is impervious to changes that don't affect their structure.
One way to achieve this (and the only way I know) is to introduce the concept of "abstract spans". An abstract span does not directly contain a source location, but it identifies a source location uniquely. For example, if we store all spans in a side table then the abstract span would be the key to this table. For this to bring any improvement over the current situation, an abstract span must be stable across changes that don't affect the structure of the thing containing it. E.g. shifting down a function by a line can change the thing the abstract span points to, but the value of the abstract span itself must not change. (This is simple to achieve by using a scheme that is similar to what we already do for
HirId
. Implementing it without increasing memory requirements is harder).Implementation Strategies
There are a few prerequisites for the implementation:
DepNode
thanHir
,HirBody
,OptimizedMir
, etc, which implies that it must not be directly accessible from any of the data covered by theseDepNodes
.These goals can be achieved by:
HirId::owner
that then corresponds to oneDepNode
, making spans be tracked at the same granularity as HIR items.Span
fields with abstract spans in HIR, MIR, etc. This will mean quite a bit of refactoring everywhere these spans are used (as opposed to just being copied around)Alternatively, this could also be achieved by:
CodeMap
inaccessible from queries and generating a map fromSpan
value toDepNode
during HIR lowering, thus effectively making the existingSpan
type abstract.Span
to its contents.Spans
directly.I lean a bit towards alternative (1) but it's hard to gauge which one will lead to cleaner, more robust code in the end. Solution (1) would have a risk of false positives (too much invalidation), while solution (2) has the risk of false negatives (changes not detected) because existing APIs present tracking holes. Not detecting changes seems like the worse problem.
Regardless of the implementation, we will have to store additional tables in crate metadata that allow mapping from abstract spans to regular spans for upstream crates.
Abstract Span Representation
Ideally, an abstract span would not take up more space than one
u32
, which is how much space aSpan
takes up. One way to achieve this would be by making abstract spans bestruct SpanId(ast::NodeId)
. Mapping fromSpanId
toSpan
would then involve mapping fromNodeId
toHirId
, taking theHirId::owner
to identify the correct side table andDepNode
, and then theHirId::local_id
as key into the side table. However, this only works for the current crate. In order for this to work across crates, we would either have to makeSpanId
also contain aCrateNum
(thus doubling its size to 8 bytes), or implement aNodeId
remapping scheme, similar to what we do for importedFileMaps
and formerly already had for AST "inlining". With the latter in place we might be able to remove theHirId
from some of the HIR structs again, which would help amortize its implementation effort.NodeId
-based abstract spans have the restriction of only being able to represent things that have aNodeId
. However, that should be easily solved by assigningNodeId
s to things that at the moment have aSpan
but noNodeId
.NodeId
-based abstract spans have the advantage that HIR structs would not have to store a separate span field. TheSpanId
could be generated from the already availableNodeId
.Abstract spans could be implemented completely separately from
NodeId
andHirId
but there's probably little advantage to doing so while quite a bit of new infrastructure would have to be put into place.Guarding against Regressions
After putting so much effort into using abstract spans, we'll want to avoid that vanilla
Span
values make their way into query results again. Luckily this should be easily achievable by adding an assertion to theHashStable
implementation forSpan
that makes sure we don't encounter unexpected invocations.Abstract Spans Level 2
The first goal would be to use abstract spans in everything up until and including MIR. An even more ambitious goal would be to also use abstract spans in cached LLVM IR and/or object files. That might allow us to skip re-optimizing code and just patch up source locations (if it's really just spans that have changed -- detecting that is another challenge).
Call for feedback
Since this will probably result in quite a few changes, I'd like to get some feedback before jumping into an implementation. Here are some guiding questions:
Any kind of feedback is welcome!
cc @rust-lang/compiler
The text was updated successfully, but these errors were encountered: