Move bindings to FNV map (#747)

This commit is contained in:
Charlie Marsh 2022-11-14 21:42:57 -05:00 committed by GitHub
parent 8961da7b89
commit 62d4096be3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use fnv::FnvHashMap;
use rustpython_ast::{Expr, Keyword}; use rustpython_ast::{Expr, Keyword};
use rustpython_parser::ast::{Located, Location}; use rustpython_parser::ast::{Located, Location};
@ -54,7 +54,7 @@ pub struct Scope<'a> {
pub id: usize, pub id: usize,
pub kind: ScopeKind<'a>, pub kind: ScopeKind<'a>,
pub import_starred: bool, pub import_starred: bool,
pub values: BTreeMap<String, Binding>, pub values: FnvHashMap<String, Binding>,
} }
impl<'a> Scope<'a> { impl<'a> Scope<'a> {
@ -63,7 +63,7 @@ impl<'a> Scope<'a> {
id: id(), id: id(),
kind, kind,
import_starred: false, import_starred: false,
values: BTreeMap::new(), values: FnvHashMap::default(),
} }
} }
} }

View file

@ -5,6 +5,7 @@ use std::ops::Deref;
use std::path::Path; use std::path::Path;
use fnv::{FnvHashMap, FnvHashSet}; use fnv::{FnvHashMap, FnvHashSet};
use itertools::Itertools;
use log::error; use log::error;
use rustpython_parser::ast::{ use rustpython_parser::ast::{
Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Arg, Arguments, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind,
@ -2486,7 +2487,7 @@ impl<'a> Checker<'a> {
let mut unused: BTreeMap<(ImportKind, usize, Option<usize>), Vec<&str>> = let mut unused: BTreeMap<(ImportKind, usize, Option<usize>), Vec<&str>> =
BTreeMap::new(); BTreeMap::new();
for (name, binding) in scope.values.iter().rev() { for (name, binding) in scope.values.iter() {
let used = binding.used.is_some() let used = binding.used.is_some()
|| all_names || all_names
.map(|names| names.contains(name)) .map(|names| names.contains(name))
@ -2495,25 +2496,25 @@ impl<'a> Checker<'a> {
if !used { if !used {
match &binding.kind { match &binding.kind {
BindingKind::FromImportation(_, full_name, context) => { BindingKind::FromImportation(_, full_name, context) => {
let full_names = unused unused
.entry(( .entry((
ImportKind::ImportFrom, ImportKind::ImportFrom,
context.defined_by, context.defined_by,
context.defined_in, context.defined_in,
)) ))
.or_default(); .or_default()
full_names.push(full_name); .push(full_name);
} }
BindingKind::Importation(_, full_name, context) BindingKind::Importation(_, full_name, context)
| BindingKind::SubmoduleImportation(_, full_name, context) => { | BindingKind::SubmoduleImportation(_, full_name, context) => {
let full_names = unused unused
.entry(( .entry((
ImportKind::Import, ImportKind::Import,
context.defined_by, context.defined_by,
context.defined_in, context.defined_in,
)) ))
.or_default(); .or_default()
full_names.push(full_name); .push(full_name);
} }
_ => {} _ => {}
} }
@ -2554,7 +2555,7 @@ impl<'a> Checker<'a> {
if self.path.ends_with("__init__.py") { if self.path.ends_with("__init__.py") {
checks.push(Check::new( checks.push(Check::new(
CheckKind::UnusedImport( CheckKind::UnusedImport(
full_names.into_iter().map(String::from).collect(), full_names.into_iter().sorted().map(String::from).collect(),
true, true,
), ),
Range::from_located(child), Range::from_located(child),
@ -2562,7 +2563,7 @@ impl<'a> Checker<'a> {
} else { } else {
let mut check = Check::new( let mut check = Check::new(
CheckKind::UnusedImport( CheckKind::UnusedImport(
full_names.into_iter().map(String::from).collect(), full_names.into_iter().sorted().map(String::from).collect(),
false, false,
), ),
Range::from_located(child), Range::from_located(child),