Skip to content

Commit

Permalink
Initial capacity, buffer size & load factor is now configurable
Browse files Browse the repository at this point in the history
Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
  • Loading branch information
larsewi committed Apr 15, 2024
1 parent 7d1ab55 commit ffdd296
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ AM_CONDITIONAL([NDEBUG], [test x"$debug" = x"no"])
# Config constants.
AC_DEFINE([LCH_DEFAULT_MAX_CHAIN_LENGTH], 2048, [Default max chain length used in block garbage collector])
AC_DEFINE([LCH_JSON_PRETTY_INDENT_SIZE], 2, [Indent size used when composing pretty JSON])
AC_DEFINE([LCH_BUFFER_SIZE], 1024, [Initial buffer size allocated by leech])
AC_DEFINE([LCH_DICT_CAPACITY], 256, [Initial dictionary capacity allocated by leech])
AC_DEFINE([LCH_DICT_LOAD_FACTOR], 0.75f, [Initial dictionary capacity allocated by leech])
AC_DEFINE([LCH_LIST_CAPACITY], 256, [Initial list capacity allocated by leech])

# pkg-check modules.
PKG_CHECK_MODULES([CHECK], [check])
Expand Down
4 changes: 1 addition & 3 deletions lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "files.h"
#include "logger.h"

#define INITIAL_CAPACITY 1028

struct LCH_Buffer {
size_t length;
size_t capacity;
Expand Down Expand Up @@ -60,7 +58,7 @@ static LCH_Buffer *LCH_BufferCreateWithCapacity(size_t capacity) {
}

LCH_Buffer *LCH_BufferCreate(void) {
return LCH_BufferCreateWithCapacity(INITIAL_CAPACITY);
return LCH_BufferCreateWithCapacity(LCH_BUFFER_SIZE);
}

bool LCH_BufferAppend(LCH_Buffer *const self, const char byte) {
Expand Down
2 changes: 0 additions & 2 deletions lib/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#define LCH_MEBIBYTE(n) (n * 1024UL * 1024UL)
#define LCH_GIBIBYTE(n) (n * 1024ULL * 1024ULL * 1024ULL)

#define LCH_BUFFER_SIZE LCH_KIBIBYTE(4)

#define LCH_GENISIS_BLOCK_ID "0000000000000000000000000000000000000000"

#define LCH_LENGTH(x) (sizeof(x) / sizeof(*x))
Expand Down
7 changes: 2 additions & 5 deletions lib/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include <errno.h>
#include <string.h>

#define INITIAL_CAPACITY 64
#define LOAD_FACTOR 0.75f

typedef struct DictElement {
LCH_Buffer *key;
void *value;
Expand All @@ -29,7 +26,7 @@ LCH_Dict *LCH_DictCreate() {
}

self->length = self->in_use = 0;
self->capacity = INITIAL_CAPACITY;
self->capacity = LCH_DICT_CAPACITY;
self->buffer = (DictElement **)calloc(self->capacity, sizeof(DictElement *));

if (self->buffer == NULL) {
Expand Down Expand Up @@ -81,7 +78,7 @@ static size_t ComputeIndex(const LCH_Dict *const dict,
}

static bool EnsureCapacity(LCH_Dict *const dict) {
if (dict->in_use < (dict->capacity * LOAD_FACTOR)) {
if (dict->in_use < (dict->capacity * LCH_DICT_LOAD_FACTOR)) {
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "logger.h"

#define INITIAL_CAPACITY 32

typedef struct ListElement {
void *value;
void (*destroy)(void *);
Expand Down Expand Up @@ -67,7 +65,7 @@ static LCH_List *LCH_ListCreateWithCapacity(const size_t capacity) {
}

LCH_List *LCH_ListCreate() {
return LCH_ListCreateWithCapacity(INITIAL_CAPACITY);
return LCH_ListCreateWithCapacity(LCH_LIST_CAPACITY);
}

size_t LCH_ListLength(const LCH_List *const self) {
Expand Down
8 changes: 4 additions & 4 deletions tests/check_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,19 +960,19 @@ START_TEST(test_LCH_JsonCompose) {
"{\n"
" \"version\": \"" PACKAGE_VERSION
"\",\n"
" \"max_chain_length\": 64.000000,\n"
" \"compression\": false,\n"
" \"tables\": {\n"
" \"BTL\": {\n"
" \"subsidiary_fields\": null,\n"
" \"primary_fields\": [\n"
" \"first_name\",\n"
" \"last_name\",\n"
" \"born\"\n"
" ]\n"
" ],\n"
" \"subsidiary_fields\": null\n"
" }\n"
" },\n"
" \"pretty_json\": true,\n"
" \"max_chain_length\": 64.000000\n"
" \"pretty_json\": true\n"
"}\n";

ck_assert_str_eq(LCH_BufferData(actual), expected);
Expand Down

0 comments on commit ffdd296

Please sign in to comment.