浏览代码

Improve allocation behaviour of MADT parser

Matt Taylor 5 年之前
父节点
当前提交
9b3693c994
共有 2 个文件被更改,包括 21 次插入4 次删除
  1. 1 1
      acpi/src/lib.rs
  2. 20 3
      acpi/src/madt.rs

+ 1 - 1
acpi/src/lib.rs

@@ -205,7 +205,7 @@ where
         interrupt_model: None,
         hpet: None,
         dsdt: None,
-        ssdts: Vec::with_capacity(0),
+        ssdts: Vec::new(),
         pci_config_regions: None,
     };
 

+ 20 - 3
acpi/src/madt.rs

@@ -394,10 +394,27 @@ fn parse_apic_model(acpi: &mut Acpi, mapping: &PhysicalMapping<Madt>) -> Result<
     use crate::interrupt::LocalInterruptLine;
 
     let mut local_apic_address = (*mapping).local_apic_address as u64;
-    let mut io_apics = Vec::new();
     let mut local_apic_nmi_line = None;
-    let mut interrupt_source_overrides = Vec::new();
-    let mut nmi_sources = Vec::new();
+    let mut io_apic_count = 0;
+    let mut iso_count = 0;
+    let mut nmi_source_count = 0;
+    let mut processor_count = 0usize;
+
+    // Do a pass over the entries so we know how much space we should reserve in the vectors
+    for entry in (*mapping).entries() {
+        match entry {
+            MadtEntry::IoApic(_) => io_apic_count += 1,
+            MadtEntry::InterruptSourceOverride(_) => iso_count += 1,
+            MadtEntry::NmiSource(_) => nmi_source_count += 1,
+            MadtEntry::LocalApic(_) => processor_count += 1,
+            _ => (),
+        }
+    }
+
+    let mut io_apics = Vec::with_capacity(io_apic_count);
+    let mut interrupt_source_overrides = Vec::with_capacity(iso_count);
+    let mut nmi_sources = Vec::with_capacity(nmi_source_count);
+    acpi.application_processors = Vec::with_capacity(processor_count.saturating_sub(1)); // Subtract one for the BSP
 
     for entry in (*mapping).entries() {
         match entry {