Skip to content

Remove lru_cache from _FanovaTree - #6900

Merged
c-bata merged 2 commits into
optuna:masterfrom
not522:remove-lru-cache-fanova
Oct 22, 2026
Merged

c-bata merged 2 commits into
optuna:masterfrom
not522:remove-lru-cache-fanova

Conversation

@not522

@not522 not522 commented Oct 13, 2026

Copy link
Copy Markdown
Member

Motivation

Same as #6859 and #6864.
Using lru_cache on methods is generally discouraged. (https://docs.python.org/3.15/faq/programming.html#faq-cache-method-calls)

Description of the changes

This PR removes @lru_cache decorators and replaces them with properties. The calculation results are identical to those of the master branch.

Speed Benchmark

This PR slightly improves execution speed.

import itertools
import time

import optuna
from optuna.importance import FanovaImportanceEvaluator


optuna.logging.set_verbosity(optuna.logging.ERROR)

results = []
for n_trials, n_params in itertools.product([100, 1000], [2, 8, 32]):
    sampler = optuna.samplers.TPESampler(seed=0)
    study = optuna.create_study(sampler=sampler)

    def objective(trial):
        val = 0
        for i in range(n_params):
            xi = trial.suggest_float(str(i), -4, 4)
            val += (xi - 2) ** 2
        return val

    study.optimize(objective, n_trials=n_trials)

    for n_trees in [32, 64]:
        start = time.time()
        importances = optuna.importance.get_param_importances(
            study, evaluator=FanovaImportanceEvaluator(n_trees=n_trees, seed=0)
        )
        elapsed_time = time.time() - start
        print(importances)

        results.append((n_trials, n_params, n_trees, elapsed_time))

print("| n_trials | n_params | n_trees | time |")
print("| -------- | -------- | ------- | ---- |")

for n_trials, n_params, n_trees, s in results:
    print(f"| {n_trials} | {n_params} | {n_trees} | {s:.3f}s |")
n_trials n_params n_trees time (master) time (PR)
100 2 32 0.138s 0.124s
100 2 64 0.286s 0.247s
100 8 32 0.159s 0.151s
100 8 64 0.318s 0.302s
100 32 32 0.189s 0.173s
100 32 64 0.365s 0.348s
1000 2 32 4.182s 3.718s
1000 2 64 8.269s 7.412s
1000 8 32 6.894s 6.423s
1000 8 64 13.975s 12.897s
1000 32 32 4.823s 4.565s
1000 32 64 9.456s 8.923s

Checklist

  • I used an LLM while working on this PR.

@not522 not522 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Oct 13, 2026

@y0z y0z left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@not522

not522 commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@c-bata Could you review this PR?

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this PR does not change the results and makes the calculation slightly faster. LGTM.

@c-bata c-bata added this to the v5.1.0 milestone Oct 22, 2026
@c-bata
c-bata merged commit d4bb3b7 into optuna:master Oct 22, 2026
11 checks passed
varunbln added a commit to varunbln/optuna that referenced this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants