Skip to content

[perf]: replace eval() with direct comparison in check_param#499

Open
vTusharr wants to merge 1 commit intodswah:mainfrom
vTusharr:main
Open

[perf]: replace eval() with direct comparison in check_param#499
vTusharr wants to merge 1 commit intodswah:mainfrom
vTusharr:main

Conversation

@vTusharr
Copy link
Copy Markdown

@vTusharr vTusharr commented Mar 6, 2026

Replace eval('np.' + repr(param_dt) + constraint) with a lookup dict of direct numpy comparison operators in check_param(). This eliminates many calls to eval() and repr() per run ; Falls back to eval() for unknown constraint strings to maintain backwards compatibility.

Summary

check_param() previously built ->msg = param_name + f" = {repr(param)}"
at the start of the function. This forced to compute the string and run repr(param) on every call, even when no error occurred.

Change

The message construction is moved into a nested helper:

def _build_msg():
    return param_name + f" = {repr(param)}"

Error paths now call it only when needed:

raise TypeError(_build_msg())

###why?

This uses lazy evaluation while keeping the code DRY . Instead of computing the message on every call or duplicating the string-building code across multiple error sites, it is built only when an exception is raised.

###now
Avoids unnecessary repr(param) and string formatting in the common case where parameters are valid, removing overhead from the hot path and producing a significant speedup.

py-spy flame graph

before

flamegraph_pyspy_before

after

flamegraph_pyspy_after

Profiling methodology

  • Tools: cProfile, py-spy (top + record), flameprof
  • profiling across 12 operation categories (fit, gridsearch,
    predict, intervals, sampling, partial dependence, constraints, etc.)

tests 162 passed, 1 skipped, 62 warnings in 30.35s

@vTusharr vTusharr force-pushed the main branch 3 times, most recently from 6e787e3 to cc75ebc Compare March 13, 2026 16:21
- Replace eval() with direct comparison in check_param to avoid evaluation overhead
- Defer exception message building to avoid costly repr() and string formatting overhead

Signed-off-by: Tushar Verma <tusharVermaiota@proton.me>
@dswah
Copy link
Copy Markdown
Owner

dswah commented Apr 17, 2026

@vTusharr Cool thanks for the PR.
Can you help me understand if this change improved performance?
I see the flame graphs, but i cant tell if it is faster with your contribution.

@vTusharr
Copy link
Copy Markdown
Author

@dswah Yeah , was kinda busy with exams & all , Old code was calling eval() and building error message strings on all calls, even when validation passed , during a gridsearch that's a lot of calls , so i made it so that only when it fails the string is constructed and replaced eval("np." + repr(param_dt) + constraint) with a dict of pre-built lambdas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants