Skip to content

Commit 057f573

Browse files
committed
Merge pull request #11606 from yuyichao/delay-cache-type
Do not cache the type before we finish constructing it.
2 parents 54cee1f + e67f5a3 commit 057f573

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/jltypes.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,26 @@ static jl_value_t *inst_type_w_(jl_value_t *t, jl_value_t **env, size_t n,
19511951
static jl_svec_t *inst_all(jl_svec_t *p, jl_value_t **env, size_t n,
19521952
jl_typestack_t *stack, int check);
19531953

1954+
static jl_value_t*
1955+
lookup_type_stack(jl_typestack_t *stack, jl_datatype_t *tt, size_t ntp,
1956+
jl_value_t **iparams)
1957+
{
1958+
// if an identical instantiation is already in process somewhere
1959+
// up the stack, return it. this computes a fixed point for
1960+
// recursive types.
1961+
jl_typename_t *tn = tt->name;
1962+
while (stack != NULL) {
1963+
if (stack->tt->name == tn &&
1964+
ntp == jl_svec_len(stack->tt->parameters) &&
1965+
typekey_eq(stack->tt, iparams, ntp)) {
1966+
jl_value_t *lkup = (jl_value_t*)stack->tt;
1967+
return lkup == tn->primary ? NULL : lkup;
1968+
}
1969+
stack = stack->prev;
1970+
}
1971+
return NULL;
1972+
}
1973+
19541974
static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **iparams, size_t ntp,
19551975
int cacheable, int isabstract, jl_typestack_t *stack,
19561976
jl_value_t **env, size_t n)
@@ -1965,6 +1985,10 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
19651985
if (lkup != NULL)
19661986
return lkup;
19671987
}
1988+
jl_value_t *stack_lkup = lookup_type_stack(stack, dt, ntp, iparams);
1989+
if (stack_lkup) {
1990+
return stack_lkup;
1991+
}
19681992

19691993
// always use original type constructor
19701994
if (!istuple) {
@@ -2008,8 +2032,6 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
20082032
ndt->size = 0;
20092033
ndt->alignment = 1;
20102034

2011-
if (cacheable) jl_cache_type_(ndt);
2012-
20132035
if (istuple)
20142036
ndt->super = jl_any_type;
20152037
else
@@ -2047,6 +2069,10 @@ static jl_value_t *inst_datatype(jl_datatype_t *dt, jl_svec_t *p, jl_value_t **i
20472069
ndt->ninitialized = ntp;
20482070
else
20492071
ndt->ninitialized = dt->ninitialized;
2072+
2073+
if (cacheable)
2074+
jl_cache_type_(ndt);
2075+
20502076
JL_GC_POP();
20512077
return (jl_value_t*)ndt;
20522078
}
@@ -2213,25 +2239,8 @@ static jl_value_t *inst_type_w_(jl_value_t *t, jl_value_t **env, size_t n,
22132239
// if t's parameters are not bound in the environment, return it uncopied (#9378)
22142240
if (!bound && t == tc) { JL_GC_POP(); return (jl_value_t*)t; }
22152241

2216-
// if an identical instantiation is already in process somewhere
2217-
// up the stack, return it. this computes a fixed point for
2218-
// recursive types.
2219-
jl_typestack_t *tmp = stack;
2220-
jl_value_t *lkup = NULL;
2221-
while (tmp != NULL) {
2222-
if (tmp->tt->name==tn && ntp==jl_svec_len(tmp->tt->parameters) &&
2223-
typekey_eq(tmp->tt, iparams, ntp)) {
2224-
lkup = (jl_value_t*)tmp->tt;
2225-
break;
2226-
}
2227-
tmp = tmp->prev;
2228-
}
2229-
if (lkup != NULL && lkup != (jl_value_t*)tc) {
2230-
JL_GC_POP();
2231-
return lkup;
2232-
}
2233-
jl_value_t *result = inst_datatype((jl_datatype_t*)tt, NULL, iparams, ntp, cacheable, isabstract,
2234-
stack, env, n);
2242+
jl_value_t *result = inst_datatype(tt, NULL, iparams, ntp, cacheable,
2243+
isabstract, stack, env, n);
22352244
JL_GC_POP();
22362245
return result;
22372246
}

0 commit comments

Comments
 (0)