blob: ca93d76d984c6d66f75b910a0cd44631563e8af1 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file hash_table.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang dictionary for storing strings and generic hash table
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include <string.h>
16#include <stdint.h>
17#include <stdlib.h>
18#include <pthread.h>
19#include <assert.h>
20
21#include "common.h"
22#include "context.h"
23#include "hash_table.h"
24
25static int
26lydict_val_eq(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
27{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020028 LY_CHECK_ARG_RET(NULL, val1_p, val2_p, cb_data, 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020029
30 const char *str1 = ((struct dict_rec *)val1_p)->value;
31 const char *str2 = ((struct dict_rec *)val2_p)->value;
32
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020033 LY_CHECK_ERR_RET(!str1, LOGARG(NULL, val1_p), 0);
34 LY_CHECK_ERR_RET(!str2, LOGARG(NULL, val2_p), 0);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020035
36 if (strncmp(str1, str2, *(size_t *)cb_data) == 0) {
37 return 1;
38 }
39
40 return 0;
41}
42
43void
44lydict_init(struct dict_table *dict)
45{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020046 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047
48 dict->hash_tab = lyht_new(1024, sizeof(struct dict_rec), lydict_val_eq, NULL, 1);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020049 LY_CHECK_ERR_RET(!dict->hash_tab, LOGINT(NULL), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +020050 pthread_mutex_init(&dict->lock, NULL);
51}
52
53void
54lydict_clean(struct dict_table *dict)
55{
56 unsigned int i;
57 struct dict_rec *dict_rec = NULL;
58 struct ht_rec *rec = NULL;
59
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020060 LY_CHECK_ARG_RET(NULL, dict,);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020061
62 for (i = 0; i < dict->hash_tab->size; i++) {
63 /* get ith record */
64 rec = (struct ht_rec *)&dict->hash_tab->recs[i * dict->hash_tab->rec_size];
65 if (rec->hits == 1) {
66 /*
67 * this should not happen, all records inserted into
68 * dictionary are supposed to be removed using lydict_remove()
69 * before calling lydict_clean()
70 */
71 dict_rec = (struct dict_rec *)rec->val;
72 LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
73 /* if record wasn't removed before free string allocated for that record */
74#ifdef NDEBUG
75 free(dict_rec->value);
76#endif
77 }
78 }
79
80 /* free table and destroy mutex */
81 lyht_free(dict->hash_tab);
82 pthread_mutex_destroy(&dict->lock);
83}
84
85/*
86 * Bob Jenkin's one-at-a-time hash
87 * http://www.burtleburtle.net/bob/hash/doobs.html
88 *
89 * Spooky hash is faster, but it works only for little endian architectures.
90 */
91static uint32_t
92dict_hash(const char *key, size_t len)
93{
94 uint32_t hash, i;
95
96 for (hash = i = 0; i < len; ++i) {
97 hash += key[i];
98 hash += (hash << 10);
99 hash ^= (hash >> 6);
100 }
101 hash += (hash << 3);
102 hash ^= (hash >> 11);
103 hash += (hash << 15);
104 return hash;
105}
106
107/*
108 * Usage:
109 * - init hash to 0
110 * - repeatedly call dict_hash_multi(), provide hash from the last call
111 * - call dict_hash_multi() with key_part = NULL to finish the hash
112 */
113uint32_t
114dict_hash_multi(uint32_t hash, const char *key_part, size_t len)
115{
116 uint32_t i;
117
118 if (key_part) {
119 for (i = 0; i < len; ++i) {
120 hash += key_part[i];
121 hash += (hash << 10);
122 hash ^= (hash >> 6);
123 }
124 } else {
125 hash += (hash << 3);
126 hash ^= (hash >> 11);
127 hash += (hash << 15);
128 }
129
130 return hash;
131}
132
133API void
134lydict_remove(struct ly_ctx *ctx, const char *value)
135{
136 size_t len;
137 int ret;
138 uint32_t hash;
139 struct dict_rec rec, *match = NULL;
140 char *val_p;
141
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200142 if (!value) {
Michal Vasko0b3a4172018-09-07 12:20:18 +0200143 return;
Michal Vasko0f6b3e22018-09-07 12:18:12 +0200144 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200145
146 len = strlen(value);
147 hash = dict_hash(value, len);
148
149 /* create record for lyht_find call */
150 rec.value = (char *)value;
151 rec.refcount = 0;
152
153 pthread_mutex_lock(&ctx->dict.lock);
154 /* set len as data for compare callback */
155 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
156 /* check if value is already inserted */
157 ret = lyht_find(ctx->dict.hash_tab, &rec, hash, (void **)&match);
158
159 if (ret == 0) {
160 LY_CHECK_ERR_GOTO(!match, LOGINT(ctx), finish);
161
162 /* if value is already in dictionary, decrement reference counter */
163 match->refcount--;
164 if (match->refcount == 0) {
165 /*
166 * remove record
167 * save pointer to stored string before lyht_remove to
168 * free it after it is removed from hash table
169 */
170 val_p = match->value;
171 ret = lyht_remove(ctx->dict.hash_tab, &rec, hash);
172 free(val_p);
173 LY_CHECK_ERR_GOTO(ret, LOGINT(ctx), finish);
174 }
175 }
176
177finish:
178 pthread_mutex_unlock(&ctx->dict.lock);
179}
180
181static char *
182dict_insert(struct ly_ctx *ctx, char *value, size_t len, int zerocopy)
183{
184 struct dict_rec *match = NULL, rec;
185 int ret = 0;
186 uint32_t hash;
187
188 hash = dict_hash(value, len);
189 /* set len as data for compare callback */
190 lyht_set_cb_data(ctx->dict.hash_tab, (void *)&len);
191 /* create record for lyht_insert */
192 rec.value = value;
193 rec.refcount = 1;
194
195 LOGDBG(LY_LDGDICT, "inserting \"%s\"", rec.value);
196 ret = lyht_insert(ctx->dict.hash_tab, (void *)&rec, hash, (void **)&match);
Michal Vaskofcc96b92018-09-12 11:12:01 +0200197 if (ret == LY_EEXIST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200198 match->refcount++;
199 if (zerocopy) {
200 free(value);
201 }
Michal Vaskofcc96b92018-09-12 11:12:01 +0200202 } else if (ret == LY_SUCCESS) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200203 if (!zerocopy) {
204 /*
205 * allocate string for new record
206 * record is already inserted in hash table
207 */
208 match->value = malloc(sizeof *match->value * (len + 1));
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200209 LY_CHECK_ERR_RET(!match->value, LOGMEM(ctx), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200210 memcpy(match->value, value, len);
211 match->value[len] = '\0';
212 }
213 } else {
214 /* lyht_insert returned error */
215 LOGINT(ctx);
216 return NULL;
217 }
218
219 return match->value;
220}
221
222API const char *
223lydict_insert(struct ly_ctx *ctx, const char *value, size_t len)
224{
225 const char *result;
226
Radek Krejci0ae092d2018-09-20 16:43:19 +0200227 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200228
Radek Krejci0ae092d2018-09-20 16:43:19 +0200229 if (!len) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200230 len = strlen(value);
231 }
232
233 pthread_mutex_lock(&ctx->dict.lock);
234 result = dict_insert(ctx, (char *)value, len, 0);
235 pthread_mutex_unlock(&ctx->dict.lock);
236
237 return result;
238}
239
240API const char *
241lydict_insert_zc(struct ly_ctx *ctx, char *value)
242{
243 const char *result;
244
Radek Krejci0ae092d2018-09-20 16:43:19 +0200245 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200246
247 pthread_mutex_lock(&ctx->dict.lock);
248 result = dict_insert(ctx, value, strlen(value), 1);
249 pthread_mutex_unlock(&ctx->dict.lock);
250
251 return result;
252}
253
254static struct ht_rec *
255lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx)
256{
257 return (struct ht_rec *)&recs[idx * rec_size];
258}
259
260struct hash_table *
261lyht_new(uint32_t size, uint16_t val_size, values_equal_cb val_equal, void *cb_data, int resize)
262{
263 struct hash_table *ht;
264
265 /* check that 2^x == size (power of 2) */
266 assert(size && !(size & (size - 1)));
267 assert(val_equal && val_size);
268 assert(resize == 0 || resize == 1);
269
270 if (size < LYHT_MIN_SIZE) {
271 size = LYHT_MIN_SIZE;
272 }
273
274 ht = malloc(sizeof *ht);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200275 LY_CHECK_ERR_RET(!ht, LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200276
277 ht->used = 0;
278 ht->size = size;
279 ht->val_equal = val_equal;
280 ht->cb_data = cb_data;
281 ht->resize = (uint16_t)resize;
282
283 ht->rec_size = (sizeof(struct ht_rec) - 1) + val_size;
284 /* allocate the records correctly */
285 ht->recs = calloc(size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200286 LY_CHECK_ERR_RET(!ht->recs, free(ht); LOGMEM(NULL), NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200287
288 return ht;
289}
290
291values_equal_cb
292lyht_set_cb(struct hash_table *ht, values_equal_cb new_val_equal)
293{
294 values_equal_cb prev;
295
296 prev = ht->val_equal;
297 ht->val_equal = new_val_equal;
298 return prev;
299}
300
301void *
302lyht_set_cb_data(struct hash_table *ht, void *new_cb_data)
303{
304 void *prev;
305
306 prev = ht->cb_data;
307 ht->cb_data = new_cb_data;
308 return prev;
309}
310
311struct hash_table *
312lyht_dup(const struct hash_table *orig)
313{
314 struct hash_table *ht;
315
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200316 LY_CHECK_ARG_RET(NULL, orig, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200317
318 ht = lyht_new(orig->size, orig->rec_size - (sizeof(struct ht_rec) - 1), orig->val_equal, orig->cb_data, orig->resize ? 1 : 0);
319 if (!ht) {
320 return NULL;
321 }
322
323 memcpy(ht->recs, orig->recs, orig->used * orig->rec_size);
324 ht->used = orig->used;
325 return ht;
326}
327
328void
329lyht_free(struct hash_table *ht)
330{
331 if (ht) {
332 free(ht->recs);
333 free(ht);
334 }
335}
336
337static LY_ERR
338lyht_resize(struct hash_table *ht, int enlarge)
339{
340 struct ht_rec *rec;
341 unsigned char *old_recs;
342 uint32_t i, old_size;
343 int ret;
344
345 old_recs = ht->recs;
346 old_size = ht->size;
347
348 if (enlarge) {
349 /* double the size */
350 ht->size <<= 1;
351 } else {
352 /* half the size */
353 ht->size >>= 1;
354 }
355
356 ht->recs = calloc(ht->size, ht->rec_size);
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200357 LY_CHECK_ERR_RET(!ht->recs, LOGMEM(NULL); ht->recs = old_recs; ht->size = old_size, LY_EMEM);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200358
359 /* reset used, it will increase again */
360 ht->used = 0;
361
362 /* add all the old records into the new records array */
363 for (i = 0; i < old_size; ++i) {
364 rec = lyht_get_rec(old_recs, ht->rec_size, i);
365 if (rec->hits > 0) {
366 ret = lyht_insert(ht, rec->val, rec->hash, NULL);
367 assert(!ret);
368 (void)ret;
369 }
370 }
371
372 /* final touches */
373 free(old_recs);
374 return LY_SUCCESS;
375}
376
377/* return: 0 - hash found, returned its record,
378 * 1 - hash not found, returned the record where it would be inserted */
379static int
380lyht_find_first(struct hash_table *ht, uint32_t hash, struct ht_rec **rec_p)
381{
382 struct ht_rec *rec;
383 uint32_t i, idx;
384
385 if (rec_p) {
386 *rec_p = NULL;
387 }
388
389 idx = i = hash & (ht->size - 1);
390 rec = lyht_get_rec(ht->recs, ht->rec_size, idx);
391
392 /* skip through overflow and deleted records */
393 while ((rec->hits != 0) && ((rec->hits == -1) || ((rec->hash & (ht->size - 1)) != idx))) {
394 if ((rec->hits == -1) && rec_p && !(*rec_p)) {
395 /* remember this record for return */
396 *rec_p = rec;
397 }
398 i = (i + 1) % ht->size;
399 if (i == idx) {
400 /* we went through all the records (very unlikely, but possible when many records are invalid),
401 * just return not found */
402 assert(!rec_p || *rec_p);
403 return 1;
404 }
405 rec = lyht_get_rec(ht->recs, ht->rec_size, i);
406 }
407 if (rec->hits == 0) {
408 /* we could not find the value */
409 if (rec_p && !*rec_p) {
410 *rec_p = rec;
411 }
412 return 1;
413 }
414
415 /* we have found a record with equal (shortened) hash */
416 if (rec_p) {
417 *rec_p = rec;
418 }
419 return 0;
420}
421
422/**
423 * @brief Search for the next collision.
424 *
425 * @param[in] ht Hash table to search in.
426 * @param[in,out] last Last returned collision record.
427 * @param[in] first First collision record (hits > 1).
428 * @return 0 when hash collision found, \p last points to this next collision,
429 * 1 when hash collision not found, \p last points to the record where it would be inserted.
430 */
431static int
432lyht_find_collision(struct hash_table *ht, struct ht_rec **last, struct ht_rec *first)
433{
434 struct ht_rec *empty = NULL;
435 uint32_t i, idx;
436
437 assert(last && *last);
438
439 idx = (*last)->hash & (ht->size - 1);
440 i = (((unsigned char *)*last) - ht->recs) / ht->rec_size;
441
442 do {
443 i = (i + 1) % ht->size;
444 *last = lyht_get_rec(ht->recs, ht->rec_size, i);
445 if (*last == first) {
446 /* we went through all the records (very unlikely, but possible when many records are invalid),
447 * just return an invalid record */
448 assert(empty);
449 *last = empty;
450 return 1;
451 }
452
453 if (((*last)->hits == -1) && !empty) {
454 empty = *last;
455 }
456 } while (((*last)->hits != 0) && (((*last)->hits == -1) || (((*last)->hash & (ht->size - 1)) != idx)));
457
458 if ((*last)->hits > 0) {
459 /* we found a collision */
460 assert((*last)->hits == 1);
461 return 0;
462 }
463
464 /* no next collision found, return the record where it would be inserted */
465 if (empty) {
466 *last = empty;
467 } /* else (*last)->hits == 0, it is already correct */
468 return 1;
469}
470
471int
472lyht_find(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
473{
474 struct ht_rec *rec, *crec;
475 uint32_t i, c;
476 int r;
477
478 if (lyht_find_first(ht, hash, &rec)) {
479 /* not found */
480 return 1;
481 }
482 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
483 /* even the value matches */
484 if (match_p) {
485 *match_p = rec->val;
486 }
487 return 0;
488 }
489
490 /* some collisions, we need to go through them, too */
491 crec = rec;
492 c = rec->hits;
493 for (i = 1; i < c; ++i) {
494 r = lyht_find_collision(ht, &rec, crec);
495 assert(!r);
496 (void)r;
497
498 /* compare values */
499 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 0, ht->cb_data)) {
500 if (match_p) {
501 *match_p = rec->val;
502 }
503 return 0;
504 }
505 }
506
507 /* not found even in collisions */
508 return 1;
509}
510
511int
512lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
513{
514 struct ht_rec *rec, *crec;
515 uint32_t i, c;
516 int r, found = 0;
517
518 if (lyht_find_first(ht, hash, &rec)) {
519 /* not found, cannot happen */
520 assert(0);
521 }
522
523 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
524 /* previously returned value */
525 found = 1;
526 }
527
528 if (rec->hits == 1) {
529 /* there are no more similar values */
530 assert(rec->hash == hash);
531 assert(found);
532 return 1;
533 }
534
535 /* go through collisions and find next one after the previous one */
536 crec = rec;
537 c = rec->hits;
538 for (i = 1; i < c; ++i) {
539 r = lyht_find_collision(ht, &rec, crec);
540 assert(!r);
541 (void)r;
542
543 if (rec->hash != hash) {
544 /* a normal collision, we are not interested in those */
545 continue;
546 }
547
548 if (found) {
549 /* next value with equal hash, found our value */
550 if (match_p) {
551 *match_p = rec->val;
552 }
553 return 0;
554 }
555
556 if (!ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
557 /* already returned value, skip */
558 continue;
559 }
560
561 /* this one was returned previously, continue looking */
562 found = 1;
563 }
564
565 /* the last equal value was already returned */
566 assert(found);
567 return 1;
568}
569
570LY_ERR
571lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
572 values_equal_cb resize_val_equal, void **match_p)
573{
574 struct ht_rec *rec, *crec = NULL;
575 int32_t i;
576 int r, ret;
577 values_equal_cb old_val_equal;
578
579 if (!lyht_find_first(ht, hash, &rec)) {
580 /* we found matching shortened hash */
581 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
582 /* even the value matches */
583 if (match_p) {
584 *match_p = (void *)&rec->val;
585 }
586 return LY_EEXIST;
587 }
588
589 /* some collisions, we need to go through them, too */
590 crec = rec;
591 for (i = 1; i < crec->hits; ++i) {
592 r = lyht_find_collision(ht, &rec, crec);
593 assert(!r);
594
595 /* compare values */
596 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
597 if (match_p) {
598 *match_p = (void *)&rec->val;
599 }
600 return LY_EEXIST;
601 }
602 }
603
604 /* value not found, get the record where it will be inserted */
605 r = lyht_find_collision(ht, &rec, crec);
606 assert(r);
607 }
608
609 /* insert it into the returned record */
610 assert(rec->hits < 1);
611 rec->hash = hash;
612 rec->hits = 1;
613 memcpy(&rec->val, val_p, ht->rec_size - (sizeof(struct ht_rec) - 1));
614 if (match_p) {
615 *match_p = (void *)&rec->val;
616 }
617
618 if (crec) {
619 /* there was a collision, increase hits */
620 if (crec->hits == INT32_MAX) {
621 LOGINT(NULL);
622 }
623 ++crec->hits;
624 }
625
626 /* check size & enlarge if needed */
627 ret = LY_SUCCESS;
628 ++ht->used;
629 if (ht->resize) {
630 r = (ht->used * 100) / ht->size;
631 if ((ht->resize == 1) && (r >= LYHT_FIRST_SHRINK_PERCENTAGE)) {
632 /* enable shrinking */
633 ht->resize = 2;
634 }
635 if ((ht->resize == 2) && (r >= LYHT_ENLARGE_PERCENTAGE)) {
636 if (resize_val_equal) {
637 old_val_equal = lyht_set_cb(ht, resize_val_equal);
638 }
639
640 /* enlarge */
641 ret = lyht_resize(ht, 1);
642 /* if hash_table was resized, we need to find new matching value */
643 if (ret == LY_SUCCESS && match_p) {
644 lyht_find(ht, val_p, hash, match_p);
645 }
646
647 if (resize_val_equal) {
648 lyht_set_cb(ht, old_val_equal);
649 }
650 }
651 }
652 return ret;
653}
654
Radek Krejci0ae092d2018-09-20 16:43:19 +0200655LY_ERR
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200656lyht_insert(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p)
657{
658 return lyht_insert_with_resize_cb(ht, val_p, hash, NULL, match_p);
659}
660
661LY_ERR
662lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
663{
664 struct ht_rec *rec, *crec;
665 int32_t i;
666 int first_matched = 0, r, ret;
667
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200668 LY_CHECK_ERR_RET(lyht_find_first(ht, hash, &rec), LOGARG(NULL, hash), LY_EINVAL); /* hash not found */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200669
670 if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
671 /* even the value matches */
672 first_matched = 1;
673 }
674
675 /* we always need to go through collisions */
676 crec = rec;
677 for (i = 1; i < crec->hits; ++i) {
678 r = lyht_find_collision(ht, &rec, crec);
679 assert(!r);
680
681 /* compare values */
682 if (!first_matched && (rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
683 break;
684 }
685 }
686
687 if (i < crec->hits) {
688 /* one of collisions matched, reduce collision count, remove the record */
689 assert(!first_matched);
690 --crec->hits;
691 rec->hits = -1;
692 } else if (first_matched) {
693 /* the first record matches */
694 if (crec != rec) {
695 /* ... so put the last collision in its place */
696 rec->hits = crec->hits - 1;
697 memcpy(crec, rec, ht->rec_size);
698 }
699 rec->hits = -1;
700 } else {
701 /* value not found even in collisions */
702 LOGINT(NULL);
703 return LY_EINT;
704 }
705
706 /* check size & shrink if needed */
707 ret = LY_SUCCESS;
708 --ht->used;
709 if (ht->resize == 2) {
710 r = (ht->used * 100) / ht->size;
711 if ((r < LYHT_SHRINK_PERCENTAGE) && (ht->size > LYHT_MIN_SIZE)) {
712 /* shrink */
713 ret = lyht_resize(ht, 0);
714 }
715 }
716
717 return ret;
718}