Class BytecodeSupport.ConstantsBuffer

java.lang.Object
com.oracle.truffle.api.bytecode.BytecodeSupport.ConstantsBuffer
Enclosing class:
BytecodeSupport

public static final class BytecodeSupport.ConstantsBuffer extends Object
Specialized reusable and allocation-free buffer for constants. The first 8 literals are stored linearly in constants[]. Once that threshold is crossed, all literals are also entered into an open-address hash table (keys[]/values[]) so that subsequent look-ups run in amortised O(1) without allocating any Map.Entry objects. After the client has finished emitting a method it must call materialize() to obtain an immutable snapshot of the constant pool for that method. The buffer can then be filled again without further allocations. Call clear() at the end of using this buffer if you want to release the object references or down-size the backing arrays. Not thread-safe. Intended for use in generated code. Do not use directly.
Since:
25.0
  • Constructor Details

    • ConstantsBuffer

      public ConstantsBuffer()
  • Method Details

    • add

      public int add(Object c)
      Inserts constant (non-null) and returns its pool index. If an equal constant is already present, its existing index is returned.
      Throws:
      NullPointerException - if constant is null
      Since:
      25.0
    • addNull

      public int addNull()
      Inserts a constant without value and returns its index. The added null values are intended to be later patched after materialization. The client must ensure that the reserved null slots do not contain duplicates already contained in the materialized constants array.
      Since:
      25.0
    • materialize

      public Object[] materialize()
      Copies the internal constants[] array and returns it. All auxiliary data structures are reset so that the buffer is ready for the next use. Note that this implementation deliberately does not clear all references contained in the buffer. If all references should be cleared, then clear() should be called after materialization.
      Returns:
      a freshly allocated array of exactly size() elements, never null.
      Since:
      25.0
    • create

      public Object[] create()
    • clear

      public void clear()
      Clears all object references held in this buffer. If the pool had grown beyond 512 constants its internal arrays are replaced by new arrays of that bounded size; otherwise the existing arrays are reused.

      The buffer must be materialised before it can be cleared.

      Throws:
      IllegalStateException - if materialize() has not been called since the last insertion
      Since:
      25.0