From b1229ab822df9b413cbdfb4adaa1776b129d8b49 Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 1 Dec 2022 21:21:57 +0100 Subject: [PATCH] Optimize parsing with attr.path.is_ident() --- pyo3-macros-backend/src/attributes.rs | 9 +++++---- pyo3-macros-backend/src/konst.rs | 17 +++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pyo3-macros-backend/src/attributes.rs b/pyo3-macros-backend/src/attributes.rs index 86b429898ee..8ea0ff3e05d 100644 --- a/pyo3-macros-backend/src/attributes.rs +++ b/pyo3-macros-backend/src/attributes.rs @@ -195,12 +195,13 @@ pub fn take_pyo3_options(attrs: &mut Vec) -> Result> } Ok(false) }; - if let Ok(mut meta) = attr.parse_meta() { - if handle_cfg_feature_pyo3(&mut attr, &mut meta, parse_attr)? { - continue; + if attr.path.is_ident("cfg_attr") { + if let Ok(mut meta) = attr.parse_meta() { + if handle_cfg_feature_pyo3(&mut attr, &mut meta, parse_attr)? { + continue; + } } } - if let Some(options) = get_pyo3_options(&attr)? { out.extend(options.into_iter()); continue; diff --git a/pyo3-macros-backend/src/konst.rs b/pyo3-macros-backend/src/konst.rs index 83be903d00b..5dda8e59309 100644 --- a/pyo3-macros-backend/src/konst.rs +++ b/pyo3-macros-backend/src/konst.rs @@ -70,13 +70,18 @@ impl ConstAttributes { for mut attr in attrs.drain(..) { let parse_attr = |meta, _attr: &Attribute| parse_attribute(&mut attributes, &meta); - if let Ok(mut meta) = attr.parse_meta() { - if handle_cfg_feature_pyo3(&mut attr, &mut meta, parse_attr)? { - continue; - } + if attr.path.is_ident("cfg_attr") + || attr.path.is_ident("classattr") + || attr.path.is_ident("pyo3") + { + if let Ok(mut meta) = attr.parse_meta() { + if handle_cfg_feature_pyo3(&mut attr, &mut meta, parse_attr)? { + continue; + } - if parse_attribute(&mut attributes, &meta)? { - continue; + if parse_attribute(&mut attributes, &meta)? { + continue; + } } } new_attrs.push(attr)