Skip to content

Commit

Permalink
Nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Jan 2, 2020
1 parent f37e79f commit cef4cc4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
41 changes: 21 additions & 20 deletions kpermuter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ template <> struct sized_kpermuter_info<2> {
full_value = (uint64_t) 0xEDCBA98765432100ULL };
};

template <int width> class kpermuter {
template <int W> class kpermuter {
public:
typedef sized_kpermuter_info<(width > 3) + (width > 7) + (width > 15)> info;
typedef sized_kpermuter_info<(W > 3) + (W > 7) + (W > 15)> info;
typedef typename info::storage_type storage_type;
typedef typename info::value_type value_type;
enum { max_width = (int) (sizeof(storage_type) * 2 - 1) };
Expand All @@ -76,7 +76,7 @@ template <int width> class kpermuter {
Elements will be allocated in order 0, 1, ..., @a width - 1. */
static inline value_type make_empty() {
value_type p = (value_type) info::initial_value >> ((max_width - width) << 2);
value_type p = (value_type) info::initial_value >> ((max_width - W) << 2);
return p & ~(value_type) 15;
}
/** @brief Return a permuter with size @a n.
Expand All @@ -85,7 +85,7 @@ template <int width> class kpermuter {
(*this)[i] == i. Elements n through @a width - 1 are free, and will be
allocated in that order. */
static inline value_type make_sorted(int n) {
value_type mask = (n == width ? (value_type) 0 : (value_type) 16 << (n << 2)) - 1;
value_type mask = (n == W ? (value_type) 0 : (value_type) 16 << (n << 2)) - 1;
return (make_empty() << (n << 2))
| ((value_type) info::full_value & mask)
| n;
Expand All @@ -95,13 +95,16 @@ template <int width> class kpermuter {
int size() const {
return x_ & 15;
}
static int width() {
return W;
}
/** @brief Return the permuter's element @a i.
@pre 0 <= i < width */
int operator[](int i) const {
return (x_ >> ((i << 2) + 4)) & 15;
}
int back() const {
return (*this)[width - 1];
return (*this)[W - 1];
}
value_type value() const {
return x_;
Expand Down Expand Up @@ -147,7 +150,6 @@ template <int width> class kpermuter {
@pre size() <= @a si
@return The newly allocated element. */
void insert_selected(int di, int si) {
(void) width;
int value = (*this)[si];
value_type mask = ((value_type) 256 << (si << 2)) - 1;
// increment size, leave lower slots unchanged
Expand Down Expand Up @@ -177,7 +179,6 @@ template <int width> class kpermuter {
<li>q[q.size()] == p[i]</li>
</ul> */
void remove(int i) {
(void) width;
if (int(x_ & 15) == i + 1) {
--x_;
} else {
Expand Down Expand Up @@ -212,13 +213,13 @@ template <int width> class kpermuter {
void remove_to_back(int i) {
value_type mask = ~(((value_type) 16 << (i << 2)) - 1);
// clear unused slots
value_type x = x_ & (((value_type) 16 << (width << 2)) - 1);
value_type x = x_ & (((value_type) 16 << (W << 2)) - 1);
// decrement size, leave lower slots unchanged
x_ = ((x - 1) & ~mask)
// shift higher entries down
| ((x >> 4) & mask)
// shift removed element up
| ((x & mask) << ((width - i - 1) << 2));
| ((x & mask) << ((W - i - 1) << 2));
}
/** @brief Rotate the permuter's elements between @a i and size().
@pre 0 <= @a i <= @a j <= size()
Expand All @@ -236,12 +237,12 @@ template <int width> class kpermuter {
<li>Given k with i <= k < q.size(), q[k] == p[i + (k - i + j - i) mod (size() - i)]</li>
</ul> */
void rotate(int i, int j) {
value_type mask = (i == width ? (value_type) 0 : (value_type) 16 << (i << 2)) - 1;
value_type mask = (i == W ? (value_type) 0 : (value_type) 16 << (i << 2)) - 1;
// clear unused slots
value_type x = x_ & (((value_type) 16 << (width << 2)) - 1);
value_type x = x_ & (((value_type) 16 << (W << 2)) - 1);
x_ = (x & mask)
| ((x >> ((j - i) << 2)) & ~mask)
| ((x & ~mask) << ((width - j) << 2));
| ((x & ~mask) << ((W - j) << 2));
}
/** @brief Exchange the elements at positions @a i and @a j. */
void exchange(int i, int j) {
Expand All @@ -251,19 +252,19 @@ template <int width> class kpermuter {
/** @brief Exchange positions of values @a x and @a y. */
void exchange_values(int x, int y) {
value_type diff = 0, p = x_;
for (int i = 0; i < width; ++i, diff <<= 4, p <<= 4) {
int v = (p >> (width << 2)) & 15;
for (int i = 0; i < W; ++i, diff <<= 4, p <<= 4) {
int v = (p >> (W << 2)) & 15;
diff ^= -((v == x) | (v == y)) & (x ^ y);
}
x_ ^= diff;
}

lcdf::String unparse() const;

bool operator==(const kpermuter<width>& x) const {
bool operator==(const kpermuter<W>& x) const {
return x_ == x.x_;
}
bool operator!=(const kpermuter<width>& x) const {
bool operator!=(const kpermuter<W>& x) const {
return !(*this == x);
}

Expand All @@ -274,8 +275,8 @@ template <int width> class kpermuter {
value_type x_;
};

template <int width>
lcdf::String kpermuter<width>::unparse() const
template <int W>
lcdf::String kpermuter<W>::unparse() const
{
char buf[max_width + 3], *s = buf;
value_type p(x_);
Expand All @@ -286,7 +287,7 @@ lcdf::String kpermuter<width>::unparse() const
if (i == n) {
*s++ = ':';
}
if (i == width) {
if (i == W) {
break;
}
if ((p & 15) < 10) {
Expand All @@ -297,7 +298,7 @@ lcdf::String kpermuter<width>::unparse() const
seen |= 1 << (p & 15);
p >>= 4;
}
if (seen != (1 << width) - 1) {
if (seen != (1 << W) - 1) {
*s++ = '?';
*s++ = '!';
}
Expand Down
9 changes: 8 additions & 1 deletion masstree_key.hh
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ class key {
return s;
}
int unparse_printable(char* data, int datalen) const {
String s = unparse().printable();
String s = unparse_printable();
int cplen = std::min(s.length(), datalen);
memcpy(data, s.data(), cplen);
return cplen;
}
String unparse_printable() const {
return unparse().printable();
}
static String unparse_ikey(ikey_type ikey) {
key<ikey_type> k(ikey);
return k.unparse();
}
static String unparse_printable_ikey(ikey_type ikey) {
key<ikey_type> k(ikey);
return k.unparse_printable();
}

// used during scan
Str prefix_string() const {
Expand Down
2 changes: 1 addition & 1 deletion masstree_struct.hh
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class leaf : public node_base<P> {
masstree_precondition(sz % 64 == 0 && sz / 64 < 128);
extrasize64_ = (int(sz) >> 6) - ((int(sizeof(*this)) + 63) >> 6);
if (extrasize64_ > 0) {
new((void *)&iksuf_[0]) internal_ksuf_type(width, sz - sizeof(*this));
new((void*) &iksuf_[0]) internal_ksuf_type(width, sz - sizeof(*this));
}
if (P::need_phantom_epoch) {
phantom_epoch_[0] = phantom_epoch;
Expand Down
47 changes: 27 additions & 20 deletions mttest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ struct kvtest_client {
}
void remove_check(Str key);

void print() {
table_->print(stderr);
}
void puts_done() {
}
void wait_all() {
Expand All @@ -305,13 +308,16 @@ struct kvtest_client {
}
void finish() {
Json counters;
for (int i = 0; i < tc_max; ++i)
for (int i = 0; i < tc_max; ++i) {
if (uint64_t c = ti_->counter(threadcounter(i)))
counters.set(threadcounter_names[i], c);
if (counters)
}
if (counters) {
report_.set("counters", counters);
if (!quiet)
}
if (!quiet) {
fprintf(stderr, "%d: %s\n", ti_->index(), report_.unparse().c_str());
}
}

T *table_;
Expand All @@ -333,8 +339,8 @@ static volatile int kvtest_printing;

template <typename T> inline void kvtest_print(const T &table, FILE* f, threadinfo *ti) {
// only print out the tree from the first failure
while (!bool_cmpxchg((int *) &kvtest_printing, 0, ti->index() + 1))
/* spin */;
while (!bool_cmpxchg((int *) &kvtest_printing, 0, ti->index() + 1)) {
}
table.print(f);
}

Expand Down Expand Up @@ -363,24 +369,25 @@ bool kvtest_client<T>::get_sync(Str key, Str& value) {
template <typename T>
void kvtest_client<T>::get_check(Str key, Str expected) {
Str val;
if (!q_[0].run_get1(table_->table(), key, 0, val, *ti_)) {
fail("get(%.*s) failed (expected %.*s)\n", key.len, key.s,
expected.len, expected.s);
} else if (expected != val) {
fail("get(%.*s) returned unexpected value %.*s (expected %.*s)\n",
key.len, key.s, std::min(val.len, 40), val.s,
expected.len, expected.s);
if (unlikely(!q_[0].run_get1(table_->table(), key, 0, val, *ti_))) {
fail("get(%s) failed (expected %s)\n", String(key).printable().c_str(),
String(expected).printable().c_str());
} else if (unlikely(expected != val)) {
fail("get(%s) returned unexpected value %s (expected %s)\n",
String(key).printable().c_str(),
String(val).substr(0, 40).printable().c_str(),
String(expected).substr(0, 40).printable().c_str());
}
}

template <typename T>
void kvtest_client<T>::get_col_check(Str key, int col,
Str expected) {
Str val;
if (!q_[0].run_get1(table_->table(), key, col, val, *ti_)) {
if (unlikely(!q_[0].run_get1(table_->table(), key, col, val, *ti_))) {
fail("get.%d(%.*s) failed (expected %.*s)\n",
col, key.len, key.s, expected.len, expected.s);
} else if (expected != val) {
} else if (unlikely(expected != val)) {
fail("get.%d(%.*s) returned unexpected value %.*s (expected %.*s)\n",
col, key.len, key.s, std::min(val.len, 40), val.s,
expected.len, expected.s);
Expand All @@ -390,8 +397,8 @@ void kvtest_client<T>::get_col_check(Str key, int col,
template <typename T>
void kvtest_client<T>::get_check_absent(Str key) {
Str val;
if (q_[0].run_get1(table_->table(), key, 0, val, *ti_)) {
fail("get(%.*s) failed (expected absent key)\n", key.len, key.s);
if (unlikely(q_[0].run_get1(table_->table(), key, 0, val, *ti_))) {
fail("get(%s) failed (expected absent key)\n", String(key).printable().c_str());
}
}

Expand Down Expand Up @@ -460,8 +467,8 @@ void kvtest_client<T>::put(Str key, Str value) {

template <typename T>
void kvtest_client<T>::insert_check(Str key, Str value) {
if (q_[0].run_replace(table_->table(), key, value, *ti_) != Inserted) {
fail("insert(%.*s) did not insert\n", key.len, key.s);
if (unlikely(q_[0].run_replace(table_->table(), key, value, *ti_) != Inserted)) {
fail("insert(%s) did not insert\n", String(key).printable().c_str());
}
}

Expand Down Expand Up @@ -495,8 +502,8 @@ bool kvtest_client<T>::remove_sync(Str key) {

template <typename T>
void kvtest_client<T>::remove_check(Str key) {
if (!kvtest_remove(*this, key)) {
fail("remove(%.*s) did not remove\n", key.len, key.s);
if (unlikely(!kvtest_remove(*this, key))) {
fail("remove(%s) did not remove\n", String(key).printable().c_str());
}
}

Expand Down

0 comments on commit cef4cc4

Please sign in to comment.