@@ -920,6 +920,7 @@ def evaluate(ex: str,
920920 casting : str = 'same_kind' ,
921921 sanitize : Optional [bool ] = None ,
922922 _frame_depth : int = 3 ,
923+ disable_cache : bool = False ,
923924 ** kwargs ) -> numpy .ndarray :
924925 r"""
925926 Evaluate a simple array expression element-wise using the virtual machine.
@@ -978,10 +979,41 @@ def evaluate(ex: str,
978979 The calling frame depth. Unless you are a NumExpr developer you should
979980 not set this value.
980981
982+ disable_cache: bool
983+ If set to be `True`, disables the uses of internal expression cache.
984+
985+ By default, NumExpr caches compiled expressions and associated metadata
986+ (via the internal `_numexpr_last`, `_numexpr_cache`, and `_names_cache`
987+ structures). This allows repeated evaluations of the same expression
988+ to skip recompilation, improving performance in workloads where the same
989+ expression is executed multiple times.
990+
991+ However, caching retains references to input and output arrays in order
992+ to support re-evaluation. As a result, this can increase their reference
993+ counts and may prevent them from being garbage-collected immediately.
994+ In situations where precise control over object lifetimes or memory
995+ management is required, set `disable_cache=True` to avoid this behavior.
996+
997+ Default is `False`.
998+
981999 """
9821000 # We could avoid code duplication if we called validate and then re_evaluate
9831001 # here, but we have difficulties with the `sys.getframe(2)` call in
9841002 # `getArguments`
1003+
1004+ # If dissable_cache set to be True, we evaluate the expression here
1005+ # Otherwise we validate and then re_evaluate
1006+ if disable_cache :
1007+ context = getContext (kwargs )
1008+ names , ex_uses_vml = getExprNames (ex , context , sanitize = sanitize )
1009+ arguments = getArguments (names , local_dict , global_dict , _frame_depth = _frame_depth - 1 )
1010+ signature = [(name , getType (arg )) for (name , arg ) in
1011+ zip (names , arguments )]
1012+ compiled_ex = NumExpr (ex , signature , sanitize = sanitize , ** context )
1013+ kwargs = {'out' : out , 'order' : order , 'casting' : casting ,
1014+ 'ex_uses_vml' : ex_uses_vml }
1015+ return compiled_ex (* arguments , ** kwargs )
1016+
9851017 e = validate (ex , local_dict = local_dict , global_dict = global_dict ,
9861018 out = out , order = order , casting = casting ,
9871019 _frame_depth = _frame_depth , sanitize = sanitize , ** kwargs )
0 commit comments