Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

18.1 Introduction to Radix Trees

Although it is progressively being superseded by the XArray subsystem, the Radix Tree remains widely adopted across various core layers of the Linux system. A radix tree is essentially a space-optimized variant of a trie (prefix tree); it features a minimal memory footprint, though its insertion and deletion logic loops are significantly more complex. To establish a robust understanding of the radix tree, this section first introduces the baseline concepts of a standard trie structure.

A trie (also designated as a prefix tree or word lookup tree) is a specialized tree-shaped data storage architecture engineered to facilitate high-efficiency string retrieval. Its core algorithmic design principle dictates that the common prefix strings shared by multiple keys are multiplexed down the exact same branch of the tree, which minimizes string lookup latencies and effectively balances a "space-for-time" engineering trade-off. The root node of a trie typically resolves as vacant, and each downstream node represents a single character slot. In physical memory, these nodes are arranged in chronological alphabetical sequence. Tracing a contiguous path from the root node down to a targeted node yields the compiled string key corresponding to that entry. Nodes within a trie commonly enclose a boolean flag (such as isEndOfWord) to signal whether the accumulated path up to that checkpoint constitutes a fully finalized word. The absolute branching factor (fan-out) of a trie node is governed by the absolute magnitude of the active character set. For instance, when orchestrating low-case English alphabetic strings, each individual node accommodates a maximum of 26 child pointers.

A trie structure is predominantly deployed to handle keyword lookup sweeps, auto-completion engines (providing real-time predictive vocabulary suggestions based on an input prefix string), spell-checking layers, IP routing table lookups, and word frequency accounting statistics. Figure 18-1 illustrates the topology of a canonical trie structure, wherein the green nodes denote that a path terminated at that checkpoint constitutes a valid finalized string key.

Figure 18-1 Structural Layout of a Trie (Prefix Tree)

The principal operations governing a trie encompass insertion and prefix matching lookups, and its microarchitectural operational efficiency depends strictly on the absolute character length $L$ of the target key, remaining completely decoupled from the total volume of keys archived within the dictionary repository.

A radix tree represents a space-compressed manifestation of a standard trie. While a trie node is structurally constrained to store exactly one character, a radix tree node can encapsulate anywhere from 1 to $2^{r}$ characters $(where r \ge 1)$. Under this design constraint, when an individual node possesses exactly one child, the parent node and child node are compressed and merged together. A tree structure generated by recursively executing this compression pass on all single-child pathways across a trie framework is designated as a radix tree. Figure 18-2 details the topology of a compressed radix tree, where green blocks similarly track the termination boundary of an individual key sequence.

Figure 18-2 Structural Layout of a Radix Tree

Compared to traditional tries, a radix tree features significantly shorter search paths and demands a smaller memory storage footprint. Because an individual step throughout a tree-walk query can process multiple characters simultaneously, it delivers superior lookup velocity; however, the algorithmic implementations for lookups, insertions, and deletions are conspicuously more complex. Within the Linux kernel, radix trees are primarily leveraged for:

  • The Page Cache Subsystem:
    The core kernel utilizes a radix tree to manage the memory pages cached for each individual file object. Within this map, the search key represents the logical page offset within the file, while the value maps to a direct pointer referencing the target physical page descriptor (struct page). This allows the memory subsystem to rapidly locate cached data pages based on a file offset, effectively bypassing costly disk I/O operations.

  • Kernel ID Allocators:
    The IDR mechanism leverages radix trees to map raw integer identifiers (such as process PIDs, file descriptors, and device minor numbers) directly to their corresponding kernel data structure pointers. Similarly, the IDA framework acts as a more lightweight, bitmap-driven allocator built on top of this architecture to orchestrate unallocated ID resources efficiently.

  • Tagging Mechanisms within Memory Management:
    Radix tree nodes natively support specialized metadata tagging capabilities (such as tracking Dirty or Writeback states). This architectural feature allows the kernel to rapidly locate all dirty pages or pages undergoing writeback cycles within a massive cache structure without executing a brute-force traversal across the entire tree, which significantly optimizes page flushing efficiency.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com