Minor optimisations and bug fixes

Fix bug in mt_mpmc.c, in Linux mutexes are not recursive.
Add arena_trim_string() to the arena API
Removing arena->path, now paths are pushed to arena->metadata
Replacing fe->owner[128] with char *owner; the owner is not pushed as a
string to arena->metadata and trimed with arena_trim_string()
Improving cache locality in arena->metadata, the memory layout is not
fe; fe->path; fe->owner.
Cache aligning all arenas except HasherContext->arena to sizeof(void *).
Pushing elements one by one instead of snprintf() in finalize_file() and
hash_worker().
Getting the full path of current directory instead of "."
Fixing bug in path formating, this allow us to remove normalize_path()
from the hot loop.
This commit is contained in:
2026-05-08 15:45:57 +01:00
parent 7d2a24d0be
commit 16c6aeae65
8 changed files with 352 additions and 160 deletions

123
arena.c
View File

@@ -196,7 +196,7 @@ mem_arena *arena_create(arena_params *params) { // mk create
arena->free_list = arena_create(&(arena_params){
.reserve_size = MiB(1),
.commit_size = MiB(1),
.align = ARENA_ALIGN,
.align = ARENA_CACHE_ALIGN,
.push_size = sizeof(arena_free_node),
.allow_free_list = false,
.free_list = NULL,
@@ -438,12 +438,14 @@ void *arena_push(mem_arena **arena_ptr, u64 size, bool zero) { // mk push
if (local_post > selected->commit_pos -
ALIGN_UP_POW2(sizeof(mem_arena), selected->align)) {
u64 new_commit = ALIGN_UP_POW2(local_post + ALIGN_UP_POW2(sizeof(mem_arena), selected->align), arena_pagesize());
u64 new_commit = ALIGN_UP_POW2(
local_post + ALIGN_UP_POW2(sizeof(mem_arena), selected->align),
arena_pagesize());
new_commit = MIN(new_commit, selected->reserve_size);
if (!plat_mem_commit((u8 *)selected + selected->commit_pos,
new_commit - selected->commit_pos)) {
printf("ERROR: Could not commit memory!\n");
printf("ERROR: Could not commit memory!\n");
return NULL;
}
@@ -620,6 +622,119 @@ void *arena_swapback_pop(mem_arena **arena_ptr, u64 index) { // mk swapback
/* ============================================================
Utilities
============================================================ */
typedef enum arena_trim_flags {
ARENA_TRIM_NONE = 0,
ARENA_TRIM_SPACE = 1 << 0,
ARENA_TRIM_TAB = 1 << 1,
ARENA_TRIM_LF = 1 << 2,
ARENA_TRIM_CR = 1 << 3,
ARENA_TRIM_NUL = 1 << 4,
} arena_trim_flags;
u64 arena_trim_string(mem_arena **arena_ptr, char *str, u8 termination_flags) {
ASSERT(arena_ptr);
ASSERT(*arena_ptr);
ASSERT(str);
if (!arena_ptr || !*arena_ptr || !str) {
return 0;
}
mem_arena *arena = *arena_ptr;
/* ------------------------------------------------------------
Find owning block
------------------------------------------------------------ */
mem_arena *owner = arena_block_from_ptr(arena, (u8 *)str);
ASSERT(owner);
if (!owner) {
return 0;
}
/* ------------------------------------------------------------
Must be current block
------------------------------------------------------------ */
if (owner != arena) {
fprintf(stderr, "arena_trim_string(): string is not "
"in current arena block.\n");
return 0;
}
/* ------------------------------------------------------------
Compute string position
------------------------------------------------------------ */
u64 str_pos = arena_pos_from_ptr(arena, str);
/* ------------------------------------------------------------
Original reserved size
------------------------------------------------------------ */
u64 allocated_size = arena->pos - str_pos;
/* ------------------------------------------------------------
Compute sizes
------------------------------------------------------------ */
u64 str_size = strlen(str);
char *dst = str + str_size;
u64 termination_size = 0;
if (termination_flags & ARENA_TRIM_SPACE) {
*dst++ = ' ';
termination_size++;
}
if (termination_flags & ARENA_TRIM_TAB) {
*dst++ = '\t';
termination_size++;
}
if (termination_flags & ARENA_TRIM_CR) {
*dst++ = '\r';
termination_size++;
}
if (termination_flags & ARENA_TRIM_LF) {
*dst++ = '\n';
termination_size++;
}
if (termination_flags & ARENA_TRIM_NUL) {
*dst++ = '\0';
termination_size++;
}
/* ------------------------------------------------------------
Final used size
------------------------------------------------------------ */
u64 used_size = str_size + termination_size;
used_size = ALIGN_UP_POW2(used_size, arena->align);
/* ------------------------------------------------------------
Overflow detection
------------------------------------------------------------ */
if (used_size > allocated_size) {
fprintf(stderr, "arena_trim_string(): string overflow "
"detected.\n");
}
/* ------------------------------------------------------------
Update arena position
------------------------------------------------------------ */
arena->pos = str_pos + used_size;
return used_size;
}
void *arena_clear(mem_arena **arena_ptr) { // mk clear
@@ -801,7 +916,7 @@ mem_arena_temp arena_scratch_get(mem_arena **conflicts, u32 num_conflicts) {
arena_params params = {
.reserve_size = MiB(64),
.commit_size = MiB(1),
.align = ARENA_ALIGN,
.align = ARENA_CACHE_ALIGN,
.push_size = 8,
.allow_free_list = false,
.allow_swapback = true,