SimpleJwkFilter throws excessive amount of ClassCastException

Issue #143 closed
Gustav Åkesson created an issue

The getCrv and getThumbs methods in SimpleJwkFilter uses ClassCastException as a control-flow mechanism, which impacts performance (even though there is no semantic fault here).

This overhead could easily be mitigated by instead checking instanceof of the object before performing the casting, and simply return EMPTY if that condition evaluates false.

Comments (5)

  1. Gustav Åkesson reporter

    Exceptions are typically very costly and we see this in our JMC JFR profilings (otherwise I would not have found it).

    I tried to reproduce in JMH benchmark but in that the JIT is able to optimize the try/catch completely through inlining, but this does not seem to happen in our production.

    Besides, doesn’t the code look nicer than having a try/catch?

    String getCrv()
    {
        if (!(jwk instanceof EllipticCurveJsonWebKey))
        {
            return null;
        }
        return ((EllipticCurveJsonWebKey) jwk).getCurveName();
    }
    
  2. Log in to comment