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


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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to