[ty] Reduce number of inline stored definitions per place (#19409)

This commit is contained in:
Micha Reiser 2025-07-18 18:28:46 +02:00 committed by GitHub
parent e6e029a8b7
commit 630c7a3152
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 36 additions and 36 deletions

View file

@ -107,7 +107,7 @@ pub struct Definitions<'db> {
impl<'db> Definitions<'db> {
pub(crate) fn single(definition: Definition<'db>) -> Self {
Self {
definitions: smallvec::smallvec![definition],
definitions: smallvec::smallvec_inline![definition],
}
}

View file

@ -10,7 +10,7 @@ use ruff_index::{IndexVec, newtype_index};
use ruff_python_ast as ast;
use ruff_python_ast::name::Name;
use rustc_hash::FxHasher;
use smallvec::{SmallVec, smallvec};
use smallvec::SmallVec;
use crate::Db;
use crate::ast_node_ref::AstNodeRef;
@ -162,10 +162,10 @@ impl TryFrom<ast::ExprRef<'_>> for PlaceExpr {
}
impl PlaceExpr {
pub(crate) fn name(name: Name) -> Self {
pub(crate) const fn name(name: Name) -> Self {
Self {
root_name: name,
sub_segments: smallvec![],
sub_segments: SmallVec::new_const(),
}
}

View file

@ -71,16 +71,12 @@ impl ScopedDefinitionId {
}
}
/// Can keep inline this many live bindings or declarations per place at a given time; more will
/// go to heap.
const INLINE_DEFINITIONS_PER_PLACE: usize = 4;
/// Live declarations for a single place at some point in control flow, with their
/// corresponding reachability constraints.
#[derive(Clone, Debug, Default, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
pub(super) struct Declarations {
/// A list of live declarations for this place, sorted by their `ScopedDefinitionId`
live_declarations: SmallVec<[LiveDeclaration; INLINE_DEFINITIONS_PER_PLACE]>,
live_declarations: SmallVec<[LiveDeclaration; 2]>,
}
/// One of the live declarations for a single place at some point in control flow.
@ -199,7 +195,7 @@ pub(super) struct Bindings {
/// "unbound" binding.
unbound_narrowing_constraint: Option<ScopedNarrowingConstraint>,
/// A list of live bindings for this place, sorted by their `ScopedDefinitionId`
live_bindings: SmallVec<[LiveBinding; INLINE_DEFINITIONS_PER_PLACE]>,
live_bindings: SmallVec<[LiveBinding; 2]>,
}
impl Bindings {

View file

@ -508,7 +508,7 @@ impl<'a> SuppressionsBuilder<'a> {
lint_registry,
seen_non_trivia_token: false,
line: Vec::new(),
file: SmallVec::new(),
file: SmallVec::new_const(),
unknown: Vec::new(),
invalid: Vec::new(),
}

View file

@ -5007,9 +5007,9 @@ impl<'db> Type<'db> {
| Type::ProtocolInstance(_)
| Type::PropertyInstance(_)
| Type::TypeIs(_) => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
*self, scope_id
)],
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::InvalidType(*self, scope_id)
],
fallback_type: Type::unknown(),
}),
@ -5017,11 +5017,13 @@ impl<'db> Type<'db> {
KnownInstanceType::TypeAliasType(alias) => Ok(alias.value_type(db)),
KnownInstanceType::TypeVar(typevar) => Ok(Type::TypeVar(*typevar)),
KnownInstanceType::SubscriptedProtocol(_) => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Protocol],
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::Protocol
],
fallback_type: Type::unknown(),
}),
KnownInstanceType::SubscriptedGeneric(_) => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Generic],
invalid_expressions: smallvec::smallvec_inline![InvalidTypeExpression::Generic],
fallback_type: Type::unknown(),
}),
},
@ -5057,7 +5059,7 @@ impl<'db> Type<'db> {
let Some(class) = nearest_enclosing_class(db, index, scope_id, &module) else {
return Err(InvalidTypeExpressionError {
fallback_type: Type::unknown(),
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::InvalidType(*self, scope_id)
],
});
@ -5081,18 +5083,20 @@ impl<'db> Type<'db> {
SpecialFormType::Literal
| SpecialFormType::Union
| SpecialFormType::Intersection => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::RequiresArguments(*self)
],
fallback_type: Type::unknown(),
}),
SpecialFormType::Protocol => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Protocol],
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::Protocol
],
fallback_type: Type::unknown(),
}),
SpecialFormType::Generic => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Generic],
invalid_expressions: smallvec::smallvec_inline![InvalidTypeExpression::Generic],
fallback_type: Type::unknown(),
}),
@ -5103,7 +5107,7 @@ impl<'db> Type<'db> {
| SpecialFormType::TypeGuard
| SpecialFormType::Unpack
| SpecialFormType::CallableTypeOf => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::RequiresOneArgument(*self)
],
fallback_type: Type::unknown(),
@ -5111,7 +5115,7 @@ impl<'db> Type<'db> {
SpecialFormType::Annotated | SpecialFormType::Concatenate => {
Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::RequiresTwoArguments(*self)
],
fallback_type: Type::unknown(),
@ -5120,7 +5124,7 @@ impl<'db> Type<'db> {
SpecialFormType::ClassVar | SpecialFormType::Final => {
Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::TypeQualifier(*special_form)
],
fallback_type: Type::unknown(),
@ -5130,7 +5134,7 @@ impl<'db> Type<'db> {
SpecialFormType::ReadOnly
| SpecialFormType::NotRequired
| SpecialFormType::Required => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::TypeQualifierRequiresOneArgument(*special_form)
],
fallback_type: Type::unknown(),
@ -5184,9 +5188,9 @@ impl<'db> Type<'db> {
"Support for `types.UnionType` instances in type expressions"
)),
_ => Err(InvalidTypeExpressionError {
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
*self, scope_id
)],
invalid_expressions: smallvec::smallvec_inline![
InvalidTypeExpression::InvalidType(*self, scope_id)
],
fallback_type: Type::unknown(),
}),
},

View file

@ -8,7 +8,7 @@ use std::fmt;
use itertools::Itertools;
use ruff_db::parsed::parsed_module;
use smallvec::{SmallVec, smallvec};
use smallvec::{SmallVec, smallvec, smallvec_inline};
use super::{Argument, CallArguments, CallError, CallErrorKind, InferContext, Signature, Type};
use crate::db::Db;
@ -1019,7 +1019,7 @@ impl<'db> From<CallableBinding<'db>> for Bindings<'db> {
fn from(from: CallableBinding<'db>) -> Bindings<'db> {
Bindings {
callable_type: from.callable_type,
elements: smallvec![from],
elements: smallvec_inline![from],
argument_forms: Box::from([]),
conflicting_forms: Box::from([]),
}
@ -1037,11 +1037,11 @@ impl<'db> From<Binding<'db>> for Bindings<'db> {
bound_type: None,
overload_call_return_type: None,
matching_overload_index: None,
overloads: smallvec![from],
overloads: smallvec_inline![from],
};
Bindings {
callable_type,
elements: smallvec![callable_binding],
elements: smallvec_inline![callable_binding],
argument_forms: Box::from([]),
conflicting_forms: Box::from([]),
}

View file

@ -13,7 +13,7 @@
use std::{collections::HashMap, slice::Iter};
use itertools::EitherOrBoth;
use smallvec::{SmallVec, smallvec};
use smallvec::{SmallVec, smallvec_inline};
use super::{DynamicType, Type, TypeTransformer, TypeVarVariance, definition_expression_type};
use crate::semantic_index::definition::Definition;
@ -34,7 +34,7 @@ pub struct CallableSignature<'db> {
impl<'db> CallableSignature<'db> {
pub(crate) fn single(signature: Signature<'db>) -> Self {
Self {
overloads: smallvec![signature],
overloads: smallvec_inline![signature],
}
}