Sitelet https://github.com/starkware-libs/cairo/commit/e6e2d25fe3054c7d9d98902e3d76cc079f50a010
Skip to content

Commit e6e2d25

Browse files
committed
performance(semantic): Made substitute be faster on empty subs.
1 parent 6296ede commit e6e2d25

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎corelib/src/test/language_features/const_test.cairo‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,29 @@ fn test_const_generic_enum_return() {
203203
assert_eq!(RESULT, Either::Left(7));
204204
}
205205

206+
trait AssocConst<const N: usize> {
207+
const VALUE: usize;
208+
}
209+
210+
impl AssocConstImpl of AssocConst<7> {
211+
const VALUE: usize = 70;
212+
}
213+
214+
const fn assoc_const_via_generic<const N: usize, impl A: AssocConst<N>>() -> usize {
215+
A::VALUE
216+
}
217+
218+
// Associated-const (impl-constant projection) evaluation in const context: both direct concrete
219+
// access (evaluated under an empty substitution) and through a generic const fn must yield the
220+
// concrete value, not an unresolved projection.
221+
#[test]
222+
fn test_const_assoc_const() {
223+
const DIRECT: usize = AssocConstImpl::VALUE;
224+
assert_eq!(DIRECT, 70);
225+
const VIA_GENERIC: usize = assoc_const_via_generic::<7, AssocConstImpl>();
226+
assert_eq!(VIA_GENERIC, 70);
227+
}
228+
206229
#[derive(Copy, Drop, PartialEq, Debug)]
207230
struct Point {
208231
x: felt252,

‎crates/cairo-lang-semantic/src/items/constant.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,11 +1251,7 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> {
12511251
where
12521252
SubstitutionRewriter<'a, 'w>: SemanticRewriter<Obj, DiagnosticAdded>,
12531253
{
1254-
if self.generic_substitution.is_empty() {
1255-
Ok(obj)
1256-
} else {
1257-
self.generic_substitution.substitute(self.db, obj)
1258-
}
1254+
self.generic_substitution.substitute(self.db, obj)
12591255
}
12601256
/// Compares two const values for value equality, treating `felt252` as a field.
12611257
///

‎crates/cairo-lang-semantic/src/substitution.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ impl<'db> GenericSubstitution<'db> {
9393
'db: 'a,
9494
SubstitutionRewriter<'a, 'r>: SemanticRewriter<Obj, DiagnosticAdded>,
9595
{
96-
SubstitutionRewriter { db, substitution: self }.rewrite(obj)
96+
if self.is_empty() {
97+
Ok(obj)
98+
} else {
99+
SubstitutionRewriter { db, substitution: self }.rewrite(obj)
100+
}
97101
}
98102
}
99103
impl<'db> Deref for GenericSubstitution<'db> {

0 commit comments

Comments
 (0)