diff --git a/Cargo.lock b/Cargo.lock index 037becf700..48260c5a40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -935,6 +935,7 @@ dependencies = [ "anyhow", "clap 4.0.22", "configparser", + "fnv", "once_cell", "regex", "ruff", diff --git a/flake8_to_ruff/Cargo.toml b/flake8_to_ruff/Cargo.toml index 2a8d90fe38..efaab3af22 100644 --- a/flake8_to_ruff/Cargo.toml +++ b/flake8_to_ruff/Cargo.toml @@ -10,6 +10,7 @@ name = "flake8_to_ruff" anyhow = { version = "1.0.66" } clap = { version = "4.0.1", features = ["derive"] } configparser = { version = "3.0.2" } +fnv = { version = "1.0.7" } once_cell = { version = "1.16.0" } regex = { version = "1.6.0" } ruff = { path = "..", default-features = false } diff --git a/flake8_to_ruff/src/parser.rs b/flake8_to_ruff/src/parser.rs index 5392239a70..87144cb8c1 100644 --- a/flake8_to_ruff/src/parser.rs +++ b/flake8_to_ruff/src/parser.rs @@ -1,7 +1,7 @@ -use std::collections::BTreeMap; use std::str::FromStr; use anyhow::Result; +use fnv::FnvHashMap; use once_cell::sync::Lazy; use regex::Regex; use ruff::checks_gen::CheckCodePrefix; @@ -179,8 +179,8 @@ pub fn parse_files_to_codes_mapping(value: &str) -> Result, -) -> BTreeMap> { - let mut per_file_ignores: BTreeMap> = BTreeMap::new(); +) -> FnvHashMap> { + let mut per_file_ignores: FnvHashMap> = FnvHashMap::default(); for pair in pairs { per_file_ignores .entry(pair.pattern) diff --git a/src/autofix/fixer.rs b/src/autofix/fixer.rs index 8cb17b6622..6eb7125541 100644 --- a/src/autofix/fixer.rs +++ b/src/autofix/fixer.rs @@ -61,7 +61,7 @@ fn apply_fixes<'a>( ) -> Cow<'a, str> { let mut output = RopeBuilder::new(); let mut last_pos: Location = Location::new(1, 0); - let mut applied: BTreeSet<&Patch> = BTreeSet::new(); + let mut applied: BTreeSet<&Patch> = BTreeSet::default(); for fix in fixes.sorted_by_key(|fix| fix.patch.location) { // If we already applied an identical fix as part of another correction, skip diff --git a/src/cli.rs b/src/cli.rs index 4fa010f575..dd4564d8c8 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,8 @@ -use std::collections::BTreeMap; use std::fmt; use std::path::PathBuf; use clap::{command, Parser}; +use fnv::FnvHashMap; use log::warn; use regex::Regex; @@ -188,7 +188,7 @@ pub fn collect_per_file_ignores( pairs: Vec, project_root: &Option, ) -> Vec { - let mut per_file_ignores: BTreeMap> = BTreeMap::new(); + let mut per_file_ignores: FnvHashMap> = FnvHashMap::default(); for pair in pairs { per_file_ignores .entry(pair.pattern) diff --git a/src/docstrings/google.rs b/src/docstrings/google.rs index 22169a8c5d..06a76fc31e 100644 --- a/src/docstrings/google.rs +++ b/src/docstrings/google.rs @@ -1,11 +1,10 @@ //! Abstractions for Google-style docstrings. -use std::collections::BTreeSet; - +use fnv::FnvHashSet; use once_cell::sync::Lazy; -pub(crate) static GOOGLE_SECTION_NAMES: Lazy> = Lazy::new(|| { - BTreeSet::from([ +pub(crate) static GOOGLE_SECTION_NAMES: Lazy> = Lazy::new(|| { + FnvHashSet::from_iter([ "Args", "Arguments", "Attention", @@ -37,35 +36,36 @@ pub(crate) static GOOGLE_SECTION_NAMES: Lazy> = Lazy::new ]) }); -pub(crate) static LOWERCASE_GOOGLE_SECTION_NAMES: Lazy> = Lazy::new(|| { - BTreeSet::from([ - "args", - "arguments", - "attention", - "attributes", - "caution", - "danger", - "error", - "example", - "examples", - "hint", - "important", - "keyword args", - "keyword arguments", - "methods", - "note", - "notes", - "return", - "returns", - "raises", - "references", - "see also", - "tip", - "todo", - "warning", - "warnings", - "warns", - "yield", - "yields", - ]) -}); +pub(crate) static LOWERCASE_GOOGLE_SECTION_NAMES: Lazy> = + Lazy::new(|| { + FnvHashSet::from_iter([ + "args", + "arguments", + "attention", + "attributes", + "caution", + "danger", + "error", + "example", + "examples", + "hint", + "important", + "keyword args", + "keyword arguments", + "methods", + "note", + "notes", + "return", + "returns", + "raises", + "references", + "see also", + "tip", + "todo", + "warning", + "warnings", + "warns", + "yield", + "yields", + ]) + }); diff --git a/src/docstrings/numpy.rs b/src/docstrings/numpy.rs index 27cf8ac919..a5cb342e7e 100644 --- a/src/docstrings/numpy.rs +++ b/src/docstrings/numpy.rs @@ -1,11 +1,10 @@ //! Abstractions for NumPy-style docstrings. -use std::collections::BTreeSet; - +use fnv::FnvHashSet; use once_cell::sync::Lazy; -pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy> = Lazy::new(|| { - BTreeSet::from([ +pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy> = Lazy::new(|| { + FnvHashSet::from_iter([ "short summary", "extended summary", "parameters", @@ -22,8 +21,8 @@ pub(crate) static LOWERCASE_NUMPY_SECTION_NAMES: Lazy> = ]) }); -pub(crate) static NUMPY_SECTION_NAMES: Lazy> = Lazy::new(|| { - BTreeSet::from([ +pub(crate) static NUMPY_SECTION_NAMES: Lazy> = Lazy::new(|| { + FnvHashSet::from_iter([ "Short Summary", "Extended Summary", "Parameters", diff --git a/src/docstrings/styles.rs b/src/docstrings/styles.rs index e19597f950..33388674da 100644 --- a/src/docstrings/styles.rs +++ b/src/docstrings/styles.rs @@ -1,5 +1,4 @@ -use std::collections::BTreeSet; - +use fnv::FnvHashSet; use once_cell::sync::Lazy; use crate::docstrings::google::{GOOGLE_SECTION_NAMES, LOWERCASE_GOOGLE_SECTION_NAMES}; @@ -11,14 +10,14 @@ pub(crate) enum SectionStyle { } impl SectionStyle { - pub(crate) fn section_names(&self) -> &Lazy> { + pub(crate) fn section_names(&self) -> &Lazy> { match self { SectionStyle::NumPy => &NUMPY_SECTION_NAMES, SectionStyle::Google => &GOOGLE_SECTION_NAMES, } } - pub(crate) fn lowercase_section_names(&self) -> &Lazy> { + pub(crate) fn lowercase_section_names(&self) -> &Lazy> { match self { SectionStyle::NumPy => &LOWERCASE_NUMPY_SECTION_NAMES, SectionStyle::Google => &LOWERCASE_GOOGLE_SECTION_NAMES, diff --git a/src/fs.rs b/src/fs.rs index 888bf276d6..a90091cd90 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,11 +1,11 @@ use std::borrow::Cow; -use std::collections::BTreeSet; use std::fs::File; use std::io::{BufReader, Read}; use std::ops::Deref; use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; +use fnv::FnvHashSet; use log::debug; use path_absolutize::{path_dedot, Absolutize}; use walkdir::{DirEntry, WalkDir}; @@ -121,7 +121,7 @@ pub fn iter_python_files<'a>( pub(crate) fn ignores_from_path<'a>( path: &Path, pattern_code_pairs: &'a [PerFileIgnore], -) -> Result> { +) -> Result> { let (file_path, file_basename) = extract_path_names(path)?; Ok(pattern_code_pairs .iter() diff --git a/src/pydocstyle/plugins.rs b/src/pydocstyle/plugins.rs index ba9e613ec2..6c33564932 100644 --- a/src/pydocstyle/plugins.rs +++ b/src/pydocstyle/plugins.rs @@ -1,5 +1,6 @@ use std::collections::BTreeSet; +use fnv::FnvHashSet; use itertools::Itertools; use once_cell::sync::Lazy; use regex::Regex; @@ -1287,7 +1288,11 @@ fn common_section( blanks_and_section_underline(checker, definition, context); } -fn missing_args(checker: &mut Checker, definition: &Definition, docstrings_args: &BTreeSet<&str>) { +fn missing_args( + checker: &mut Checker, + definition: &Definition, + docstrings_args: &FnvHashSet<&str>, +) { if let DefinitionKind::Function(parent) | DefinitionKind::NestedFunction(parent) | DefinitionKind::Method(parent) = definition.kind @@ -1377,7 +1382,7 @@ fn args_section(checker: &mut Checker, definition: &Definition, context: &Sectio checker, definition, // Collect the list of arguments documented in the docstring. - &BTreeSet::from_iter(args_sections.iter().filter_map(|section| { + &FnvHashSet::from_iter(args_sections.iter().filter_map(|section| { match GOOGLE_ARGS_REGEX.captures(section.as_str()) { Some(caps) => caps.get(1).map(|arg_name| arg_name.as_str()), None => None, @@ -1388,7 +1393,7 @@ fn args_section(checker: &mut Checker, definition: &Definition, context: &Sectio fn parameters_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) { // Collect the list of arguments documented in the docstring. - let mut docstring_args: BTreeSet<&str> = Default::default(); + let mut docstring_args: FnvHashSet<&str> = FnvHashSet::default(); let section_level_indent = helpers::leading_space(context.line); for i in 1..context.following_lines.len() { let current_line = context.following_lines[i - 1]; diff --git a/src/pyflakes/checks.rs b/src/pyflakes/checks.rs index 29bc7e8ff3..711137e88d 100644 --- a/src/pyflakes/checks.rs +++ b/src/pyflakes/checks.rs @@ -1,5 +1,4 @@ -use std::collections::BTreeSet; - +use fnv::FnvHashSet; use regex::Regex; use rustpython_parser::ast::{ Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt, StmtKind, @@ -113,7 +112,7 @@ pub fn duplicate_arguments(arguments: &Arguments) -> Vec { } // Search for duplicates. - let mut idents: BTreeSet<&str> = BTreeSet::new(); + let mut idents: FnvHashSet<&str> = FnvHashSet::default(); for arg in all_arguments { let ident = &arg.node.arg; if idents.contains(ident.as_str()) { diff --git a/src/python/sys.rs b/src/python/sys.rs index 36d4ee2fd5..9ada1e35f8 100644 --- a/src/python/sys.rs +++ b/src/python/sys.rs @@ -1,10 +1,9 @@ -use std::collections::BTreeSet; - +use fnv::FnvHashSet; use once_cell::sync::Lazy; // See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library -pub static KNOWN_STANDARD_LIBRARY: Lazy> = Lazy::new(|| { - BTreeSet::from([ +pub static KNOWN_STANDARD_LIBRARY: Lazy> = Lazy::new(|| { + FnvHashSet::from_iter([ "_ast", "_dummy_thread", "_thread", diff --git a/src/settings/options.rs b/src/settings/options.rs index 89ed8844bc..4a6e17711e 100644 --- a/src/settings/options.rs +++ b/src/settings/options.rs @@ -1,7 +1,6 @@ //! Options that the user can provide via pyproject.toml. -use std::collections::BTreeMap; - +use fnv::FnvHashMap; use serde::{Deserialize, Serialize}; use crate::checks_gen::CheckCodePrefix; @@ -29,5 +28,5 @@ pub struct Options { pub isort: Option, pub pep8_naming: Option, // Tables are required to go last. - pub per_file_ignores: Option>>, + pub per_file_ignores: Option>>, } diff --git a/src/settings/pyproject.rs b/src/settings/pyproject.rs index 6aa4e6ae2d..4ba414f598 100644 --- a/src/settings/pyproject.rs +++ b/src/settings/pyproject.rs @@ -96,12 +96,12 @@ pub fn load_options(pyproject: &Option) -> Result { #[cfg(test)] mod tests { - use std::collections::BTreeMap; use std::env::current_dir; use std::path::PathBuf; use std::str::FromStr; use anyhow::Result; + use fnv::FnvHashMap; use crate::checks_gen::CheckCodePrefix; use crate::flake8_quotes::settings::Quote; @@ -346,7 +346,7 @@ other-attribute = 1 extend_select: None, ignore: None, extend_ignore: None, - per_file_ignores: Some(BTreeMap::from([( + per_file_ignores: Some(FnvHashMap::from_iter([( "__init__.py".to_string(), vec![CheckCodePrefix::F401] ),])),