Class AdaptingIntegerArray


  • final class AdaptingIntegerArray
    extends java.lang.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 < 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void clear()
      Zeroes out all counts in this array.
      (package private) AdaptingIntegerArray copy()
      Returns a deep-copy of this array, preserving cell size.
      (package private) long get​(int index)
      Grab the value stored at index.
      (package private) void increment​(int idx, long count)
      Add count to the value stored at index.
      (package private) int length()
      Return the length of this integer array.
      private void resizeToInt()
      Convert from short => int backing array.
      private void resizeToLong()
      convert from int => long backing array.
      private void resizeToShort()
      Convert from byte => short backing array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • byteBacking

        @Nullable
        private byte[] byteBacking
      • shortBacking

        @Nullable
        private short[] shortBacking
      • intBacking

        @Nullable
        private int[] intBacking
      • longBacking

        @Nullable
        private long[] longBacking
    • Constructor Detail

      • 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 Detail

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