Skip to content

Commit d1514f3

Browse files
authored
perf: cache unfold_definition in the kernel (#12259)
This PR ensures we cache the result of `unfold_definition` definition in the kernel type checker. We used to cache this information in a thread local storage, but it was deleted during the Lean 3 to Lean 4 transition.
1 parent a972c4f commit d1514f3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/kernel/type_checker.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,22 @@ optional<constant_info> type_checker::is_delta(expr const & e) const {
497497
optional<expr> type_checker::unfold_definition_core(expr const & e) {
498498
if (is_constant(e)) {
499499
if (auto d = is_delta(e)) {
500-
if (length(const_levels(e)) == d->get_num_lparams()) {
500+
levels const & us = const_levels(e);
501+
unsigned len = length(us);
502+
if (len == d->get_num_lparams()) {
501503
if (m_diag) {
502504
m_diag->record_unfold(d->get_name());
503505
}
504-
return some_expr(instantiate_value_lparams(*d, const_levels(e)));
506+
if (len > 0) {
507+
auto it = m_st->m_unfold.find(e);
508+
if (it != m_st->m_unfold.end())
509+
return some_expr(it->second);
510+
expr result = instantiate_value_lparams(*d, us);
511+
m_st->m_unfold.insert(mk_pair(e, result));
512+
return some_expr(result);
513+
} else {
514+
return some_expr(instantiate_value_lparams(*d, us));
515+
}
505516
}
506517
}
507518
}

src/kernel/type_checker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class type_checker {
3333
expr_map<expr> m_whnf;
3434
equiv_manager m_eqv_manager;
3535
expr_pair_set m_failure;
36+
expr_map<expr> m_unfold;
3637
friend type_checker;
3738
public:
3839
state(environment const & env);

0 commit comments

Comments
 (0)