Parcourir la source

Don't reallocate when normalizing an AmlName that is already normal

Isaac Woods il y a 4 ans
Parent
commit
cb3838920b
1 fichiers modifiés avec 9 ajouts et 1 suppressions
  1. 9 1
      aml/src/namespace.rs

+ 9 - 1
aml/src/namespace.rs

@@ -228,7 +228,15 @@ impl AmlName {
     /// Normalize an AML path, resolving prefix chars. Returns `AmlError::InvalidNormalizedName` if the path
     /// normalizes to an invalid path (e.g. `\^_FOO`)
     pub fn normalize(self) -> Result<AmlName, AmlError> {
-        Ok(AmlName(self.0.iter().try_fold(alloc::vec![], |mut name, &component| match component {
+        /*
+         * If the path is already normal, just return it as-is. This avoids an unneccessary heap allocation and
+         * free.
+         */
+        if self.is_normal() {
+            return Ok(self);
+        }
+
+        Ok(AmlName(self.0.iter().try_fold(Vec::new(), |mut name, &component| match component {
             seg @ NameComponent::Segment(_) => {
                 name.push(seg);
                 Ok(name)