Skip to content

Exception uses as flow control #170

@fbacchella

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions