From ec26266fe6b05bdb6863498f77299f38d571d996 Mon Sep 17 00:00:00 2001 From: 6unyoung Date: Fri, 28 Nov 2025 18:30:14 +0900 Subject: [PATCH] INTERNAL: Bundle nested fields of prefix_t with NESTED_PREFIX --- engines/default/prefix.c | 45 +++++++++++++++++++++++++++------------- engines/default/prefix.h | 8 +++---- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/engines/default/prefix.c b/engines/default/prefix.c index cff69199b..8d629d65a 100644 --- a/engines/default/prefix.c +++ b/engines/default/prefix.c @@ -266,8 +266,6 @@ static int _prefix_insert(prefix_t *pt, uint32_t hash) prefxp->total_prefix_items++; } #else - assert(pt->parent_prefix != NULL); - pt->parent_prefix->child_prefix_items++; prefxp->total_prefix_items++; #endif return 1; @@ -294,8 +292,6 @@ static void _prefix_delete(const char *prefix, const int nprefix, uint32_t hash) prefxp->total_prefix_items--; } #else - assert(pt->parent_prefix != NULL); - pt->parent_prefix->child_prefix_items--; prefxp->total_prefix_items--; #endif /* unlink and free the prefix structure */ @@ -351,6 +347,9 @@ ENGINE_ERROR_CODE prefix_link(hash_item *it, const uint32_t item_size, bool *int if (prefix_depth >= DEFAULT_PREFIX_MAX_DEPTH) { break; } +#ifndef NESTED_PREFIX + break; +#endif } if (prefix_depth == 0) { @@ -413,7 +412,6 @@ ENGINE_ERROR_CODE prefix_link(hash_item *it, const uint32_t item_size, bool *int if (PREFIX_IS_RSVD(key, pt->nprefix)) { pt->internal = 1; /* internal prefix */ } - pt->parent_prefix = (j == 0 ? null_pt : prefix_list[j-1].pt); #endif time(&pt->create_time); @@ -445,13 +443,22 @@ void prefix_unlink(hash_item *it, const uint32_t item_size, bool drop_if_empty) if (drop_if_empty) { while (pt != NULL && pt != null_pt) { - prefix_t *parent_pt = pt->parent_prefix; - if (pt->child_prefix_items > 0 || pt->total_count_exclusive > 0) + bool has_items = pt->total_count_exclusive > 0; + bool has_children = false; + prefix_t *next_pt = NULL; + +#ifdef NESTED_PREFIX + has_children = pt->child_prefix_items > 0; + next_pt = pt->parent_prefix; +#endif + if (has_items || has_children) { break; /* NOT empty */ + } + assert(pt->total_bytes_exclusive == 0); _prefix_delete(_get_prefix(pt), pt->nprefix, svcore->hash(_get_prefix(pt), pt->nprefix, 0)); - pt = parent_pt; + pt = next_pt; } } } @@ -512,8 +519,12 @@ bool prefix_isvalid(hash_item *it, rel_time_t current_time) it->time <= pt->oldest_live) return false; /* traverse parent prefixes to validate them */ +#ifdef NESTED_PREFIX pt = pt->parent_prefix; - } while(pt != NULL && pt != null_pt); +#else + pt = NULL; +#endif + } while (pt != NULL && pt != null_pt); return true; } @@ -528,8 +539,12 @@ static uint32_t do_count_invalid_prefix(void) for (i = 0; i < size; i++) { pt = prefxp->hashtable[i]; while (pt) { - if (pt->child_prefix_items == 0 && pt->total_count_exclusive == 0) - invalid_prefix++; + if (pt->total_count_exclusive == 0) { +#ifdef NESTED_PREFIX + if (pt->child_prefix_items == 0) +#endif + invalid_prefix++; + } pt = pt->h_next; } } @@ -593,7 +608,6 @@ static int _prefix_stats_write_buffer(char *buffer, const size_t buflen, pt->items_bytes_exclusive[ITEM_TYPE_MAP], pt->items_bytes_exclusive[ITEM_TYPE_BTREE], /* FUTURE: NESTED_PREFIX - (uint64_t)pt->child_prefix_items, (uint64_t)0, (uint64_t)0, */ @@ -665,7 +679,6 @@ char *prefix_dump_stats(token_t *tokens, const size_t ntokens, int *length) /* write prefix stats in the buffer */ if (num_prefixes > prefxp->total_prefix_items) { /* include null prefix */ pt = null_pt; - assert(pt->child_prefix_items == 0); pos += _prefix_stats_write_buffer(buffer+pos, buflen-pos, format, pt, false); assert(pos < buflen); } @@ -714,7 +727,11 @@ char *prefix_dump_stats(token_t *tokens, const size_t ntokens, int *length) #ifdef SCAN_COMMAND static bool _prefix_isempty(prefix_t *pt) { - return pt->child_prefix_items == 0 && pt->total_count_exclusive == 0; +#ifdef NESTED_PREFIX + return pt->child_prefix_items == 0 && pt->total_count_exclusive == 0; +#else + return pt->total_count_exclusive == 0; +#endif } static int _prefix_scan_direct(const char *cursor, int req_count, void **item_array, int item_arrsz) diff --git a/engines/default/prefix.h b/engines/default/prefix.h index b346e9f75..ae79b8ec7 100644 --- a/engines/default/prefix.h +++ b/engines/default/prefix.h @@ -36,10 +36,6 @@ typedef struct _prefix_t { time_t create_time; struct _prefix_t *h_next; /* prefix hash chain */ - struct _prefix_t *parent_prefix; - - /* Number of child prefix items */ - uint32_t child_prefix_items; /* count and bytes of cache items per item type */ uint64_t total_count_exclusive; @@ -48,6 +44,10 @@ typedef struct _prefix_t { uint64_t items_bytes_exclusive[ITEM_TYPE_MAX]; #ifdef NESTED_PREFIX + struct _prefix_t *parent_prefix; + /* Number of child prefix items */ + uint32_t child_prefix_items; + /* includes cache items that belong to child prefixes */ uint64_t total_count_inclusive; /* NOT yet used */ uint64_t total_bytes_inclusive; /* NOT yet used */