Skip to content

Commit

Permalink
types: improve insertion and lookup performance
Browse files Browse the repository at this point in the history
Currently, this uses an expensive tree lookup and requires a lock.
Switching this to a hash function instead of a comparison allows us
to make this much faster, especially for insertion.
  • Loading branch information
vtjnash committed Mar 11, 2020
1 parent 0aa1d96 commit b199eab
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 191 deletions.
9 changes: 3 additions & 6 deletions doc/src/devdocs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,11 @@ MyType{Int64,2}
julia> MyType{Float32, 5}
MyType{Float32,5}
julia> MyType.body.body.name.cache
svec(MyType{Float32,5}, MyType{Int64,2}, #undef, #undef, #undef, #undef, #undef, #undef)
```

(The cache is pre-allocated to have length 8, but only the first two entries are populated.) Consequently,
when you instantiate a parametric type, each concrete type gets saved in a type cache. However,
instances containing free type variables are not cached.
When you instantiate a parametric type, each concrete type gets saved in a type
cache (`MyType.body.body.name.cache`). However, instances containing free type
variables are not cached.

## Tuple types

Expand Down
10 changes: 9 additions & 1 deletion src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,9 @@ STATIC_INLINE jl_value_t *verify_type(jl_value_t *v) JL_NOTSAFEPOINT
}
#endif

jl_datatype_t *jl_lookup_cache_type_(jl_datatype_t *type);
void jl_cache_type_(jl_datatype_t *type);

static jl_datatype_t *jl_recache_type(jl_datatype_t *dt) JL_GC_DISABLED
{
jl_datatype_t *t; // the type after unique'ing
Expand Down Expand Up @@ -2964,7 +2967,12 @@ static jl_datatype_t *jl_recache_type(jl_datatype_t *dt) JL_GC_DISABLED
t = dt;
}
else {
t = (jl_datatype_t*)jl_cache_type_(dt);
t = jl_lookup_cache_type_(dt);
if (t == NULL) {
jl_cache_type_(dt);
t = dt;
}
assert(t->hash == dt->hash);
assert(jl_invalid_types_equal(t, dt));
}
ptrhash_put(&uniquing_table, dt, t);
Expand Down
Loading

0 comments on commit b199eab

Please sign in to comment.