Skip to content

Commit 37ba4f8

Browse files
committed
Add read::EntriesRaw::read_attributes
1 parent 8def963 commit 37ba4f8

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

benches/bench.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ fn bench_read() {
135135
"read::EntriesRaw::read_attribute_inline",
136136
bench_entries_raw_inline,
137137
);
138+
c.bench_function("read::EntriesRaw::read_attributes", bench_entries_raw_bulk);
138139

139140
let mut c = Criterion::default().configure_from_args();
140141
c.bench_function("parse .debug_abbrev", bench_parsing_debug_abbrev);
@@ -378,6 +379,44 @@ fn bench_entries_raw_inline(b: &mut Bencher) {
378379
});
379380
}
380381

382+
fn bench_entries_raw_bulk(b: &mut Bencher) {
383+
let debug_abbrev = read_section("debug_abbrev");
384+
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);
385+
386+
let debug_info = read_section("debug_info");
387+
388+
b.iter(|| {
389+
let debug_info = DebugInfo::new(&debug_info, LittleEndian);
390+
391+
let mut attrs = Vec::new();
392+
let mut iter = debug_info.units();
393+
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
394+
let abbrevs = unit
395+
.abbreviations(&debug_abbrev)
396+
.expect("Should parse abbreviations");
397+
398+
let mut raw = unit
399+
.entries_raw(&abbrevs, None)
400+
.expect("Should have entries");
401+
while !raw.is_empty() {
402+
if let Some(abbrev) = raw
403+
.read_abbreviation()
404+
.expect("Should parse abbreviation code")
405+
{
406+
raw.read_attributes(abbrev.attributes(), &mut attrs)
407+
.expect("Should parse attributes");
408+
for attr in &attrs {
409+
let name = attr.name();
410+
black_box(name);
411+
let value = attr.raw_value();
412+
black_box(value);
413+
}
414+
}
415+
}
416+
}
417+
});
418+
}
419+
381420
fn bench_parsing_debug_aranges(b: &mut Bencher) {
382421
let debug_aranges = read_section("debug_aranges");
383422
let debug_aranges = DebugAranges::new(&debug_aranges, LittleEndian);

src/read/unit.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Functions for parsing DWARF `.debug_info` and `.debug_types` sections.
22
3+
use alloc::vec::Vec;
34
use core::cell::Cell;
45
use core::ops::{Range, RangeFrom, RangeTo};
56

@@ -2587,6 +2588,28 @@ impl<'abbrev, 'unit, R: Reader> EntriesRaw<'abbrev, 'unit, R> {
25872588
parse_attribute(&mut self.input, self.unit.encoding(), spec)
25882589
}
25892590

2591+
/// Read all attributes into a `Vec`.
2592+
///
2593+
/// This will clear `attrs` before reading.
2594+
///
2595+
/// It is recommended to reuse the same `Vec` for multiple calls to avoid allocations.
2596+
pub fn read_attributes(
2597+
&mut self,
2598+
specs: &[AttributeSpecification],
2599+
attrs: &mut Vec<Attribute<R>>,
2600+
) -> Result<()> {
2601+
attrs.clear();
2602+
attrs.reserve(specs.len());
2603+
for spec in specs {
2604+
attrs.push(parse_attribute(
2605+
&mut self.input,
2606+
self.unit.encoding(),
2607+
*spec,
2608+
)?);
2609+
}
2610+
Ok(())
2611+
}
2612+
25902613
/// Skip all the attributes of an abbreviation.
25912614
#[inline]
25922615
pub fn skip_attributes(&mut self, specs: &[AttributeSpecification]) -> Result<()> {

0 commit comments

Comments
 (0)