Skip to content

Commit 652c69c

Browse files
jrose-appleDavide Italiano
authored andcommitted
[Serialization] Use local BumpPtrAllocator for fixed-sized buffers
...instead of std::vector, which (1) will always make separate allocations, and (2) has features and overhead we don't need I don't expect this to actually affect performance too much, but it seems more correct for what Serialization needs anyway.
1 parent 0fcbe72 commit 652c69c

File tree

4 files changed

+71
-36
lines changed

4 files changed

+71
-36
lines changed

include/swift/Serialization/ModuleFile.h

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,32 +285,56 @@ class ModuleFile
285285
};
286286

287287
private:
288+
/// An allocator for buffers owned by the file.
289+
llvm::BumpPtrAllocator Allocator;
290+
291+
/// Allocates a buffer using #Allocator and initializes it with the contents
292+
/// of the container \p rawData, then stores it in \p buffer.
293+
///
294+
/// \p buffer is passed as an argument rather than returned so that the
295+
/// element type can be inferred.
296+
template <typename T, typename RawData>
297+
void allocateBuffer(MutableArrayRef<T> &buffer, const RawData &rawData);
298+
299+
/// Allocates a buffer using #Allocator and initializes it with the contents
300+
/// of the container \p rawData, then stores it in \p buffer.
301+
///
302+
/// \p buffer is passed as an argument rather than returned so that the
303+
/// element type can be inferred.
304+
template <typename T, typename RawData>
305+
void allocateBuffer(ArrayRef<T> &buffer, const RawData &rawData) {
306+
assert(buffer.empty());
307+
MutableArrayRef<T> result;
308+
allocateBuffer(result, rawData);
309+
buffer = result;
310+
}
311+
288312
/// Decls referenced by this module.
289-
std::vector<Serialized<Decl*>> Decls;
313+
MutableArrayRef<Serialized<Decl*>> Decls;
290314

291315
/// DeclContexts referenced by this module.
292-
std::vector<Serialized<DeclContext*>> DeclContexts;
316+
MutableArrayRef<Serialized<DeclContext*>> DeclContexts;
293317

294318
/// Local DeclContexts referenced by this module.
295-
std::vector<Serialized<DeclContext*>> LocalDeclContexts;
319+
MutableArrayRef<Serialized<DeclContext*>> LocalDeclContexts;
296320

297321
/// Normal protocol conformances referenced by this module.
298-
std::vector<Serialized<NormalProtocolConformance *>> NormalConformances;
322+
MutableArrayRef<Serialized<NormalProtocolConformance *>> NormalConformances;
299323

300324
/// SILLayouts referenced by this module.
301-
std::vector<Serialized<SILLayout *>> SILLayouts;
325+
MutableArrayRef<Serialized<SILLayout *>> SILLayouts;
302326

303327
/// Types referenced by this module.
304-
std::vector<Serialized<Type>> Types;
328+
MutableArrayRef<Serialized<Type>> Types;
305329

306330
/// Generic signatures referenced by this module.
307-
std::vector<Serialized<GenericSignature *>> GenericSignatures;
331+
MutableArrayRef<Serialized<GenericSignature *>> GenericSignatures;
308332

309333
/// Generic environments referenced by this module.
310-
std::vector<Serialized<GenericEnvironment *>> GenericEnvironments;
334+
MutableArrayRef<Serialized<GenericEnvironment *>> GenericEnvironments;
311335

312336
/// Substitution maps referenced by this module.
313-
std::vector<Serialized<SubstitutionMap>> SubstitutionMaps;
337+
MutableArrayRef<Serialized<SubstitutionMap>> SubstitutionMaps;
314338

315339
/// Represents an identifier that may or may not have been deserialized yet.
316340
///
@@ -328,7 +352,7 @@ class ModuleFile
328352
};
329353

330354
/// Identifiers referenced by this module.
331-
std::vector<SerializedIdentifier> Identifiers;
355+
MutableArrayRef<SerializedIdentifier> Identifiers;
332356

333357
class DeclTableInfo;
334358
using SerializedDeclTable =
@@ -377,9 +401,7 @@ class ModuleFile
377401

378402
TinyPtrVector<Decl *> ImportDecls;
379403

380-
using DeclIDVector = SmallVector<serialization::DeclID, 4>;
381-
382-
DeclIDVector OrderedTopLevelDecls;
404+
ArrayRef<serialization::DeclID> OrderedTopLevelDecls;
383405

384406
class DeclCommentTableInfo;
385407
using SerializedDeclCommentTable =
@@ -864,6 +886,19 @@ class ModuleFile
864886
Optional<ForeignErrorConvention> maybeReadForeignErrorConvention();
865887
};
866888

889+
template <typename T, typename RawData>
890+
void ModuleFile::allocateBuffer(MutableArrayRef<T> &buffer,
891+
const RawData &rawData) {
892+
assert(buffer.empty() && "reallocating deserialized buffer");
893+
if (rawData.empty())
894+
return;
895+
896+
void *rawBuffer = Allocator.Allocate(sizeof(T) * rawData.size(), alignof(T));
897+
buffer = llvm::makeMutableArrayRef(static_cast<T *>(rawBuffer),
898+
rawData.size());
899+
std::uninitialized_copy(rawData.begin(), rawData.end(), buffer.begin());
900+
}
901+
867902
} // end namespace swift
868903

869904
#endif

lib/Serialization/DeserializeSIL.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ SILDeserializer::SILDeserializer(
182182
DefaultWitnessTableList = readFuncTable(scratch, blobData);
183183
else if (kind == sil_index_block::SIL_PROPERTY_OFFSETS) {
184184
// No matching 'names' block for property descriptors needed yet.
185-
Properties.assign(scratch.begin(), scratch.end());
185+
MF->allocateBuffer(Properties, scratch);
186186
return;
187187
}
188188

@@ -195,27 +195,27 @@ SILDeserializer::SILDeserializer(
195195
assert((next.Kind == llvm::BitstreamEntry::Record &&
196196
offKind == sil_index_block::SIL_FUNC_OFFSETS) &&
197197
"Expect a SIL_FUNC_OFFSETS record.");
198-
Funcs.assign(scratch.begin(), scratch.end());
198+
MF->allocateBuffer(Funcs, scratch);
199199
} else if (kind == sil_index_block::SIL_VTABLE_NAMES) {
200200
assert((next.Kind == llvm::BitstreamEntry::Record &&
201201
offKind == sil_index_block::SIL_VTABLE_OFFSETS) &&
202202
"Expect a SIL_VTABLE_OFFSETS record.");
203-
VTables.assign(scratch.begin(), scratch.end());
203+
MF->allocateBuffer(VTables, scratch);
204204
} else if (kind == sil_index_block::SIL_GLOBALVAR_NAMES) {
205205
assert((next.Kind == llvm::BitstreamEntry::Record &&
206206
offKind == sil_index_block::SIL_GLOBALVAR_OFFSETS) &&
207207
"Expect a SIL_GLOBALVAR_OFFSETS record.");
208-
GlobalVars.assign(scratch.begin(), scratch.end());
208+
MF->allocateBuffer(GlobalVars, scratch);
209209
} else if (kind == sil_index_block::SIL_WITNESS_TABLE_NAMES) {
210210
assert((next.Kind == llvm::BitstreamEntry::Record &&
211211
offKind == sil_index_block::SIL_WITNESS_TABLE_OFFSETS) &&
212212
"Expect a SIL_WITNESS_TABLE_OFFSETS record.");
213-
WitnessTables.assign(scratch.begin(), scratch.end());
213+
MF->allocateBuffer(WitnessTables, scratch);
214214
} else if (kind == sil_index_block::SIL_DEFAULT_WITNESS_TABLE_NAMES) {
215215
assert((next.Kind == llvm::BitstreamEntry::Record &&
216216
offKind == sil_index_block::SIL_DEFAULT_WITNESS_TABLE_OFFSETS) &&
217217
"Expect a SIL_DEFAULT_WITNESS_TABLE_OFFSETS record.");
218-
DefaultWitnessTables.assign(scratch.begin(), scratch.end());
218+
MF->allocateBuffer(DefaultWitnessTables, scratch);
219219
}
220220
}
221221
}

lib/Serialization/DeserializeSIL.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ namespace swift {
3939
llvm::OnDiskIterableChainedHashTable<FuncTableInfo>;
4040

4141
std::unique_ptr<SerializedFuncTable> FuncTable;
42-
std::vector<ModuleFile::PartiallySerialized<SILFunction*>> Funcs;
42+
MutableArrayRef<ModuleFile::PartiallySerialized<SILFunction*>> Funcs;
4343

4444
std::unique_ptr<SerializedFuncTable> VTableList;
45-
std::vector<ModuleFile::Serialized<SILVTable*>> VTables;
45+
MutableArrayRef<ModuleFile::Serialized<SILVTable*>> VTables;
4646

4747
std::unique_ptr<SerializedFuncTable> GlobalVarList;
48-
std::vector<ModuleFile::Serialized<SILGlobalVariable*>> GlobalVars;
48+
MutableArrayRef<ModuleFile::Serialized<SILGlobalVariable*>> GlobalVars;
4949

5050
std::unique_ptr<SerializedFuncTable> WitnessTableList;
51-
std::vector<ModuleFile::PartiallySerialized<SILWitnessTable *>>
51+
MutableArrayRef<ModuleFile::PartiallySerialized<SILWitnessTable *>>
5252
WitnessTables;
5353

5454
std::unique_ptr<SerializedFuncTable> DefaultWitnessTableList;
55-
std::vector<ModuleFile::PartiallySerialized<SILDefaultWitnessTable *>>
55+
MutableArrayRef<ModuleFile::PartiallySerialized<SILDefaultWitnessTable *>>
5656
DefaultWitnessTables;
5757

58-
std::vector<ModuleFile::PartiallySerialized<SILProperty *>>
58+
MutableArrayRef<ModuleFile::PartiallySerialized<SILProperty *>>
5959
Properties;
6060

6161
/// A declaration will only

lib/Serialization/ModuleFile.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -796,19 +796,19 @@ bool ModuleFile::readIndexBlock(llvm::BitstreamCursor &cursor) {
796796
switch (kind) {
797797
case index_block::DECL_OFFSETS:
798798
assert(blobData.empty());
799-
Decls.assign(scratch.begin(), scratch.end());
799+
allocateBuffer(Decls, scratch);
800800
break;
801801
case index_block::DECL_CONTEXT_OFFSETS:
802802
assert(blobData.empty());
803-
DeclContexts.assign(scratch.begin(), scratch.end());
803+
allocateBuffer(DeclContexts, scratch);
804804
break;
805805
case index_block::TYPE_OFFSETS:
806806
assert(blobData.empty());
807-
Types.assign(scratch.begin(), scratch.end());
807+
allocateBuffer(Types, scratch);
808808
break;
809809
case index_block::IDENTIFIER_OFFSETS:
810810
assert(blobData.empty());
811-
Identifiers.assign(scratch.begin(), scratch.end());
811+
allocateBuffer(Identifiers, scratch);
812812
break;
813813
case index_block::TOP_LEVEL_DECLS:
814814
TopLevelDecls = readDeclTable(scratch, blobData);
@@ -836,7 +836,7 @@ bool ModuleFile::readIndexBlock(llvm::BitstreamCursor &cursor) {
836836
setEntryPointClassID(scratch.front());
837837
break;
838838
case index_block::ORDERED_TOP_LEVEL_DECLS:
839-
OrderedTopLevelDecls.assign(scratch.begin(), scratch.end());
839+
allocateBuffer(OrderedTopLevelDecls, scratch);
840840
break;
841841
case index_block::LOCAL_TYPE_DECLS:
842842
LocalTypeDecls = readLocalDeclTable(scratch, blobData);
@@ -849,27 +849,27 @@ bool ModuleFile::readIndexBlock(llvm::BitstreamCursor &cursor) {
849849
break;
850850
case index_block::LOCAL_DECL_CONTEXT_OFFSETS:
851851
assert(blobData.empty());
852-
LocalDeclContexts.assign(scratch.begin(), scratch.end());
852+
allocateBuffer(LocalDeclContexts, scratch);
853853
break;
854854
case index_block::GENERIC_SIGNATURE_OFFSETS:
855855
assert(blobData.empty());
856-
GenericSignatures.assign(scratch.begin(), scratch.end());
856+
allocateBuffer(GenericSignatures, scratch);
857857
break;
858858
case index_block::GENERIC_ENVIRONMENT_OFFSETS:
859859
assert(blobData.empty());
860-
GenericEnvironments.assign(scratch.begin(), scratch.end());
860+
allocateBuffer(GenericEnvironments, scratch);
861861
break;
862862
case index_block::SUBSTITUTION_MAP_OFFSETS:
863863
assert(blobData.empty());
864-
SubstitutionMaps.assign(scratch.begin(), scratch.end());
864+
allocateBuffer(SubstitutionMaps, scratch);
865865
break;
866866
case index_block::NORMAL_CONFORMANCE_OFFSETS:
867867
assert(blobData.empty());
868-
NormalConformances.assign(scratch.begin(), scratch.end());
868+
allocateBuffer(NormalConformances, scratch);
869869
break;
870870
case index_block::SIL_LAYOUT_OFFSETS:
871871
assert(blobData.empty());
872-
SILLayouts.assign(scratch.begin(), scratch.end());
872+
allocateBuffer(SILLayouts, scratch);
873873
break;
874874

875875
default:

0 commit comments

Comments
 (0)