Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception uses as flow control #170

fbacchella opened this issue Jan 25, 2021 · 2 comments

Exception uses as flow control #170

fbacchella opened this issue Jan 25, 2021 · 2 comments


Copy link

I’m using cache2k 2.0.0.Final.

In org.cache2k.processor.EntryProcessor, I see the code:

        try {
          R result = processor.process(mutableEntry);
        } catch (WantsDataRestartException rs) {
        } catch (NeedsLockRestartException ex) {
          needsLock = true;
        } catch (Throwable t) {
          c.failure(new EntryProcessingException(t));

So it's code were exception are used as flow control. But it's usually a bad idea, see for a deep analysis of that.

For example, when generating flamegraph for some of my code, I get the following result:


Most of the time is spent filling in the stack trace.

The code that generate this stack can be found at

Performance could be increased I think if both WantsDataRestartException and NeedsLockRestartException (and perhaps other) were static and not saving the stack.

This constructor can be used for that:

protected Exception(String message,
                    Throwable cause,
                    boolean enableSuppression,
                    boolean writableStackTrace)

If writableStackTrace is set to false, the stack trace is not saved.

@cruftex cruftex added this to the v2.2 milestone Jan 26, 2021
@cruftex cruftex self-assigned this Jan 26, 2021
Copy link

cruftex commented Jan 26, 2021

@fbacchella thanks for the analysis. That looks like a quick win. I will put an improvement in the next release.

I also looked at your code. Using JCache you need to do per entry TTL by yourself. Cache2k would support this directly and do an efficient expiry via timer wheel and scheduler.

Copy link

cruftex commented Mar 26, 2021

I implemented your suggestion and released an alpha update for verification. Thanks for reporting!

@cruftex cruftex closed this as completed Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants