[llvm] [Coverage] Improve performance of propagating Counter of Expansions (PR #122589)
Jessica Paquette via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 13 22:30:26 PST 2025
================
@@ -440,26 +441,83 @@ Error RawCoverageMappingReader::read() {
// Set the counters for the expansion regions.
// i.e. Counter of expansion region = counter of the first region
// from the expanded file.
- // Perform multiple passes to correctly propagate the counters through
- // all the nested expansion regions.
- SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
- FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
- for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
- for (auto &R : MappingRegions) {
- if (R.Kind != CounterMappingRegion::ExpansionRegion)
- continue;
- assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
- FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
+ // This assumes:
+ // - Records are partitioned by FileID.
+ // - The Count in Expansion is propagated from 1st Record in
+ // ExpandedFileID.
+ // Therefore another fact below can be assumed.
+ // - Propagation chain consists of Expansions at each 1st Record.
+
+ /// Node per File.
+ struct FileNode {
+ /// 1st Record in the File.
+ CounterMappingRegion *FirstR = nullptr;
+ /// The Expansion Record out of this File.
+ CounterMappingRegion *ExpanderR = nullptr;
+ /// Count hasn't been propagated yet (for Expansion)
+ bool ExpansionPending = false;
+ };
+ SmallVector<FileNode> FileIDExpansionRegionMapping(VirtualFileMapping.size());
+
+ // Construct the tree.
+ auto PrevFileID = std::numeric_limits<unsigned>::max(); // Invalid value
+ for (auto &R : MappingRegions) {
+ if (R.Kind == CounterMappingRegion::ExpansionRegion) {
+ // The File that contains Expansion may be a node.
+ assert(!FileIDExpansionRegionMapping[R.ExpandedFileID].ExpanderR);
+ FileIDExpansionRegionMapping[R.ExpandedFileID].ExpanderR = &R;
+
+ // It will be the node if (isExpansion and ExpansionPending).
+ FileIDExpansionRegionMapping[R.ExpandedFileID].ExpansionPending = true;
}
- for (auto &R : MappingRegions) {
- if (FileIDExpansionRegionMapping[R.FileID]) {
- FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
- FileIDExpansionRegionMapping[R.FileID] = nullptr;
- }
+
+ if (PrevFileID != R.FileID) {
----------------
ornata wrote:
We can reduce indentation here.
```
// Have we found the first record for this file already?
if (PrevFileID == R.FileID) {
// We've seen this FileID before, so we have found the
// first record. Move on to the next region.
continue;
}
// We have not. Mark the current record as the first record
// for this file.
assert(!FileIDExpansionRegionMapping[R.FileID].FirstR && "Already found a record for an ID we haven't seen before?!");
FileIDExpansionRegionMapping[R.FileID].FirstR = &R;
// Remember that we've processed this file already.
PrevFileID = R.FileID;
```
https://github.com/llvm/llvm-project/pull/122589
More information about the llvm-commits
mailing list