Class AdaptingIntegerArray

java.lang.Object
io.opentelemetry.sdk.metrics.internal.aggregator.AdaptingIntegerArray

final class AdaptingIntegerArray extends Object
An integer array that automatically expands its memory consumption (via copy/allocation) when reaching limits. This assumes counts remain low, to lower memory overhead.

This class is NOT thread-safe. It is expected to be behind a synchronized incrementer.

Instances start by attempting to store one-byte per-cell in the integer array. As values grow, this will automatically instantiate the next-size integer array (byte => short => int => long) and copy over values into the larger array. This class expects most usage to remain within the byte boundary (e.g. cell values invalid input: '<' 128).

This class lives in the (very) hot path of metric recording. As such, we do "fun" things, like switch on markers and assume non-null based on presence of the markers, as such we suppress NullAway as it can't understand/express this level of guarantee.

Implementations MUST preserve the following:

  • If cellSize == BYTE then byteBacking is not null
  • If cellSize == SHORT then shortBacking is not null
  • If cellSize == INT then intBacking is not null
  • If cellSize == LONG then longBacking is not null
  • Field Details

  • Constructor Details

    • AdaptingIntegerArray

      AdaptingIntegerArray(int size)
      Construct an adapting integer array of a given size.
    • AdaptingIntegerArray

      private AdaptingIntegerArray(AdaptingIntegerArray toCopy)
      Creates deep copy of another adapting integer array.
  • Method Details

    • copy

      Returns a deep-copy of this array, preserving cell size.
    • increment

      void increment(int idx, long count)
      Add count to the value stored at index.
    • get

      long get(int index)
      Grab the value stored at index.
    • length

      int length()
      Return the length of this integer array.
    • clear

      void clear()
      Zeroes out all counts in this array.
    • resizeToShort

      private void resizeToShort()
      Convert from byte => short backing array.
    • resizeToInt

      private void resizeToInt()
      Convert from short => int backing array.
    • resizeToLong

      private void resizeToLong()
      convert from int => long backing array.