• Yanqi Lv's avatar
    Shrink dict when deleting dictEntry (#12850) · e2b7932b
    Yanqi Lv authored
    When we insert entries into dict, it may autonomously expand if needed.
    However, when we delete entries from dict, it doesn't shrink to the
    proper size. If there are few entries in a very large dict, it may cause
    huge waste of memory and inefficiency when iterating.
    
    The main keyspace dicts (keys and expires), are shrinked by cron
    (`tryResizeHashTables` calls `htNeedsResize` and `dictResize`),
    And some data structures such as zset and hash also do that (call
    `htNeedsResize`) right after a loop of calls to `dictDelete`,
    But many other dicts are completely missing that call (they can only
    expand).
    
    In this PR, we provide the ability to automatically shrink the dict when
    deleting. The conditions triggering the shrinking is the same as
    `htNeedsResize` used to have. i.e. we expand when we're over 100%
    utilization, and shrink when we're below 10% utilization.
    
    Additionally:
    * Add `dictPauseAutoResize` so that flows that do mass deletions, will
    only trigger shrinkage at the end.
    * Rename `dictResize` to `dictShrinkToFit` (same logic as it used to
    have, but better name describing it)
    * Rename `_dictExpand` to `_dictResize` (same logic as it used to have,
    but better name describing it)
     
    related to discussion
    https://github.com/redis/redis/pull/12819#discussion_r1409293878
    
    
    
    ---------
    Co-authored-by: default avatarOran Agra <oran@redislabs.com>
    Co-authored-by: default avatarzhaozhao.zz <zhaozhao.zz@alibaba-inc.com>
    e2b7932b
t_hash.c 38.6 KB