@@ -38,6 +38,7 @@ Error dumpDebugAbbrev(DWARFContext &DCtx, DWARFYAML::Data &Y) {
3838 Abbrv.Tag = AbbrvDecl.getTag ();
3939 Abbrv.Children = AbbrvDecl.hasChildren () ? dwarf::DW_CHILDREN_yes
4040 : dwarf::DW_CHILDREN_no;
41+ Abbrv.Attributes .reserve (AbbrvDecl.getNumAttributes ());
4142 for (auto Attribute : AbbrvDecl.attributes ()) {
4243 DWARFYAML::AttributeAbbrev AttAbrv;
4344 AttAbrv.Attribute = Attribute.Attr ;
@@ -46,7 +47,7 @@ Error dumpDebugAbbrev(DWARFContext &DCtx, DWARFYAML::Data &Y) {
4647 AttAbrv.Value = Attribute.getImplicitConstValue ();
4748 Abbrv.Attributes .push_back (AttAbrv);
4849 }
49- Y.DebugAbbrev .back ().Table .push_back (Abbrv);
50+ Y.DebugAbbrev .back ().Table .push_back (std::move ( Abbrv) );
5051 }
5152 }
5253 }
@@ -67,6 +68,8 @@ Error dumpDebugAddr(DWARFContext &DCtx, DWARFYAML::Data &Y) {
6768 consumeError))
6869 return Err;
6970 AddrTables.emplace_back ();
71+ AddrTables.back ().SegAddrPairs .reserve (
72+ AddrTable.getAddressEntries ().size ());
7073 for (uint64_t Addr : AddrTable.getAddressEntries ()) {
7174 // Currently, the parser doesn't support parsing an address table with non
7275 // linear addresses (segment_selector_size != 0). The segment selectors
@@ -97,7 +100,7 @@ Error dumpDebugStrings(DWARFContext &DCtx, DWARFYAML::Data &Y) {
97100 DebugStr.push_back (CStr);
98101 }
99102
100- Y.DebugStrings = DebugStr;
103+ Y.DebugStrings = std::move ( DebugStr) ;
101104 return Err;
102105}
103106
@@ -123,16 +126,19 @@ Error dumpDebugARanges(DWARFContext &DCtx, DWARFYAML::Data &Y) {
123126 Range.CuOffset = Set.getHeader ().CuOffset ;
124127 Range.AddrSize = Set.getHeader ().AddrSize ;
125128 Range.SegSize = Set.getHeader ().SegSize ;
129+
130+ Range.Descriptors .reserve (Set.descriptors ().end () -
131+ Set.descriptors ().begin ());
126132 for (auto Descriptor : Set.descriptors ()) {
127133 DWARFYAML::ARangeDescriptor Desc;
128134 Desc.Address = Descriptor.Address ;
129135 Desc.Length = Descriptor.Length ;
130136 Range.Descriptors .push_back (Desc);
131137 }
132- DebugAranges.push_back (Range);
138+ DebugAranges.push_back (std::move ( Range) );
133139 }
134140
135- Y.DebugAranges = DebugAranges;
141+ Y.DebugAranges = std::move ( DebugAranges) ;
136142 return ErrorSuccess ();
137143}
138144
@@ -161,12 +167,13 @@ Error dumpDebugRanges(DWARFContext &DCtx, DWARFYAML::Data &Y) {
161167 YamlRanges.AddrSize = AddrSize;
162168 if (Error E = DwarfRanges.extract (Data, &Offset))
163169 return E;
170+ YamlRanges.Entries .reserve (DwarfRanges.getEntries ().size ());
164171 for (const auto &RLE : DwarfRanges.getEntries ())
165172 YamlRanges.Entries .push_back ({RLE.StartAddress , RLE.EndAddress });
166173 DebugRanges.push_back (std::move (YamlRanges));
167174 }
168175
169- Y.DebugRanges = DebugRanges;
176+ Y.DebugRanges = std::move ( DebugRanges) ;
170177 return ErrorSuccess ();
171178}
172179
@@ -192,6 +199,7 @@ dumpPubSection(const DWARFContext &DCtx, const DWARFSection &Section,
192199 Y.UnitOffset = Sets[0 ].Offset ;
193200 Y.UnitSize = Sets[0 ].Size ;
194201
202+ Y.Entries .reserve (Sets[0 ].Entries .size ());
195203 for (const DWARFDebugPubTable::Entry &E : Sets[0 ].Entries )
196204 Y.Entries .push_back (DWARFYAML::PubEntry{(uint32_t )E.SecOffset ,
197205 E.Descriptor .toBits (), E.Name });
@@ -215,6 +223,7 @@ void dumpDebugPubSections(DWARFContext &DCtx, DWARFYAML::Data &Y) {
215223}
216224
217225void dumpDebugInfo (DWARFContext &DCtx, DWARFYAML::Data &Y) {
226+ Y.Units .reserve (DCtx.getNumCompileUnits ());
218227 for (const auto &CU : DCtx.compile_units ()) {
219228 DWARFYAML::Unit NewUnit;
220229 NewUnit.Format = CU->getFormat ();
@@ -238,6 +247,7 @@ void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
238247 }));
239248 NewUnit.AbbrOffset = CU->getAbbreviations ()->getOffset ();
240249 NewUnit.AddrSize = CU->getAddressByteSize ();
250+ NewUnit.Entries .reserve (CU->getNumDIEs ());
241251 for (auto DIE : CU->dies ()) {
242252 DWARFYAML::Entry NewEntry;
243253 DataExtractor EntryData = CU->getDebugInfoExtractor ();
@@ -251,6 +261,11 @@ void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
251261
252262 auto AbbrevDecl = DIE.getAbbreviationDeclarationPtr ();
253263 if (AbbrevDecl) {
264+ // This reserve doesn't account for DW_FORM_indirect values, which would
265+ // result in more entries in NewEntry.Values than getNumAttributes()
266+ // implies. Not all binaries have these, and it'll reduce the number of
267+ // allocations in any case.
268+ NewEntry.Values .reserve (AbbrevDecl->getNumAttributes ());
254269 for (const auto &AttrSpec : AbbrevDecl->attributes ()) {
255270 DWARFYAML::FormValue NewValue;
256271 NewValue.Value = 0xDEADBEEFDEADBEEF ;
@@ -332,13 +347,13 @@ void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
332347 break ;
333348 }
334349 } while (indirect);
335- NewEntry.Values .push_back (NewValue);
350+ NewEntry.Values .push_back (std::move ( NewValue) );
336351 }
337352 }
338353
339- NewUnit.Entries .push_back (NewEntry);
354+ NewUnit.Entries .push_back (std::move ( NewEntry) );
340355 }
341- Y.Units .push_back (NewUnit);
356+ Y.Units .push_back (std::move ( NewUnit) );
342357 }
343358}
344359
@@ -465,9 +480,9 @@ void dumpDebugLines(DWARFContext &DCtx, DWARFYAML::Data &Y) {
465480 NewOp.StandardOpcodeData .push_back (LineData.getULEB128 (&Offset));
466481 }
467482 }
468- DebugLines.Opcodes .push_back (NewOp);
483+ DebugLines.Opcodes .push_back (std::move ( NewOp) );
469484 }
470- Y.DebugLines .push_back (DebugLines);
485+ Y.DebugLines .push_back (std::move ( DebugLines) );
471486 }
472487 }
473488}
0 commit comments