[PATCH] D124422: [Serialization] Improve encoding of small SourceRanges.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 25 16:17:06 PDT 2022


sammccall created this revision.
sammccall added a reviewer: ilya-biryukov.
Herald added subscribers: usaxena95, kadircet.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Instead of encoding as (start, end), encode as (start, end-start).
This is smaller in the common case when:

- we store the values as VBR
- start and end are in the same file ID and nearby

This reduces clangd/AST.cpp PCH size by ~0.75%.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124422

Files:
  clang/lib/Serialization/ASTReader.cpp
  clang/lib/Serialization/ASTWriter.cpp


Index: clang/lib/Serialization/ASTWriter.cpp
===================================================================
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -5202,14 +5202,19 @@
   Record.push_back(Raw);
 }
 
-void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
+static SourceLocation::UIntTy encodeForWrite(SourceLocation Loc) {
   SourceLocation::UIntTy Raw = Loc.getRawEncoding();
-  Record.push_back((Raw << 1) | (Raw >> (8 * sizeof(Raw) - 1)));
+  return SourceLocation::UIntTy{(Raw << 1) | (Raw >> (8 * sizeof(Raw) - 1))};
+}
+
+void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
+  Record.push_back(encodeForWrite(Loc));
 }
 
 void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
   AddSourceLocation(Range.getBegin(), Record);
-  AddSourceLocation(Range.getEnd(), Record);
+  Record.push_back(encodeForWrite(Range.getEnd()) -
+                   encodeForWrite(Range.getBegin()));
 }
 
 void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
Index: clang/lib/Serialization/ASTReader.cpp
===================================================================
--- clang/lib/Serialization/ASTReader.cpp
+++ clang/lib/Serialization/ASTReader.cpp
@@ -8992,9 +8992,12 @@
 SourceRange
 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
                            unsigned &Idx) {
-  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
-  SourceLocation end = ReadSourceLocation(F, Record, Idx);
-  return SourceRange(beg, end);
+  SourceLocation::UIntTy Begin = Record[Idx++];
+  SourceLocation::UIntTy Delta = Record[Idx++];
+  return SourceRange(
+      TranslateSourceLocation(F, ReadUntranslatedSourceLocation(Begin)),
+      TranslateSourceLocation(F,
+                              ReadUntranslatedSourceLocation(Begin + Delta)));
 }
 
 /// Read a floating-point value


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124422.425056.patch
Type: text/x-patch
Size: 1928 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220425/0141ab9f/attachment-0001.bin>


More information about the cfe-commits mailing list