shrink the buffer as chunks empty #12

Merged
greg merged 2 commits from gburd/dyn-size into main 2024-07-02 13:07:25 +00:00
3 changed files with 87 additions and 21 deletions

View file

@ -33,7 +33,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" ON)
# Add shared library
add_library(sparsemap_SHARED SHARED ${LIB_SRC} ${HEADERS})
# Set target properties for shared library (adjust if needed)
# Set target properties for the shared library (adjust if needed)
set_target_properties(sparsemap_SHARED PROPERTIES
VERSION 1.0.0 # Set library version
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" # Set output directory
@ -87,8 +87,8 @@ target_link_libraries(soak PUBLIC m)
add_custom_target(run_soak COMMAND soak WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# Add fuzzer program
add_executable(fuzzer tests/fuzzer.c)
target_link_libraries(fuzzer PRIVATE sparsemap)
target_include_directories(fuzzer PRIVATE ${HEADER_DIR} lib)
target_link_libraries(fuzzer PUBLIC m)
add_custom_target(run_fuzzer COMMAND fuzzer WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# add_executable(fuzzer tests/fuzzer.c)
# target_link_libraries(fuzzer PRIVATE sparsemap)
# target_include_directories(fuzzer PRIVATE ${HEADER_DIR} lib)
# target_link_libraries(fuzzer PUBLIC m)
# add_custom_target(run_fuzzer COMMAND fuzzer WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

View file

@ -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);

View file

@ -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 },
@ -1348,6 +1382,40 @@ test_scale_fuzz(const MunitParameter params[], void *data)
return MUNIT_OK;
}
static void *
test_scale_alternating_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_alternating_tear_down(void *fixture)
{
sparsemap_t *map = (sparsemap_t *)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 (sparsemap_idx_t i = 0; i < (1000 * 8192); i++) {
if (i % 2) {
if (sparsemap_set(map, i, true) != i) {
// printf("%zu\n", i);
break;
}
}
}
return MUNIT_OK;
}
static void *
test_scale_spans_come_spans_go_setup(const MunitParameter params[], void *user_data)
{
@ -1595,6 +1663,7 @@ static MunitTest scale_test_suite[] = {
{ (char *)"/ondrej", test_scale_ondrej, test_scale_ondrej_setup, test_scale_ondrej_tear_down, MUNIT_TEST_OPTION_NONE, NULL },
#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 *)"/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 },