shrink the buffer as chunks empty #12
2 changed files with 53 additions and 51 deletions
|
@ -292,23 +292,21 @@ __sm_chunk_increase_capacity(__sm_chunk_t *chunk, size_t capacity)
|
|||
static bool
|
||||
__sm_chunk_is_empty(__sm_chunk_t *chunk)
|
||||
{
|
||||
/* The __sm_chunk_t is empty if all flags (in m_data[0]) are zero. */
|
||||
if (chunk->m_data[0] == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* It's also empty if all flags are Zero or None. */
|
||||
register uint8_t *p = (uint8_t *)chunk->m_data;
|
||||
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
||||
if (*p) {
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) {
|
||||
return false;
|
||||
if (chunk->m_data[0] != 0) {
|
||||
/* A chunk is considered empty if all flags are SM_PAYLOAD_ZERO or _NONE. */
|
||||
register uint8_t *p = (uint8_t *)chunk->m_data;
|
||||
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
||||
if (*p) {
|
||||
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
||||
if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* The __sm_chunk_t is empty if all flags (in m_data[0]) are zero. */
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1307,11 +1305,10 @@ sparsemap_get_starting_offset(sparsemap_t *map)
|
|||
return 0;
|
||||
}
|
||||
uint8_t *p = __sm_get_chunk_data(map, 0);
|
||||
sm_idx_t start = *(sm_idx_t *)p;
|
||||
sparsemap_idx_t relative_position = *(sm_idx_t *)p;
|
||||
p += sizeof(sm_idx_t);
|
||||
__sm_chunk_t chunk;
|
||||
__sm_chunk_init(&chunk, p);
|
||||
sparsemap_idx_t relative_position = start;
|
||||
for (size_t m = 0; m < sizeof(sm_bitvec_t); m++, p++) {
|
||||
for (int n = 0; n < SM_FLAGS_PER_INDEX_BYTE; n++) {
|
||||
size_t flags = SM_CHUNK_GET_FLAGS(*p, n);
|
||||
|
|
77
tests/test.c
77
tests/test.c
|
@ -652,6 +652,39 @@ test_api_get_ending_offset(const MunitParameter params[], void *data)
|
|||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_get_starting_offset_rolling_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
(void)params;
|
||||
(void)user_data;
|
||||
sparsemap_t *map = sparsemap(10 * 1024);
|
||||
assert_ptr_not_null(map);
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_api_get_starting_offset_rolling_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
assert_ptr_not_null(map);
|
||||
munit_free(map);
|
||||
}
|
||||
static MunitResult
|
||||
test_api_get_starting_offset_rolling(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
for (sparsemap_idx_t i = 0; i < 10 * 2048; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
if (i > 2047) {
|
||||
sparsemap_set(map, i - 2048, false);
|
||||
assert_true(sparsemap_get_starting_offset(map) == i - 2047);
|
||||
// printf("%d\t%d\t%zu\n", i, i - 2047, sparsemap_get_starting_offset(map));
|
||||
}
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_api_scan_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
|
@ -1214,6 +1247,7 @@ static MunitTest api_test_suite[] = {
|
|||
{ (char *)"/count", test_api_count, test_api_count_setup, test_api_count_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/get_data", test_api_get_data, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/get_starting_offset", test_api_get_starting_offset, test_api_get_starting_offset_setup, test_api_get_starting_offset_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/get_starting_offset/rolling", test_api_get_starting_offset_rolling, test_api_get_starting_offset_rolling_setup, test_api_get_starting_offset_rolling_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/get_ending_offset", test_api_get_ending_offset, test_api_get_ending_offset_setup, test_api_get_ending_offset_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/scan", test_api_scan, test_api_scan_setup, test_api_scan_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/split", test_api_split, test_api_split_setup, test_api_split_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
|
@ -1364,48 +1398,20 @@ test_scale_alternating_tear_down(void *fixture)
|
|||
assert_ptr_not_null(map);
|
||||
munit_free(map);
|
||||
}
|
||||
extern char *bytes_as(double bytes, char *s, size_t size);
|
||||
static MunitResult
|
||||
test_scale_alternating(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
for (int i = 0; i < 8192; i++) {
|
||||
if (i % 2)
|
||||
sparsemap_set(map, i, true);
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
test_scale_8192_setup(const MunitParameter params[], void *user_data)
|
||||
{
|
||||
(void)params;
|
||||
(void)user_data;
|
||||
sparsemap_t *map = sparsemap(10 * 1024);
|
||||
assert_ptr_not_null(map);
|
||||
return (void *)map;
|
||||
}
|
||||
static void
|
||||
test_scale_8192_tear_down(void *fixture)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)fixture;
|
||||
assert_ptr_not_null(map);
|
||||
munit_free(map);
|
||||
}
|
||||
static MunitResult
|
||||
test_scale_8192(const MunitParameter params[], void *data)
|
||||
{
|
||||
sparsemap_t *map = (sparsemap_t *)data;
|
||||
(void)params;
|
||||
|
||||
for (int i = 0; i < 16384; i++) {
|
||||
sparsemap_set(map, i, true);
|
||||
if (i > 2049) {
|
||||
sparsemap_set(map, i - 2049, false);
|
||||
for (sparsemap_idx_t i = 0; i < (1000 * 8192); i++) {
|
||||
if (i % 2) {
|
||||
if (sparsemap_set(map, i, true) != i) {
|
||||
// printf("%zu\n", i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
//assert_true(sparsemap_get_starting_offset(map) == (i / 2048) * 2048);
|
||||
if (i % 2048 == 0) printf("%d\t%d\t%d\t%zu\n", i, i / 2048, i % 2048, sparsemap_get_starting_offset(map));
|
||||
}
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
@ -1658,7 +1664,6 @@ static MunitTest scale_test_suite[] = {
|
|||
#endif
|
||||
{ (char *)"/fuzz", test_scale_fuzz, test_scale_fuzz_setup, test_scale_fuzz_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/alternating", test_scale_alternating, test_scale_alternating_setup, test_scale_alternating_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/8192", test_scale_8192, test_scale_8192_setup, test_scale_8192_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/spans_come_spans_go", test_scale_spans_come_spans_go, test_scale_spans_come_spans_go_setup, test_scale_spans_come_spans_go_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/best-case", test_scale_best_case, test_scale_best_case_setup, test_scale_best_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
{ (char *)"/worst-case", test_scale_worst_case, test_scale_worst_case_setup, test_scale_worst_case_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
|
||||
|
|
Loading…
Reference in a new issue