diff --git a/lib/bb/data_smart.py b/lib/bb/data_smart.py
index 8aa7c1f99..cc87a665d 100644
--- a/lib/bb/data_smart.py
+++ b/lib/bb/data_smart.py
@@ -18,6 +18,7 @@ BitBake build tools.
 
 import builtins
 import copy
+import functools
 import re
 import sys
 from collections.abc import MutableMapping
@@ -30,6 +31,10 @@ from bb.COW  import COWDictBase
 
 logger = logging.getLogger("BitBake.Data")
 
+@functools.lru_cache(maxsize=8192)
+def compile_python_expression(code, varname):
+    return compile(code, varname, "eval")
+
 __setvar_keyword__ = [":append", ":prepend", ":remove"]
 __setvar_regexp__ = re.compile(r'(?P<base>.*?)(?P<keyword>:append|:prepend|:remove)(:(?P<add>[^A-Z]*))?$')
 __expand_var_regexp__ = re.compile(r"\${[a-zA-Z0-9\-_+./~:]+}")
@@ -133,7 +138,7 @@ class VariableParse:
             varname = 'Var <%s>' % self.varname
         else:
             varname = '<expansion>'
-        codeobj = compile(code.strip(), varname, "eval")
+        codeobj = compile_python_expression(code.strip(), varname)
 
         parser = bb.codeparser.PythonParser(self.varname, logger)
         parser.parse_python(code)
