add simple cache algorithm to the hash list

This commit is contained in:
Gabriel Wang 2022-01-24 20:39:06 +00:00 committed by Lyon
parent 0278a9d3fb
commit 123162314c
2 changed files with 46 additions and 1 deletions

View File

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

View File

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