[llvm] [lit] Remove unused lit.util.memoize (PR #217772)

Prasoon Kumar via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 03:55:54 PDT 2026


https://github.com/prasoon054 updated https://github.com/llvm/llvm-project/pull/217772

>From 5eff8dc871c52cb5542314661ccb7a06cf915176 Mon Sep 17 00:00:00 2001
From: Prasoon Kumar <prasoonkumar054 at gmail.com>
Date: Fri, 21 Aug 2026 03:17:30 +0530
Subject: [PATCH 1/2] [lit] Use functools.lru_cache instead of lit.util.memoize

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 for
_caching_re_compile.

runCommandCached takes multiple positional and keyword arguments, so it
does not hit lru_cache's single-argument fast path, but the swap removes
one more caller of the hand-rolled memoize implementation. memoize
itself is now unused and will be removed in a follow-up.
---
 llvm/utils/lit/lit/TestRunner.py | 3 ++-
 llvm/utils/lit/lit/util.py       | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

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)
 
diff --git a/llvm/utils/lit/lit/util.py b/llvm/utils/lit/lit/util.py
index 8ab109fe15d45..87207cdb22e01 100644
--- a/llvm/utils/lit/lit/util.py
+++ b/llvm/utils/lit/lit/util.py
@@ -1,4 +1,5 @@
 import errno
+import functools
 import itertools
 import math
 import numbers
@@ -460,7 +461,7 @@ def memoized(*args, **kwargs):
     return memoized
 
 
- at memoize
+ at functools.lru_cache(maxsize=None)
 def runCommandCached(lit_config, cmd, allow_failure, **kwargs):
     """
     Run a command with subprocess.run, with a cache global to this llvm-lit invocation

>From 1a918c8b496d29b55146a420cbd50e665e3899e2 Mon Sep 17 00:00:00 2001
From: Prasoon Kumar <prasoonkumar054 at gmail.com>
Date: Fri, 21 Aug 2026 03:25:03 +0530
Subject: [PATCH 2/2] [lit] Remove unused lit.util.memoize

Both callers of memoize (_caching_re_compile in TestRunner.py,
runCommandCached in util.py) were switched to functools.lru_cache.
memoize has no remaining callers, so this removes it.
---
 llvm/utils/lit/lit/util.py | 15 ---------------
 1 file changed, 15 deletions(-)

diff --git a/llvm/utils/lit/lit/util.py b/llvm/utils/lit/lit/util.py
index 87207cdb22e01..a710da651f784 100644
--- a/llvm/utils/lit/lit/util.py
+++ b/llvm/utils/lit/lit/util.py
@@ -446,21 +446,6 @@ def killProcessAndChildren(pid):
             pass
 
 
-def memoize(f):
-    cache = {}  # Unbounded
-
-    def make_key(args, kwargs):
-        return args, tuple(kwargs.items())
-
-    def memoized(*args, **kwargs):
-        key = make_key(args, kwargs)
-        if key not in cache:
-            cache[key] = f(*args, **kwargs)
-        return cache[key]
-
-    return memoized
-
-
 @functools.lru_cache(maxsize=None)
 def runCommandCached(lit_config, cmd, allow_failure, **kwargs):
     """



More information about the llvm-commits mailing list