From: Steinar H. Gunderson Date: Mon, 15 Dec 2014 01:08:17 +0000 (+0100) Subject: Drop the arena; BPFENs are bounded by 32 bytes anyway, and the typical is something... X-Git-Url: https://git.sesse.net/?p=remoteglot-book;a=commitdiff_plain;h=33d7fcf900b9c456aea2fc88900fe3c7dd4344e8 Drop the arena; BPFENs are bounded by 32 bytes anyway, and the typical is something like 20, so using those eight bytes for a pointer is a rather slight memory gain. This speeds up the sorting by a few percent due to less memory chasing. --- diff --git a/Makefile b/Makefile index 0e95cd4..1dc36ce 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ PROTOC=protoc all: binloader binlookup binmerger -binloader: binloader.o hash.o arena.o count.pb.o +binloader: binloader.o hash.o count.pb.o binmerger: binmerger.o merge_count.o count.pb.o binlookup: binlookup.o hash.o merge_count.o count.pb.o -binloader.o: binloader.cpp arena.cpp count.pb.h +binloader.o: binloader.cpp count.pb.h binmerger.o: binmerger.cpp merge_count.h count.pb.h binlookup.o: binlookup.cpp merge_count.h count.pb.h @@ -17,4 +17,4 @@ binlookup.o: binlookup.cpp merge_count.h count.pb.h .PHONY: clean clean: - $(RM) binloader binlookup binmerger binloader.o binmerger.o binlookup.o hash.o arena.o count.pb.o count.pb.h count.pb.cc + $(RM) binloader binlookup binmerger binloader.o binmerger.o binlookup.o hash.o count.pb.o count.pb.h count.pb.cc diff --git a/arena.cpp b/arena.cpp deleted file mode 100644 index 048e6db..0000000 --- a/arena.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include "arena.h" - -Arena::Arena() : first(NULL) {} - -Arena::~Arena() -{ - Block *next; - for (Block *b = first; b != NULL; b = next) { - delete[] b->memory; - - next = b->next; - delete b; - } -} - -char *Arena::alloc(size_t bytes) -{ - assert(bytes < BLOCK_SIZE); // Can fix, but we don't need to. - - if (first == NULL || first->used + bytes > BLOCK_SIZE) { - Block *b = new Block; - b->memory = new char[BLOCK_SIZE]; - b->used = 0; - b->next = first; - first = b; - } - - char *ret = first->memory + first->used; - first->used += bytes; - return ret; -} diff --git a/arena.h b/arena.h deleted file mode 100644 index 5d254dd..0000000 --- a/arena.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _ARENA_H -#define _ARENA_H - -// A simple arena for allocating lots of short strings. - -class Arena { -public: - Arena(); - ~Arena(); - - char *alloc(size_t bytes); - -private: - static constexpr size_t BLOCK_SIZE = 1048576; - - struct Block { - char *memory; - size_t used; - Block *next; - }; - Block *first; -}; - -#endif // _ARENA_H diff --git a/binloader.cpp b/binloader.cpp index 3877ffc..acf14d7 100644 --- a/binloader.cpp +++ b/binloader.cpp @@ -12,7 +12,6 @@ #include #include #include "count.pb.h" -#include "arena.h" #include "hash.h" #define DUMMY_TIMESTAMP 32503680000 @@ -42,7 +41,7 @@ static inline int memcmp_different_len(const void *s1, size_t n1, const void *s2 enum Result { WHITE = 0, DRAW, BLACK }; struct Element { - char *bpfen; // includes prev_board_hash + char bpfen[32]; // includes prev_board_hash int bpfen_len; char move[8]; // Na1xc3+ Result result; @@ -59,7 +58,6 @@ struct Element { struct ShardData { vector elems; - unique_ptr arena; // Used to allocate bpfen. int num_written_subshards = 0; }; @@ -119,7 +117,6 @@ void write_subshard(const char *basename, ShardData* shard, int bucket) mtbl_writer_destroy(&mtbl); shard->elems.clear(); - shard->arena.reset(new Arena); } int main(int argc, char **argv) @@ -132,7 +129,6 @@ int main(int argc, char **argv) for (int i = 0; i < num_buckets; ++i) { shards[i].elems.reserve(num_pos_per_subshard); - shards[i].arena.reset(new Arena); } size_t num_elems = 0; @@ -154,6 +150,7 @@ int main(int argc, char **argv) if (bpfen_len == -1) { break; } + assert(bpfen_len <= 32); if (bpfen_len >= int(sizeof(bpfen))) { fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len); // exit(1); @@ -226,7 +223,6 @@ int main(int argc, char **argv) int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets); Element e; - e.bpfen = shards[bucket].arena->alloc(bpfen_len); memcpy(e.bpfen, bpfen, bpfen_len); e.bpfen_len = bpfen_len; strcpy(e.move, move);