Defer Memory Allocation of AHI Hash Tables Until AHI Is Enabled - #729
Open
SongLibing wants to merge 1 commit into
Open
Defer Memory Allocation of AHI Hash Tables Until AHI Is Enabled#729SongLibing wants to merge 1 commit into
SongLibing wants to merge 1 commit into
Conversation
Problem ======= The AHI hash tables are initialized at startup even when AHI is disabled. They are initialized with buf_pool_get_curr_size()/sizeof(void *)/64 cells. Each slot takes 8 bytes. So the total memory they occupy is approximately equal to 1/64 of the buffer pool size. The memory is wasted if AHI is disabled. On large servers, initializing large hash tables slows startup. Solution ======== When AHI is disabled, this commit initializes the AHI hash tables with one cell per partition as requested; ut::find_prime() rounds this up to 103 cells (~824 bytes) per partition. The hash tables are restored to their normal size before AHI is enabled. Repeated SET GLOBAL innodb_adaptive_hash_index = ON statements still invoke the update callback. The enable path therefore checks the actual AHI state while holding btr_search_enabled_mutex and returns if AHI is already enabled. This makes repeated enable operations idempotent and also closes the race between buffer pool resize publishing its completion and its final attempt to re-enable AHI. With the resulting 103 cells per partition, the AHI system is fully initialized. This preserves the invariant that the per-partition hash table objects always exist. During a buffer pool resizing, AHI is temporarily disabled. If AHI was enabled when the resize started, its full-sized hash tables are retained and resized only when necessary, avoiding an unnecessary shrink-and-restore cycle. Tests ===== The regression test restores minimized hash tables, performs repeated primary-key point lookups until AHI allocates a node heap, and then disables AHI again. It verifies query correctness, hash entry allocation, node heap release, and restoration of the minimal hash table size.
SongLibing
force-pushed
the
port_delay_ahi_memory_allocation
branch
from
September 4, 2026 11:23
40eb059 to
86d5222
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The AHI hash tables are initialized at startup even when AHI is disabled. They are initialized with
buf_pool_get_curr_size()/sizeof(void *)/64 cells. Each cell takes 8 bytes. So the total memory they occupy is approximately equal to 1/64 of the buffer pool size. The memory is wasted if AHI is disabled. On large servers, initializing large hash tables slows startup.
Solution
This commit initializes the AHI hash tables with one cell per AHI partition if AHI is disabled. The hash tables are restored to their normal size before AHI is enabled.
With one slot existing, the AHI system is fully initialized. This preserves the invariant that the per-partition hash table objects always exist.
During a buffer pool resizing, AHI is temporarily disabled. If AHI was enabled when the resize started, its full-sized hash tables are retained and resized only when necessary, avoiding an unnecessary shrink-and-restore cycle.
What does this change do?
https://bugs.mysql.com/bug.php?id=112223
Why is it needed?
This patch reduces memory usage and startup time when AHI is disabled. AHI has been disabled by default since 8.4 and AHI is not recommended for production use. Therefore, without this commit, most of the server would wastes an amount of memory approximately equal to 1/64 of buffer pool size.
How was it tested?
mysql-test/scripts/ci/mtr.shpasses locallyContributor checklist
scripts/ci/format.sh)AI assistance
If AI assistance was used, describe the tool(s) and extent of use: Port the patch, added more testcases.
Areas touched
InnoDB