Uploaded image for project: 'yangtools'
  1. yangtools
  2. YANGTOOLS-177

Optimize generated range checks

    XMLWordPrintable

Details

    • Improvement
    • Status: Resolved
    • Resolution: Done
    • None
    • None
    • None
    • None
    • Operating System: All
      Platform: All

    Description

      Profiling cbench I have encountered performance overhead when constructing FlowCookie class – 20528ms for 105187 invocations. Of that, 15894ms was spent in Range.closed() invocations.

      Looking at the generated code:

      public FlowCookie(BigInteger _value) {
      if (_value != null) {
      boolean isValidRange = false;
      List<Range<BigInteger>> rangeConstraints = new ArrayList<>();
      rangeConstraints.add(Range.closed(new BigInteger("0"), new BigInteger("18446744073709551615")));
      for (Range<BigInteger> r : rangeConstraints) {
      if (r.contains(_value))

      { isValidRange = true; }

      }
      if (!isValidRange)

      { throw new IllegalArgumentException(String.format("Invalid range: %s, expected: %s.", _value, rangeConstraints)); }

      }
      this._value = _value;
      }

      It is rather obvious we can do better:

      1) those range constraints should be lazily generated, but retained as static fields (double-checked loading)

      2) BigInteger("0") should be replaced with BigInteger.ZERO (also ONE and TEN)

      3) For values less that Long.MAX_VALUE, we should use BigInteger.valueOf(long)

      4) rangeConstraints should end up being an ImmutableList

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              lborak@cisco.com Ladislav Borak
              rovarga Robert Varga
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: