From 6492f9314c13660c3ece30fdb1857efe230701bb 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 +++++++++++------ pyo3-macros-backend/src/method.rs | 1 - 3 files changed, 16 insertions(+), 11 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) diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index e0db030bffe..0c8d032e484 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -9,7 +9,6 @@ use crate::utils::{self, PythonDoc}; use proc_macro2::{Span, TokenStream}; use quote::ToTokens; use quote::{quote, quote_spanned}; -use std::borrow::Cow; use syn::ext::IdentExt; use syn::spanned::Spanned; use syn::{Attribute, Meta, Result};