Skip to content

Exception uses as flow control #170

Description

@fbacchella

I’m using cache2k 2.0.0.Final.

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

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

So it's code were exception are used as flow control. But it's usually a bad idea, see https://shipilev.net/blog/2014/exceptional-performance/ for a deep analysis of that.

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

flamegraph

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

The code that generate this stack can be found at NettyNameResolver.java

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.

Activity

  1. added this to the v2.2 milestone on Jan 26, 2021
  2. self-assigned this
    on Jan 26, 2021
  3. cruftex commented on Jan 26, 2021

    @cruftex
    Member

    @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.

  4. cruftex commented on Mar 26, 2021

    @cruftex
    Member

    I implemented your suggestion and released an alpha update for verification. Thanks for reporting!
    https://github.com/cache2k/cache2k/releases/tag/v2.1.2.Alpha

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions