enh: delay allocation of array in LRU-insert
This commit is contained in:
parent
e16af33fa3
commit
dca556a7e8
|
@ -372,23 +372,34 @@ static void taosLRUCacheShardCleanup(SLRUCacheShard *shard) {
|
||||||
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle,
|
static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *e, LRUHandle **handle,
|
||||||
bool freeOnFail) {
|
bool freeOnFail) {
|
||||||
LRUStatus status = TAOS_LRU_STATUS_OK;
|
LRUStatus status = TAOS_LRU_STATUS_OK;
|
||||||
SArray *lastReferenceList = taosArrayInit(16, POINTER_BYTES);
|
SLRUEntry *toFree = NULL;
|
||||||
|
SArray *lastReferenceList = NULL;
|
||||||
|
if (shard->usage + e->totalCharge > shard->capacity) {
|
||||||
|
lastReferenceList = taosArrayInit(16, POINTER_BYTES);
|
||||||
if (!lastReferenceList) {
|
if (!lastReferenceList) {
|
||||||
taosLRUEntryFree(e);
|
taosLRUEntryFree(e);
|
||||||
return TAOS_LRU_STATUS_FAIL;
|
return TAOS_LRU_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(void)taosThreadMutexLock(&shard->mutex);
|
(void)taosThreadMutexLock(&shard->mutex);
|
||||||
|
|
||||||
|
if (shard->usage + e->totalCharge > shard->capacity && shard->lru.next != &shard->lru) {
|
||||||
|
if (!lastReferenceList) {
|
||||||
|
lastReferenceList = taosArrayInit(16, POINTER_BYTES);
|
||||||
|
if (!lastReferenceList) {
|
||||||
|
taosLRUEntryFree(e);
|
||||||
|
(void)taosThreadMutexUnlock(&shard->mutex);
|
||||||
|
return TAOS_LRU_STATUS_FAIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
taosLRUCacheShardEvictLRU(shard, e->totalCharge, lastReferenceList);
|
taosLRUCacheShardEvictLRU(shard, e->totalCharge, lastReferenceList);
|
||||||
|
}
|
||||||
|
|
||||||
if (shard->usage + e->totalCharge > shard->capacity && (shard->strictCapacity || handle == NULL)) {
|
if (shard->usage + e->totalCharge > shard->capacity && (shard->strictCapacity || handle == NULL)) {
|
||||||
TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
|
TAOS_LRU_ENTRY_SET_IN_CACHE(e, false);
|
||||||
if (handle == NULL) {
|
if (handle == NULL) {
|
||||||
if (!taosArrayPush(lastReferenceList, &e)) {
|
toFree = e;
|
||||||
taosLRUEntryFree(e);
|
|
||||||
goto _exit;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (freeOnFail) {
|
if (freeOnFail) {
|
||||||
taosLRUEntryFree(e);
|
taosLRUEntryFree(e);
|
||||||
|
@ -413,11 +424,7 @@ static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *
|
||||||
taosLRUCacheShardLRURemove(shard, old);
|
taosLRUCacheShardLRURemove(shard, old);
|
||||||
shard->usage -= old->totalCharge;
|
shard->usage -= old->totalCharge;
|
||||||
|
|
||||||
if (!taosArrayPush(lastReferenceList, &old)) {
|
toFree = old;
|
||||||
taosLRUEntryFree(e);
|
|
||||||
taosLRUEntryFree(old);
|
|
||||||
goto _exit;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (handle == NULL) {
|
if (handle == NULL) {
|
||||||
|
@ -434,6 +441,10 @@ static LRUStatus taosLRUCacheShardInsertEntry(SLRUCacheShard *shard, SLRUEntry *
|
||||||
_exit:
|
_exit:
|
||||||
(void)taosThreadMutexUnlock(&shard->mutex);
|
(void)taosThreadMutexUnlock(&shard->mutex);
|
||||||
|
|
||||||
|
if (toFree) {
|
||||||
|
taosLRUEntryFree(toFree);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
|
for (int i = 0; i < taosArrayGetSize(lastReferenceList); ++i) {
|
||||||
SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
|
SLRUEntry *entry = taosArrayGetP(lastReferenceList, i);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue