[llvm] [lit] Use functools.lru_cache for _caching_re_compile (PR #217757)

Prasoon Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 20 13:47:23 PDT 2026


https://github.com/prasoon054 created https://github.com/llvm/llvm-project/pull/217757

lit.util.memoize builds a (args, tuple(kwargs.items())) key and does two dict lookups per call. functools.lru_cache has a faster path that uses a single positional argument directly as the key, so it does one lookup instead of two and skips the tuple construction.

Profiling lit with Scalene and comparing the two implementations on test suites showed the lru_cache version is faster.

>From 0fd7eed34a55b314bb558c158134eff2dcc76400 Mon Sep 17 00:00:00 2001
From: Prasoon Kumar <prasoonkumar054 at gmail.com>
Date: Fri, 21 Aug 2026 01:14:23 +0530
Subject: [PATCH] [lit] Use functools.lru_cache for _caching_re_compile

lit.util.memoize builds a (args, tuple(kwargs.items())) key and does two
dict lookups per call. functools.lru_cache has a faster path that uses a
single positional argument directly as the key, so it does one lookup
instead of two and skips the tuple construction.

Profiling lit with Scalene and comparing the two implementations on
test suites showed the lru_cache version is faster.
---
 llvm/utils/lit/lit/TestRunner.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 30a39745d01b9..9ef299d82c118 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 import enum
+import functools
 import io
 import os
 import pathlib
@@ -1438,7 +1439,7 @@ def regex_escape(s):
     return substitutions
 
 
- at lit.util.memoize  # Intentionally unbounded: see applySubstitutions
+ at functools.lru_cache(maxsize=None)  # Intentionally unbounded: see applySubstitutions
 def _caching_re_compile(r):
     return re.compile(r)
 



More information about the llvm-commits mailing list