From 123162314cab37353c60f9c5c26b7a8b49e02abe Mon Sep 17 00:00:00 2001 From: Gabriel Wang Date: Mon, 24 Jan 2022 20:39:06 +0000 Subject: [PATCH] add simple cache algorithm to the hash list --- src/dataArg.h | 6 +++++- src/dataArgs.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/dataArg.h b/src/dataArg.h index e23e88587..7ab8e5de5 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -28,6 +28,7 @@ #ifndef _arg__H #define _arg__H #include "dataMemory.h" +#include "dataLink.h" typedef uint8_t Arg; typedef uint32_t Hash; @@ -46,7 +47,10 @@ typedef enum { typedef struct __arg __arg; struct __arg { - __arg *next; + union { + __arg *next; + Link link; + }; uint16_t size; uint8_t type; uint8_t : 8; diff --git a/src/dataArgs.c b/src/dataArgs.c index f6e7519b4..77b775f6a 100644 --- a/src/dataArgs.c +++ b/src/dataArgs.c @@ -244,8 +244,17 @@ int32_t args_setArg(Args* self, Arg* arg) { return 0; } + +#ifndef __PIKA_CFG_HASH_LIST_CACHE_SIZE +#define __PIKA_CFG_HASH_LIST_CACHE_SIZE 4 +#endif + LinkNode* args_getNode_hash(Args* self, Hash nameHash) { + +#if 0 + + /* normal list search without cache */ LinkNode* nodeNow = self->firstNode; while (NULL != nodeNow) { @@ -257,6 +266,38 @@ LinkNode* args_getNode_hash(Args* self, Hash nameHash) { nodeNow = content_getNext(nodeNow); } +#else + + LinkNode ** ppnode = (LinkNode **)&(self->firstNode); + int_fast8_t n = 0; + + while (NULL != (*ppnode)) { + Arg* arg = (*ppnode); + Hash thisNameHash = arg_getNameHash(arg); + if (thisNameHash == nameHash) { + + __arg *tmp = (__arg *)(*ppnode); + if (n > __PIKA_CFG_HASH_LIST_CACHE_SIZE) { + + /* the first __PIKA_CFG_HASH_LIST_CACHE_SIZE items in the list + * is considered as a cache. + * Don't make __PIKA_CFG_HASH_LIST_CACHE_SIZE too big, otherwise + * this optimisation is useless. + */ + + /*! remove current node from the list */ + (*ppnode) = (LinkNode *)(tmp->next); + + /*! move the node to the cache */ + tmp->next = (__arg *)(self->firstNode); + self->firstNode = (LinkNode *)tmp; + } + return (LinkNode *)tmp; + } + n++; + ppnode = (LinkNode **)&(((__arg *)(*ppnode))->next); + } +#endif return NULL; }