Skip to content

Use cached study direction and trial for _CachedStorage's get_best_trial#6270

Merged
y0z merged 1 commit intooptuna:masterfrom
not522:get-best-trial-cache
Sep 29, 2025
Merged

Use cached study direction and trial for _CachedStorage's get_best_trial#6270
y0z merged 1 commit intooptuna:masterfrom
not522:get-best-trial-cache

Conversation

@not522
Copy link
Copy Markdown
Member

@not522 not522 commented Sep 10, 2025

Motivation

Currently, _CachedStorage's get_best_trial just calls the method of the backend storage (RDBStorage). However, performance can be improved by using cached study directions and trials.

Description of the changes

  • Introduce _get_best_trial_id in RDBStorage
  • Use cached study direction and trial for _CachedStorage's get_best_trial

Benchmark

import optuna

def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_float("x", -100, 100)
    y = trial.suggest_int("y", -100, 100)
    return x**2 + y**2

sampler = optuna.samplers.TPESampler(seed=42, multivariate=True, constant_liar=True)
study = optuna.create_study(sampler=sampler, storage="sqlite:///tmp.db")
study.optimize(objective, n_trials=1000)
master PR
12.255s 11.368s

@not522 not522 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Sep 10, 2025
@y0z
Copy link
Copy Markdown
Member

y0z commented Sep 11, 2025

@sawa3030 Could you review this PR?

@y0z y0z assigned sawa3030 and y0z Sep 11, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Sep 18, 2025
)
direction = _directions[0]
trial_id = self._backend._get_best_trial_id(study_id, direction)
return self.get_trial(trial_id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm sorry for keeping you waiting. I have one question: I’m not entirely sure how the trials in the cached storage are updated. I tested locally and confirmed that this PR works as expected, but I’m still unclear why this line works without explicitly calling self._backend.get_trial(trial_id).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

_CachedStorage's get_trial method returns the cached trial data if available, or accesses the remote database if not. This helps reduce unnecessary database access.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much. I have overlooked that point.

Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot removed the stale Exempt from stale bot labeling. label Sep 21, 2025
Copy link
Copy Markdown
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

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

LGTM. In my environment, the speedup was very small, but the code changes are fine and can be merged.

@y0z y0z merged commit 75114c0 into optuna:master Sep 29, 2025
15 checks passed
@y0z y0z added this to the v4.6.0 milestone Sep 29, 2025
@not522 not522 deleted the get-best-trial-cache branch September 29, 2025 09:40
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