From 6e312af8c25ccedd1dcd2583358be038ab4875b0 Mon Sep 17 00:00:00 2001 From: Shuwen Wang <47200617+alphabetc1@users.noreply.github.com> Date: Mon, 7 Sep 2026 11:10:32 +0800 Subject: [PATCH] fix: collect prefix hash values iteratively (#38204) --- python/sglang/srt/mem_cache/mamba_radix_cache.py | 8 +++++--- python/sglang/srt/mem_cache/radix_cache.py | 9 +++++---- .../srt/mem_cache/unified_cache/unified_tree_core.py | 9 +++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/python/sglang/srt/mem_cache/mamba_radix_cache.py b/python/sglang/srt/mem_cache/mamba_radix_cache.py index ffbc1ceb0..e98bdec52 100644 --- a/python/sglang/srt/mem_cache/mamba_radix_cache.py +++ b/python/sglang/srt/mem_cache/mamba_radix_cache.py @@ -163,9 +163,11 @@ class TreeNode: return self.hash_value[-1] def get_prefix_hash_values(self, node: TreeNode) -> List[str]: - if node is None or node.hash_value is None: - return [] - return node.get_prefix_hash_values(node.parent) + node.hash_value + chunks = [] + while node is not None and node.hash_value is not None: + chunks.append(node.hash_value) + node = node.parent + return [value for chunk in reversed(chunks) for value in chunk] def __lt__(self, other: TreeNode): return self.last_access_time < other.last_access_time diff --git a/python/sglang/srt/mem_cache/radix_cache.py b/python/sglang/srt/mem_cache/radix_cache.py index 10c527331..f33812cc7 100644 --- a/python/sglang/srt/mem_cache/radix_cache.py +++ b/python/sglang/srt/mem_cache/radix_cache.py @@ -311,10 +311,11 @@ class TreeNode: return self.hash_value[-1] def get_prefix_hash_values(self, node: TreeNode) -> List[str]: - if node is None or node.hash_value is None: - return [] - - return node.get_prefix_hash_values(node.parent) + node.hash_value + chunks = [] + while node is not None and node.hash_value is not None: + chunks.append(node.hash_value) + node = node.parent + return [value for chunk in reversed(chunks) for value in chunk] def __lt__(self, other: TreeNode): return self.last_access_time < other.last_access_time diff --git a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py index 072a4abfc..14d909b88 100644 --- a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py +++ b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py @@ -165,10 +165,11 @@ class UnifiedTreeNode: return self.hash_value[-1] def get_prefix_hash_values(self, node: UnifiedTreeNode) -> list[str]: - if node is None or node.hash_value is None: - return [] - - return node.get_prefix_hash_values(node.parent) + node.hash_value + chunks = [] + while node is not None and node.hash_value is not None: + chunks.append(node.hash_value) + node = node.parent + return [value for chunk in reversed(chunks) for value in chunk] class UnifiedLRUList: