Skip to content

Commit 630c7a3

Browse files
authored
[ty] Reduce number of inline stored definitions per place (astral-sh#19409)
1 parent e6e029a commit 630c7a3

File tree

9 files changed

+36
-36
lines changed

9 files changed

+36
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ serde_with = { version = "3.6.0", default-features = false, features = [
150150
] }
151151
shellexpand = { version = "3.0.0" }
152152
similar = { version = "2.4.0", features = ["inline"] }
153-
smallvec = { version = "1.13.2" }
153+
smallvec = { version = "1.13.2", features = ["union", "const_generics", "const_new"] }
154154
snapbox = { version = "0.6.0", features = [
155155
"diff",
156156
"term-svg",

crates/ty_ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ pub struct NavigationTargets(smallvec::SmallVec<[NavigationTarget; 1]>);
105105

106106
impl NavigationTargets {
107107
fn single(target: NavigationTarget) -> Self {
108-
Self(smallvec::smallvec![target])
108+
Self(smallvec::smallvec_inline![target])
109109
}
110110

111111
fn empty() -> Self {
112-
Self(smallvec::SmallVec::new())
112+
Self(smallvec::SmallVec::new_const())
113113
}
114114

115115
fn unique(targets: impl IntoIterator<Item = NavigationTarget>) -> Self {

crates/ty_python_semantic/src/semantic_index/definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub struct Definitions<'db> {
107107
impl<'db> Definitions<'db> {
108108
pub(crate) fn single(definition: Definition<'db>) -> Self {
109109
Self {
110-
definitions: smallvec::smallvec![definition],
110+
definitions: smallvec::smallvec_inline![definition],
111111
}
112112
}
113113

crates/ty_python_semantic/src/semantic_index/place.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ruff_index::{IndexVec, newtype_index};
1010
use ruff_python_ast as ast;
1111
use ruff_python_ast::name::Name;
1212
use rustc_hash::FxHasher;
13-
use smallvec::{SmallVec, smallvec};
13+
use smallvec::SmallVec;
1414

1515
use crate::Db;
1616
use crate::ast_node_ref::AstNodeRef;
@@ -162,10 +162,10 @@ impl TryFrom<ast::ExprRef<'_>> for PlaceExpr {
162162
}
163163

164164
impl PlaceExpr {
165-
pub(crate) fn name(name: Name) -> Self {
165+
pub(crate) const fn name(name: Name) -> Self {
166166
Self {
167167
root_name: name,
168-
sub_segments: smallvec![],
168+
sub_segments: SmallVec::new_const(),
169169
}
170170
}
171171

crates/ty_python_semantic/src/semantic_index/use_def/place_state.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,12 @@ impl ScopedDefinitionId {
7171
}
7272
}
7373

74-
/// Can keep inline this many live bindings or declarations per place at a given time; more will
75-
/// go to heap.
76-
const INLINE_DEFINITIONS_PER_PLACE: usize = 4;
77-
7874
/// Live declarations for a single place at some point in control flow, with their
7975
/// corresponding reachability constraints.
8076
#[derive(Clone, Debug, Default, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
8177
pub(super) struct Declarations {
8278
/// A list of live declarations for this place, sorted by their `ScopedDefinitionId`
83-
live_declarations: SmallVec<[LiveDeclaration; INLINE_DEFINITIONS_PER_PLACE]>,
79+
live_declarations: SmallVec<[LiveDeclaration; 2]>,
8480
}
8581

8682
/// One of the live declarations for a single place at some point in control flow.
@@ -199,7 +195,7 @@ pub(super) struct Bindings {
199195
/// "unbound" binding.
200196
unbound_narrowing_constraint: Option<ScopedNarrowingConstraint>,
201197
/// A list of live bindings for this place, sorted by their `ScopedDefinitionId`
202-
live_bindings: SmallVec<[LiveBinding; INLINE_DEFINITIONS_PER_PLACE]>,
198+
live_bindings: SmallVec<[LiveBinding; 2]>,
203199
}
204200

205201
impl Bindings {

crates/ty_python_semantic/src/suppression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl<'a> SuppressionsBuilder<'a> {
508508
lint_registry,
509509
seen_non_trivia_token: false,
510510
line: Vec::new(),
511-
file: SmallVec::new(),
511+
file: SmallVec::new_const(),
512512
unknown: Vec::new(),
513513
invalid: Vec::new(),
514514
}

crates/ty_python_semantic/src/types.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5007,21 +5007,23 @@ impl<'db> Type<'db> {
50075007
| Type::ProtocolInstance(_)
50085008
| Type::PropertyInstance(_)
50095009
| Type::TypeIs(_) => Err(InvalidTypeExpressionError {
5010-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
5011-
*self, scope_id
5012-
)],
5010+
invalid_expressions: smallvec::smallvec_inline![
5011+
InvalidTypeExpression::InvalidType(*self, scope_id)
5012+
],
50135013
fallback_type: Type::unknown(),
50145014
}),
50155015

50165016
Type::KnownInstance(known_instance) => match known_instance {
50175017
KnownInstanceType::TypeAliasType(alias) => Ok(alias.value_type(db)),
50185018
KnownInstanceType::TypeVar(typevar) => Ok(Type::TypeVar(*typevar)),
50195019
KnownInstanceType::SubscriptedProtocol(_) => Err(InvalidTypeExpressionError {
5020-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Protocol],
5020+
invalid_expressions: smallvec::smallvec_inline![
5021+
InvalidTypeExpression::Protocol
5022+
],
50215023
fallback_type: Type::unknown(),
50225024
}),
50235025
KnownInstanceType::SubscriptedGeneric(_) => Err(InvalidTypeExpressionError {
5024-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Generic],
5026+
invalid_expressions: smallvec::smallvec_inline![InvalidTypeExpression::Generic],
50255027
fallback_type: Type::unknown(),
50265028
}),
50275029
},
@@ -5057,7 +5059,7 @@ impl<'db> Type<'db> {
50575059
let Some(class) = nearest_enclosing_class(db, index, scope_id, &module) else {
50585060
return Err(InvalidTypeExpressionError {
50595061
fallback_type: Type::unknown(),
5060-
invalid_expressions: smallvec::smallvec![
5062+
invalid_expressions: smallvec::smallvec_inline![
50615063
InvalidTypeExpression::InvalidType(*self, scope_id)
50625064
],
50635065
});
@@ -5081,18 +5083,20 @@ impl<'db> Type<'db> {
50815083
SpecialFormType::Literal
50825084
| SpecialFormType::Union
50835085
| SpecialFormType::Intersection => Err(InvalidTypeExpressionError {
5084-
invalid_expressions: smallvec::smallvec![
5086+
invalid_expressions: smallvec::smallvec_inline![
50855087
InvalidTypeExpression::RequiresArguments(*self)
50865088
],
50875089
fallback_type: Type::unknown(),
50885090
}),
50895091

50905092
SpecialFormType::Protocol => Err(InvalidTypeExpressionError {
5091-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Protocol],
5093+
invalid_expressions: smallvec::smallvec_inline![
5094+
InvalidTypeExpression::Protocol
5095+
],
50925096
fallback_type: Type::unknown(),
50935097
}),
50945098
SpecialFormType::Generic => Err(InvalidTypeExpressionError {
5095-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::Generic],
5099+
invalid_expressions: smallvec::smallvec_inline![InvalidTypeExpression::Generic],
50965100
fallback_type: Type::unknown(),
50975101
}),
50985102

@@ -5103,15 +5107,15 @@ impl<'db> Type<'db> {
51035107
| SpecialFormType::TypeGuard
51045108
| SpecialFormType::Unpack
51055109
| SpecialFormType::CallableTypeOf => Err(InvalidTypeExpressionError {
5106-
invalid_expressions: smallvec::smallvec![
5110+
invalid_expressions: smallvec::smallvec_inline![
51075111
InvalidTypeExpression::RequiresOneArgument(*self)
51085112
],
51095113
fallback_type: Type::unknown(),
51105114
}),
51115115

51125116
SpecialFormType::Annotated | SpecialFormType::Concatenate => {
51135117
Err(InvalidTypeExpressionError {
5114-
invalid_expressions: smallvec::smallvec![
5118+
invalid_expressions: smallvec::smallvec_inline![
51155119
InvalidTypeExpression::RequiresTwoArguments(*self)
51165120
],
51175121
fallback_type: Type::unknown(),
@@ -5120,7 +5124,7 @@ impl<'db> Type<'db> {
51205124

51215125
SpecialFormType::ClassVar | SpecialFormType::Final => {
51225126
Err(InvalidTypeExpressionError {
5123-
invalid_expressions: smallvec::smallvec![
5127+
invalid_expressions: smallvec::smallvec_inline![
51245128
InvalidTypeExpression::TypeQualifier(*special_form)
51255129
],
51265130
fallback_type: Type::unknown(),
@@ -5130,7 +5134,7 @@ impl<'db> Type<'db> {
51305134
SpecialFormType::ReadOnly
51315135
| SpecialFormType::NotRequired
51325136
| SpecialFormType::Required => Err(InvalidTypeExpressionError {
5133-
invalid_expressions: smallvec::smallvec![
5137+
invalid_expressions: smallvec::smallvec_inline![
51345138
InvalidTypeExpression::TypeQualifierRequiresOneArgument(*special_form)
51355139
],
51365140
fallback_type: Type::unknown(),
@@ -5184,9 +5188,9 @@ impl<'db> Type<'db> {
51845188
"Support for `types.UnionType` instances in type expressions"
51855189
)),
51865190
_ => Err(InvalidTypeExpressionError {
5187-
invalid_expressions: smallvec::smallvec![InvalidTypeExpression::InvalidType(
5188-
*self, scope_id
5189-
)],
5191+
invalid_expressions: smallvec::smallvec_inline![
5192+
InvalidTypeExpression::InvalidType(*self, scope_id)
5193+
],
51905194
fallback_type: Type::unknown(),
51915195
}),
51925196
},

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88

99
use itertools::Itertools;
1010
use ruff_db::parsed::parsed_module;
11-
use smallvec::{SmallVec, smallvec};
11+
use smallvec::{SmallVec, smallvec, smallvec_inline};
1212

1313
use super::{Argument, CallArguments, CallError, CallErrorKind, InferContext, Signature, Type};
1414
use crate::db::Db;
@@ -1019,7 +1019,7 @@ impl<'db> From<CallableBinding<'db>> for Bindings<'db> {
10191019
fn from(from: CallableBinding<'db>) -> Bindings<'db> {
10201020
Bindings {
10211021
callable_type: from.callable_type,
1022-
elements: smallvec![from],
1022+
elements: smallvec_inline![from],
10231023
argument_forms: Box::from([]),
10241024
conflicting_forms: Box::from([]),
10251025
}
@@ -1037,11 +1037,11 @@ impl<'db> From<Binding<'db>> for Bindings<'db> {
10371037
bound_type: None,
10381038
overload_call_return_type: None,
10391039
matching_overload_index: None,
1040-
overloads: smallvec![from],
1040+
overloads: smallvec_inline![from],
10411041
};
10421042
Bindings {
10431043
callable_type,
1044-
elements: smallvec![callable_binding],
1044+
elements: smallvec_inline![callable_binding],
10451045
argument_forms: Box::from([]),
10461046
conflicting_forms: Box::from([]),
10471047
}

crates/ty_python_semantic/src/types/signatures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use std::{collections::HashMap, slice::Iter};
1414

1515
use itertools::EitherOrBoth;
16-
use smallvec::{SmallVec, smallvec};
16+
use smallvec::{SmallVec, smallvec_inline};
1717

1818
use super::{DynamicType, Type, TypeTransformer, TypeVarVariance, definition_expression_type};
1919
use crate::semantic_index::definition::Definition;
@@ -34,7 +34,7 @@ pub struct CallableSignature<'db> {
3434
impl<'db> CallableSignature<'db> {
3535
pub(crate) fn single(signature: Signature<'db>) -> Self {
3636
Self {
37-
overloads: smallvec![signature],
37+
overloads: smallvec_inline![signature],
3838
}
3939
}
4040

0 commit comments

Comments
 (0)