<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63228>63228</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            mapping from `SourceLocation` to `FileID` can load `SLocEntry`s from unrelated AST files
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zygoloid
      </td>
    </tr>
</table>

<pre>
    In `SourceManager::getFileIDLoaded`, we try to map from the source location address space into a `FileID`, by doing a binary search across loaded `SLocEntry`s looking for the right one. This causes a bunch of unnecessary deserialization:

- It can deserialize unrelated `SLocEntry`s from the same AST file, if the probe point is close
- It can deserialize `SLocEntry`s from different AST files, if the probe point is not close

This is redundant and potentially expensive work, especially when building with `-fmodules-embed-all-files` -- in that case, loading an `SLocEntry` also loads and decompresses the embedded file contents.

The AST reader knows which range of the source location address space it assigned to each module file, so it can do this *much* more efficiently, by only doing a search within the right AST file. It might also be worthwhile to embed a table of `SLocEntry` starting offsets in the AST file so that we can map a source location to a `SLocEntry` without doing any redundant deserialization. Or, failing that, move the binary search inside the `ASTReader` and have it just look at the first field in each `SM_SLOC_FILE_ENTRY` to see if it's found the right one, rather than deserializing the whole thing (including the contents) and registering it with the `SourceManager`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVUFv2zgT_TXyZWBDoWN99sEHf20NBEi3QJPLngKKHIlsKFLgjOJ1f_1iaDuJs-1iLw4kUvPem_dmool8HxG31er_1erzTE_sUt7-PPYpJG9nbbLH7V2Eqqkf0pQNftVR95ir5a5a7nrkvQ949_k-aYu2aupKfYIDAucjcIJBj9DlNAA7BCrfQ0hGs08RtLUZiYBGbRB85ARacE4Vz7XaI9jkYw8aWh91PgKhzsaBNjkRQSjAhd59Ml8i52PV1PI-PctnXcoFPPveMaSIC3h0nsDoiZCk6hSNg9TBFCMa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