Skip to content

Commit

Permalink
Clean up arena API
Browse files Browse the repository at this point in the history
Summary:
Easy thing goes first. This patch moves arena to internal dir; based
on which, the coming patch will deal with memtable_rep.

Test Plan: make check

Reviewers: haobo, sdong, dhruba

CC: leveldb

Differential Revision: https://reviews.facebook.net/D15615
  • Loading branch information
liukai committed Jan 31, 2014
1 parent ac92420 commit 4e0298f
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 126 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Rocksdb Change Log

## 2.8.0 (01/28/2014)

### Public API changes

* Removed arena.h from public header files.

## 2.7.0 (01/28/2014)

### Public API changes
Expand Down
12 changes: 6 additions & 6 deletions db/memtable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
#include "rocksdb/merge_operator.h"
#include "rocksdb/slice_transform.h"
#include "util/arena.h"
#include "util/coding.h"
#include "util/murmurhash.h"
#include "util/mutexlock.h"
Expand All @@ -38,9 +40,8 @@ namespace rocksdb {
MemTable::MemTable(const InternalKeyComparator& cmp, const Options& options)
: comparator_(cmp),
refs_(0),
arena_impl_(options.arena_block_size),
table_(options.memtable_factory->CreateMemTableRep(comparator_,
&arena_impl_)),
arena_(options.arena_block_size),
table_(options.memtable_factory->CreateMemTableRep(comparator_, &arena_)),
flush_in_progress_(false),
flush_completed_(false),
file_number_(0),
Expand All @@ -61,8 +62,7 @@ MemTable::~MemTable() {
}

size_t MemTable::ApproximateMemoryUsage() {
return arena_impl_.ApproximateMemoryUsage() +
table_->ApproximateMemoryUsage();
return arena_.ApproximateMemoryUsage() + table_->ApproximateMemoryUsage();
}

int MemTable::KeyComparator::operator()(const char* prefix_len_key1,
Expand Down Expand Up @@ -184,7 +184,7 @@ void MemTable::Add(SequenceNumber s, ValueType type,
const size_t encoded_len =
VarintLength(internal_key_size) + internal_key_size +
VarintLength(val_size) + val_size;
char* buf = arena_impl_.Allocate(encoded_len);
char* buf = arena_.Allocate(encoded_len);
char* p = EncodeVarint32(buf, internal_key_size);
memcpy(p, key.data(), key_size);
p += key_size;
Expand Down
4 changes: 2 additions & 2 deletions db/memtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "db/version_set.h"
#include "rocksdb/db.h"
#include "rocksdb/memtablerep.h"
#include "util/arena_impl.h"
#include "util/arena.h"
#include "util/dynamic_bloom.h"

namespace rocksdb {
Expand Down Expand Up @@ -161,7 +161,7 @@ class MemTable {

KeyComparator comparator_;
int refs_;
ArenaImpl arena_impl_;
Arena arena_;
unique_ptr<MemTableRep> table_;

// These are used to manage memtable flushes to storage
Expand Down
7 changes: 4 additions & 3 deletions db/plain_table_db_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
#include <algorithm>
#include <set>

#include "rocksdb/db.h"
#include "rocksdb/filter_policy.h"
#include "db/db_impl.h"
#include "db/filename.h"
#include "db/version_set.h"
#include "db/write_batch_internal.h"
#include "rocksdb/cache.h"
#include "rocksdb/compaction_filter.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "rocksdb/table.h"
#include "rocksdb/filter_policy.h"
#include "rocksdb/plain_table_factory.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/table.h"
#include "util/hash.h"
#include "util/logging.h"
#include "util/mutexlock.h"
Expand Down
1 change: 1 addition & 0 deletions db/simple_table_db_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

using std::unique_ptr;

// IS THIS FILE STILL NEEDED?
namespace rocksdb {

// SimpleTable is a simple table format for UNIT TEST ONLY. It is not built
Expand Down
2 changes: 1 addition & 1 deletion db/skiplist.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include <assert.h>
#include <stdlib.h>
#include "port/port.h"
#include "util/arena.h"
#include "util/random.h"
#include "rocksdb/arena.h"

namespace rocksdb {

Expand Down
14 changes: 7 additions & 7 deletions db/skiplist_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "db/skiplist.h"
#include <set>
#include "rocksdb/env.h"
#include "util/arena_impl.h"
#include "util/arena.h"
#include "util/hash.h"
#include "util/random.h"
#include "util/testharness.h"
Expand All @@ -34,9 +34,9 @@ struct TestComparator {
class SkipTest { };

TEST(SkipTest, Empty) {
ArenaImpl arena_impl;
Arena arena;
TestComparator cmp;
SkipList<Key, TestComparator> list(cmp, &arena_impl);
SkipList<Key, TestComparator> list(cmp, &arena);
ASSERT_TRUE(!list.Contains(10));

SkipList<Key, TestComparator>::Iterator iter(&list);
Expand All @@ -54,9 +54,9 @@ TEST(SkipTest, InsertAndLookup) {
const int R = 5000;
Random rnd(1000);
std::set<Key> keys;
ArenaImpl arena_impl;
Arena arena;
TestComparator cmp;
SkipList<Key, TestComparator> list(cmp, &arena_impl);
SkipList<Key, TestComparator> list(cmp, &arena);
for (int i = 0; i < N; i++) {
Key key = rnd.Next() % R;
if (keys.insert(key).second) {
Expand Down Expand Up @@ -209,14 +209,14 @@ class ConcurrentTest {
// Current state of the test
State current_;

ArenaImpl arena_impl_;
Arena arena_;

// SkipList is not protected by mu_. We just use a single writer
// thread to modify it.
SkipList<Key, TestComparator> list_;

public:
ConcurrentTest() : list_(TestComparator(), &arena_impl_) { }
ConcurrentTest() : list_(TestComparator(), &arena_) {}

// REQUIRES: External synchronization
void WriteStep(Random* rnd) {
Expand Down
45 changes: 0 additions & 45 deletions include/rocksdb/arena.h

This file was deleted.

6 changes: 1 addition & 5 deletions include/rocksdb/memtablerep.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
// iteration over the entire collection is rare since doing so requires all the
// keys to be copied into a sorted data structure.

#ifndef STORAGE_ROCKSDB_DB_MEMTABLEREP_H_
#define STORAGE_ROCKSDB_DB_MEMTABLEREP_H_
#pragma once

#include <memory>
#include "rocksdb/slice_transform.h"

namespace rocksdb {

Expand Down Expand Up @@ -199,5 +197,3 @@ extern MemTableRepFactory* NewHashLinkListRepFactory(
const SliceTransform* transform, size_t bucket_count = 50000);

}

#endif // STORAGE_ROCKSDB_DB_MEMTABLEREP_H_
1 change: 1 addition & 0 deletions table/table_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rocksdb/plain_table_factory.h"
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/memtablerep.h"
#include "table/meta_blocks.h"
#include "rocksdb/plain_table_factory.h"
Expand Down
6 changes: 3 additions & 3 deletions tools/sst_dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ int main(int argc, char** argv) {
"Table Properties:\n"
"------------------------------\n"
" %s", table_properties.ToString("\n ", ": ").c_str());
fprintf(stdout, "# deleted keys: %lu\n",
rocksdb::GetDeletedKeys(
table_properties.user_collected_properties));
fprintf(stdout, "# deleted keys: %zd\n",
rocksdb::GetDeletedKeys(
table_properties.user_collected_properties));
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions util/arena_impl.cc → util/arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "util/arena_impl.h"
#include "util/arena.h"
#include <algorithm>

namespace rocksdb {

const size_t ArenaImpl::kMinBlockSize = 4096;
const size_t ArenaImpl::kMaxBlockSize = 2 << 30;
const size_t Arena::kMinBlockSize = 4096;
const size_t Arena::kMaxBlockSize = 2 << 30;
static const int kAlignUnit = sizeof(void*);

size_t OptimizeBlockSize(size_t block_size) {
// Make sure block_size is in optimal range
block_size = std::max(ArenaImpl::kMinBlockSize, block_size);
block_size = std::min(ArenaImpl::kMaxBlockSize, block_size);
block_size = std::max(Arena::kMinBlockSize, block_size);
block_size = std::min(Arena::kMaxBlockSize, block_size);

// make sure block_size is the multiple of kAlignUnit
if (block_size % kAlignUnit != 0) {
Expand All @@ -29,19 +29,18 @@ size_t OptimizeBlockSize(size_t block_size) {
return block_size;
}

ArenaImpl::ArenaImpl(size_t block_size)
: kBlockSize(OptimizeBlockSize(block_size)) {
Arena::Arena(size_t block_size) : kBlockSize(OptimizeBlockSize(block_size)) {
assert(kBlockSize >= kMinBlockSize && kBlockSize <= kMaxBlockSize &&
kBlockSize % kAlignUnit == 0);
}

ArenaImpl::~ArenaImpl() {
Arena::~Arena() {
for (const auto& block : blocks_) {
delete[] block;
}
}

char* ArenaImpl::AllocateFallback(size_t bytes, bool aligned) {
char* Arena::AllocateFallback(size_t bytes, bool aligned) {
if (bytes > kBlockSize / 4) {
// Object is more than a quarter of our block size. Allocate it separately
// to avoid wasting too much space in leftover bytes.
Expand All @@ -63,7 +62,7 @@ char* ArenaImpl::AllocateFallback(size_t bytes, bool aligned) {
}
}

char* ArenaImpl::AllocateAligned(size_t bytes) {
char* Arena::AllocateAligned(size_t bytes) {
assert((kAlignUnit & (kAlignUnit - 1)) ==
0); // Pointer size should be a power of 2
size_t current_mod =
Expand All @@ -83,7 +82,7 @@ char* ArenaImpl::AllocateAligned(size_t bytes) {
return result;
}

char* ArenaImpl::AllocateNewBlock(size_t block_bytes) {
char* Arena::AllocateNewBlock(size_t block_bytes) {
char* block = new char[block_bytes];
blocks_memory_ += block_bytes;
blocks_.push_back(block);
Expand Down
26 changes: 12 additions & 14 deletions util/arena_impl.h → util/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

// ArenaImpl is an implementation of Arena class. For a request of small size,
// Arena is an implementation of Arena class. For a request of small size,
// it allocates a block with pre-defined block size. For a request of big
// size, it uses malloc to directly get the requested size.

Expand All @@ -16,37 +16,35 @@
#include <vector>
#include <assert.h>
#include <stdint.h>
#include "rocksdb/arena.h"
#include "util/arena.h"

namespace rocksdb {

class ArenaImpl : public Arena {
class Arena {
public:
// No copying allowed
ArenaImpl(const ArenaImpl&) = delete;
void operator=(const ArenaImpl&) = delete;
Arena(const Arena&) = delete;
void operator=(const Arena&) = delete;

static const size_t kMinBlockSize;
static const size_t kMaxBlockSize;

explicit ArenaImpl(size_t block_size = kMinBlockSize);
virtual ~ArenaImpl();
explicit Arena(size_t block_size = kMinBlockSize);
~Arena();

virtual char* Allocate(size_t bytes) override;
char* Allocate(size_t bytes);

virtual char* AllocateAligned(size_t bytes) override;
char* AllocateAligned(size_t bytes);

// Returns an estimate of the total memory usage of data allocated
// by the arena (exclude the space allocated but not yet used for future
// allocations).
virtual const size_t ApproximateMemoryUsage() {
const size_t ApproximateMemoryUsage() {
return blocks_memory_ + blocks_.capacity() * sizeof(char*) -
alloc_bytes_remaining_;
}

virtual const size_t MemoryAllocatedBytes() override {
return blocks_memory_;
}
const size_t MemoryAllocatedBytes() { return blocks_memory_; }

private:
// Number of bytes allocated in one block
Expand All @@ -72,7 +70,7 @@ class ArenaImpl : public Arena {
size_t blocks_memory_ = 0;
};

inline char* ArenaImpl::Allocate(size_t bytes) {
inline char* Arena::Allocate(size_t bytes) {
// The semantics of what to return are a bit messy if we allow
// 0-byte allocations, so we disallow them here (we don't need
// them for our internal use).
Expand Down
Loading

0 comments on commit 4e0298f

Please sign in to comment.