Skip to content

Commit 082d831

Browse files
committed
Rewrite bitv to use classes and optimize its representation
Rewrote bitv as a class that uses a 32-bit int as its representation for bit vectors of 32 bits or less, and a vector (the old representation) otherwise. I didn't benchmark very much, but a bit of informal benchmarking suggested this is a win. Closes #2341
1 parent 6ac86e9 commit 082d831

File tree

10 files changed

+721
-578
lines changed

10 files changed

+721
-578
lines changed

src/libstd/bitv.rs

+484-330
Large diffs are not rendered by default.

src/libsyntax/ext/pipes/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ updating the states using rule (2) until there are no changes.
2929

3030
import dvec::extensions;
3131

32-
import std::bitv::{bitv, methods};
32+
import std::bitv::{bitv};
3333

3434
import proto::methods;
3535
import ast_builder::empty_span;
@@ -38,7 +38,7 @@ fn analyze(proto: protocol, _cx: ext_ctxt) {
3838
#debug("initializing colive analysis");
3939
let num_states = proto.num_states();
4040
let colive = do (copy proto.states).map_to_vec |state| {
41-
let bv = bitv(num_states, false);
41+
let bv = ~bitv(num_states, false);
4242
for state.reachable |s| {
4343
bv.set(s.id, true);
4444
}

src/rustc/middle/tstate/ann.rs

+32-36
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ fn empty_poststate(num_vars: uint) -> poststate {
5353

5454
fn false_postcond(num_vars: uint) -> postcond {
5555
let rslt = create_tritv(num_vars);
56-
tritv_set_all(rslt);
57-
ret rslt;
56+
rslt.set_all();
57+
rslt
5858
}
5959

6060
fn empty_pre_post(num_vars: uint) -> pre_and_post {
@@ -76,15 +76,11 @@ fn get_pre(&&p: pre_and_post) -> precond { ret p.precondition; }
7676

7777
fn get_post(&&p: pre_and_post) -> postcond { ret p.postcondition; }
7878

79-
fn difference(p1: precond, p2: precond) -> bool {
80-
ret tritv_difference(p1, p2);
81-
}
79+
fn difference(p1: precond, p2: precond) -> bool { p1.difference(p2) }
8280

83-
fn union(p1: precond, p2: precond) -> bool { ret tritv_union(p1, p2); }
81+
fn union(p1: precond, p2: precond) -> bool { p1.union(p2) }
8482

85-
fn intersect(p1: precond, p2: precond) -> bool {
86-
ret tritv_intersect(p1, p2);
87-
}
83+
fn intersect(p1: precond, p2: precond) -> bool { p1.intersect(p2) }
8884

8985
fn pps_len(p: pre_and_post) -> uint {
9086
// gratuitous check
@@ -95,13 +91,13 @@ fn pps_len(p: pre_and_post) -> uint {
9591

9692
fn require(i: uint, p: pre_and_post) {
9793
// sets the ith bit in p's pre
98-
tritv_set(i, p.precondition, ttrue);
94+
p.precondition.set(i, ttrue);
9995
}
10096

10197
fn require_and_preserve(i: uint, p: pre_and_post) {
10298
// sets the ith bit in p's pre and post
103-
tritv_set(i, p.precondition, ttrue);
104-
tritv_set(i, p.postcondition, ttrue);
99+
p.precondition.set(i, ttrue);
100+
p.postcondition.set(i, ttrue);
105101
}
106102

107103
fn set_in_postcond(i: uint, p: pre_and_post) -> bool {
@@ -110,8 +106,8 @@ fn set_in_postcond(i: uint, p: pre_and_post) -> bool {
110106
}
111107

112108
fn set_in_postcond_(i: uint, p: postcond) -> bool {
113-
let was_set = tritv_get(p, i);
114-
tritv_set(i, p, ttrue);
109+
let was_set = p.get(i);
110+
p.set(i, ttrue);
115111
ret was_set != ttrue;
116112
}
117113

@@ -121,8 +117,8 @@ fn set_in_poststate(i: uint, s: pre_and_post_state) -> bool {
121117
}
122118

123119
fn set_in_poststate_(i: uint, p: poststate) -> bool {
124-
let was_set = tritv_get(p, i);
125-
tritv_set(i, p, ttrue);
120+
let was_set = p.get(i);
121+
p.set(i, ttrue);
126122
ret was_set != ttrue;
127123

128124
}
@@ -133,8 +129,8 @@ fn clear_in_poststate(i: uint, s: pre_and_post_state) -> bool {
133129
}
134130

135131
fn clear_in_poststate_(i: uint, s: poststate) -> bool {
136-
let was_set = tritv_get(s, i);
137-
tritv_set(i, s, tfalse);
132+
let was_set = s.get(i);
133+
s.set(i, tfalse);
138134
ret was_set != tfalse;
139135
}
140136

@@ -144,61 +140,61 @@ fn clear_in_prestate(i: uint, s: pre_and_post_state) -> bool {
144140
}
145141

146142
fn clear_in_prestate_(i: uint, s: prestate) -> bool {
147-
let was_set = tritv_get(s, i);
148-
tritv_set(i, s, tfalse);
143+
let was_set = s.get(i);
144+
s.set(i, tfalse);
149145
ret was_set != tfalse;
150146
}
151147

152148
fn clear_in_postcond(i: uint, s: pre_and_post) -> bool {
153149
// sets the ith bit in p's post
154-
let was_set = tritv_get(s.postcondition, i);
155-
tritv_set(i, s.postcondition, tfalse);
150+
let was_set = s.postcondition.get(i);
151+
s.postcondition.set(i, tfalse);
156152
ret was_set != tfalse;
157153
}
158154

159155
// Sets all the bits in a's precondition to equal the
160156
// corresponding bit in p's precondition.
161157
fn set_precondition(a: ts_ann, p: precond) {
162-
tritv_copy(a.conditions.precondition, p);
158+
a.conditions.precondition.become(p);
163159
}
164160

165161

166162
// Sets all the bits in a's postcondition to equal the
167163
// corresponding bit in p's postcondition.
168164
fn set_postcondition(a: ts_ann, p: postcond) {
169-
tritv_copy(a.conditions.postcondition, p);
165+
a.conditions.postcondition.become(p);
170166
}
171167

172168

173169
// Sets all the bits in a's prestate to equal the
174170
// corresponding bit in p's prestate.
175171
fn set_prestate(a: ts_ann, p: prestate) -> bool {
176-
ret tritv_copy(a.states.prestate, p);
172+
a.states.prestate.become(p)
177173
}
178174

179175

180176
// Sets all the bits in a's postcondition to equal the
181177
// corresponding bit in p's postcondition.
182178
fn set_poststate(a: ts_ann, p: poststate) -> bool {
183-
ret tritv_copy(a.states.poststate, p);
179+
a.states.poststate.become(p)
184180
}
185181

186182

187183
// Set all the bits in p that are set in new
188184
fn extend_prestate(p: prestate, newv: poststate) -> bool {
189-
ret tritv_union(p, newv);
185+
p.union(newv)
190186
}
191187

192188

193189
// Set all the bits in p that are set in new
194190
fn extend_poststate(p: poststate, newv: poststate) -> bool {
195-
ret tritv_union(p, newv);
191+
p.union(newv)
196192
}
197193

198194
// Sets the given bit in p to "don't care"
199195
fn relax_prestate(i: uint, p: prestate) -> bool {
200-
let was_set = tritv_get(p, i);
201-
tritv_set(i, p, dont_care);
196+
let was_set = p.get(i);
197+
p.set(i, dont_care);
202198
ret was_set != dont_care;
203199
}
204200

@@ -211,10 +207,10 @@ fn relax_poststate(i: uint, p: poststate) -> bool {
211207
fn relax_precond(i: uint, p: precond) { relax_prestate(i, p); }
212208

213209
// Sets all the bits in p to "don't care"
214-
fn clear(p: precond) { tritv_clear(p); }
210+
fn clear(p: precond) { p.clear(); }
215211

216212
// Sets all the bits in p to true
217-
fn set(p: precond) { tritv_set_all(p); }
213+
fn set(p: precond) { p.set_all(); }
218214

219215
fn ann_precond(a: ts_ann) -> precond { ret a.conditions.precondition; }
220216

@@ -227,16 +223,16 @@ fn pp_clone(p: pre_and_post) -> pre_and_post {
227223
postcondition: clone(p.postcondition)};
228224
}
229225

230-
fn clone(p: prestate) -> prestate { ret tritv_clone(p); }
226+
fn clone(p: prestate) -> prestate { p.clone() }
231227

232228

233229
// returns true if a implies b
234230
// that is, returns true except if for some bits c and d,
235231
// c = 1 and d = either 0 or "don't know"
236232
fn implies(a: t, b: t) -> bool {
237-
let tmp = tritv_clone(b);
238-
tritv_difference(tmp, a);
239-
ret tritv_doesntcare(tmp);
233+
let tmp = b.clone();
234+
tmp.difference(a);
235+
tmp.doesntcare()
240236
}
241237

242238
fn trit_str(t: trit) -> ~str {

src/rustc/middle/tstate/auxiliary.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import tstate::ann::{pre_and_post, pre_and_post_state, empty_ann, prestate,
1515
clear_in_poststate_};
1616
import driver::session::session;
1717
import dvec::{dvec, extensions};
18-
import tritv::{dont_care, tfalse, tritv_get, ttrue};
18+
import tritv::{trit, tfalse, ttrue, dont_care, t};
1919

2020
import syntax::print::pprust::{constr_args_to_str, lit_to_str};
2121

@@ -59,7 +59,7 @@ fn tritv_to_str(fcx: fn_ctxt, v: tritv::t) -> ~str {
5959
let mut s = ~"";
6060
let mut comma = false;
6161
for constraints(fcx).each |p| {
62-
alt tritv_get(v, p.bit_num) {
62+
alt v.get(p.bit_num) {
6363
dont_care { }
6464
tt {
6565
s +=
@@ -80,8 +80,8 @@ fn first_difference_string(fcx: fn_ctxt, expected: tritv::t, actual: tritv::t)
8080
-> ~str {
8181
let mut s = ~"";
8282
for constraints(fcx).each |c| {
83-
if tritv_get(expected, c.bit_num) == ttrue &&
84-
tritv_get(actual, c.bit_num) != ttrue {
83+
if expected.get(c.bit_num) == ttrue &&
84+
actual.get(c.bit_num) != ttrue {
8585
s = constraint_to_str(fcx.ccx.tcx, c.c);
8686
break;
8787
}
@@ -108,35 +108,35 @@ fn log_cond(v: ~[uint]) { log(debug, tos(v)); }
108108
fn log_cond_err(v: ~[uint]) { log(error, tos(v)); }
109109

110110
fn log_pp(pp: pre_and_post) {
111-
let p1 = tritv::to_vec(pp.precondition);
112-
let p2 = tritv::to_vec(pp.postcondition);
111+
let p1 = pp.precondition.to_vec();
112+
let p2 = pp.postcondition.to_vec();
113113
#debug("pre:");
114114
log_cond(p1);
115115
#debug("post:");
116116
log_cond(p2);
117117
}
118118

119119
fn log_pp_err(pp: pre_and_post) {
120-
let p1 = tritv::to_vec(pp.precondition);
121-
let p2 = tritv::to_vec(pp.postcondition);
120+
let p1 = pp.precondition.to_vec();
121+
let p2 = pp.postcondition.to_vec();
122122
#error("pre:");
123123
log_cond_err(p1);
124124
#error("post:");
125125
log_cond_err(p2);
126126
}
127127

128128
fn log_states(pp: pre_and_post_state) {
129-
let p1 = tritv::to_vec(pp.prestate);
130-
let p2 = tritv::to_vec(pp.poststate);
129+
let p1 = pp.prestate.to_vec();
130+
let p2 = pp.poststate.to_vec();
131131
#debug("prestate:");
132132
log_cond(p1);
133133
#debug("poststate:");
134134
log_cond(p2);
135135
}
136136

137137
fn log_states_err(pp: pre_and_post_state) {
138-
let p1 = tritv::to_vec(pp.prestate);
139-
let p2 = tritv::to_vec(pp.poststate);
138+
let p1 = pp.prestate.to_vec();
139+
let p2 = pp.poststate.to_vec();
140140
#error("prestate:");
141141
log_cond_err(p1);
142142
#error("poststate:");

src/rustc/middle/tstate/pre_post_conditions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn handle_update(fcx: fn_ctxt, parent: @expr, lhs: @expr, rhs: @expr,
184184
alt lhs.node {
185185
expr_path(p) {
186186
let post = expr_postcond(fcx.ccx, parent);
187-
let tmp = tritv_clone(post);
187+
let tmp = post.clone();
188188

189189
alt ty {
190190
oper_move {
@@ -497,7 +497,7 @@ fn find_pre_post_stmt(fcx: fn_ctxt, s: stmt) {
497497
/* Clear out anything that the previous initializer
498498
guaranteed */
499499
let e_pp = expr_pp(fcx.ccx, an_init.expr);
500-
tritv_copy(prev_pp.precondition,
500+
prev_pp.precondition.become(
501501
seq_preconds(fcx, ~[prev_pp, e_pp]));
502502

503503
/* Include the LHSs too, since those aren't in the

0 commit comments

Comments
 (0)