From 3f7380ccec2ebb7809b05d43d6241206650ab02b Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sun, 19 Jun 2011 18:02:37 -0700 Subject: [PATCH] rustc: Change smallintmap to use an ivec and use it for the node type table. 3x typechecking speedup. --- src/comp/middle/ty.rs | 9 ++++----- src/comp/middle/typeck.rs | 15 ++------------- src/lib/ivec.rs | 7 ++++--- src/lib/smallintmap.rs | 17 ++++++++++------- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index fcf4aa251cf..70071931732 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -352,7 +352,7 @@ type type_store = interner::interner[raw_t]; type ty_param_substs_opt_and_ty = tup(option::t[vec[ty::t]], ty::t); type node_type_table = - @mutable vec[mutable option::t[ty::ty_param_substs_opt_and_ty]]; + @smallintmap::smallintmap[ty::ty_param_substs_opt_and_ty]; fn populate_type_store(&ctxt cx) { intern(cx, ty_nil, none[str]); @@ -394,9 +394,8 @@ fn mk_rcache() -> creader_cache { } fn mk_ctxt(session::session s, resolve::def_map dm, constr_table cs) -> ctxt { - let vec[mutable option::t[ty::ty_param_substs_opt_and_ty]] ntt_sub = - [mutable ]; - let node_type_table ntt = @mutable ntt_sub; + let node_type_table ntt = + @smallintmap::mk[ty::ty_param_substs_opt_and_ty](); auto tcache = new_def_hash[ty::ty_param_count_and_ty](); auto items = new_def_hash[any_item](); auto ts = @interner::mk[raw_t](hash_raw_ty, eq_raw_ty); @@ -1597,7 +1596,7 @@ fn ann_to_ty_param_substs_opt_and_ty(&ctxt cx, &ast::ann ann) -> ty_param_substs_opt_and_ty { // Pull out the node type table. - alt ({ cx.node_types.(ann.id) }) { + alt (smallintmap::find(*cx.node_types, ann.id)) { case (none) { cx.sess.bug("ann_to_ty_param_substs_opt_and_ty() called on an " + "untyped node"); diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 42a4cec1499..deb1f96eb18 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -42,6 +42,7 @@ import std::option; import std::option::none; import std::option::some; import std::option::from_maybe; +import std::smallintmap; import middle::tstate::ann::ts_ann; type ty_table = hashmap[ast::def_id, ty::t]; @@ -385,12 +386,7 @@ fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast::ty ast_ty) -> ty::t { mod write { fn inner(&node_type_table ntt, uint node_id, &ty_param_substs_opt_and_ty tpot) { - auto ntt_ = *ntt; - vec::grow_set(ntt_, - node_id, - none[ty_param_substs_opt_and_ty], - some[ty_param_substs_opt_and_ty](tpot)); - *ntt = ntt_; + smallintmap::insert(*ntt, node_id, tpot); } // Writes a type parameter count and type pair into the node type table. @@ -1173,13 +1169,6 @@ fn replace_expr_type(&@fn_ctxt fcx, &@ast::expr expr, write::ty_fixup(fcx, ty::expr_ann(expr).id, tup(new_tps, new_tyt._1)); } -fn replace_node_type_only(&ty::ctxt tcx, uint fixup, ty::t new_t) { - auto fixup_opt = tcx.node_types.(fixup); - auto tps = option::get[ty::ty_param_substs_opt_and_ty](fixup_opt)._0; - tcx.node_types.(fixup) = - some[ty::ty_param_substs_opt_and_ty](tup(tps, new_t)); -} - // AST fragment checking fn check_lit(@crate_ctxt ccx, &@ast::lit lit) -> ty::t { diff --git a/src/lib/ivec.rs b/src/lib/ivec.rs index 2bc23c54b9c..0547c111d65 100644 --- a/src/lib/ivec.rs +++ b/src/lib/ivec.rs @@ -2,6 +2,7 @@ import option::none; import option::some; +import uint::next_power_of_two; type operator2[T,U,V] = fn(&T, &U) -> V; @@ -110,7 +111,7 @@ fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] { /// Expands the given vector in-place by appending `n` copies of `initval`. fn grow[T](&mutable T[] v, uint n, &T initval) { - reserve(v, len(v) + n); + reserve(v, next_power_of_two(len(v) + n)); let uint i = 0u; while (i < n) { v += ~[initval]; @@ -120,7 +121,7 @@ fn grow[T](&mutable T[] v, uint n, &T initval) { // TODO: Remove me once we have slots. fn grow_mut[T](&mutable T[mutable] v, uint n, &T initval) { - reserve(v, len(v) + n); + reserve(v, next_power_of_two(len(v) + n)); let uint i = 0u; while (i < n) { v += ~[mutable initval]; @@ -131,7 +132,7 @@ fn grow_mut[T](&mutable T[mutable] v, uint n, &T initval) { /// Calls `f` `n` times and appends the results of these calls to the given /// vector. fn grow_fn[T](&mutable T[] v, uint n, fn(uint)->T init_fn) { - reserve(v, len(v) + n); + reserve(v, next_power_of_two(len(v) + n)); let uint i = 0u; while (i < n) { v += ~[init_fn(i)]; diff --git a/src/lib/smallintmap.rs b/src/lib/smallintmap.rs index 0c35e16cb93..b8cd89d0d24 100644 --- a/src/lib/smallintmap.rs +++ b/src/lib/smallintmap.rs @@ -5,19 +5,21 @@ import option::none; import option::some; -type smallintmap[T] = rec(mutable vec[mutable option::t[T]] v); +// FIXME: Should not be @; there's a bug somewhere in rustc that requires this +// to be. +type smallintmap[T] = @rec(mutable (option::t[T])[mutable] v); fn mk[T]() -> smallintmap[T] { - let vec[mutable option::t[T]] v = [mutable ]; - ret rec(mutable v=v); + let (option::t[T])[mutable] v = ~[mutable]; + ret @rec(mutable v=v); } fn insert[T](&smallintmap[T] m, uint key, &T val) { - vec::grow_set[option::t[T]](m.v, key, none[T], some[T](val)); + ivec::grow_set[option::t[T]](m.v, key, none[T], some[T](val)); } fn find[T](&smallintmap[T] m, uint key) -> option::t[T] { - if (key < vec::len[option::t[T]](m.v)) { ret m.v.(key); } + if (key < ivec::len[option::t[T]](m.v)) { ret m.v.(key); } ret none[T]; } @@ -36,7 +38,8 @@ fn contains_key[T](&smallintmap[T] m, uint key) -> bool { } fn truncate[T](&smallintmap[T] m, uint len) { - m.v = vec::slice_mut[option::t[T]](m.v, 0u, len); + m.v = ivec::slice_mut[option::t[T]](m.v, 0u, len); } -fn max_key[T](&smallintmap[T] m) -> uint { ret vec::len[option::t[T]](m.v); } \ No newline at end of file +fn max_key[T](&smallintmap[T] m) -> uint { ret ivec::len[option::t[T]](m.v); } +