Class SpartanContextImpl
java.lang.Object
org.spartan.internal.engine.context.SpartanContextImpl
- All Implemented Interfaces:
org.spartan.api.engine.context.SpartanContext
public class SpartanContextImpl
extends Object
implements org.spartan.api.engine.context.SpartanContext
Implementation of SpartanContext using MemorySegment for shared memory with native code.
The data is stored in a MemorySegment that C++ can directly read/write.
Elements are ordered by index to ensure consistent data order for model training.
Memory Architecture:
dataSegment: Contiguous buffer of doubles - flattened element datacleanSizesSegment: Int array for variable element sizes (dynamic slicing)agentIdSegment: Cached agent identifier for native callsslotCountSegment: Cached variable element count for native calls
Zero-GC Hot Path: The update() method is designed to run at high frequency
without allocating objects. All iteration uses pre-cached arrays indexed by position.
-
Constructor Summary
ConstructorsConstructorDescriptionSpartanContextImpl(@NotNull String identifier, @NotNull Arena arena) SpartanContextImpl(@NotNull String identifier, @NotNull Arena arena, int initialCapacity) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElement(@NotNull org.spartan.api.engine.context.element.SpartanContextElement element, int index) @NotNull ArenagetArena()Internal: Get the Arena used by this context.@NotNull MemorySegmentgetData()@NotNull Collection<org.spartan.api.engine.context.element.SpartanContextElement> getElementByIdentifier(@NotNull String identifier) @NotNull Collection<org.spartan.api.engine.context.element.SpartanContextElement> <SpartanContextElementType extends org.spartan.api.engine.context.element.SpartanContextElement>
@NotNull Collection<SpartanContextElementType> getElementsOfType(@NotNull Class<SpartanContextElementType> type) @NotNull StringintgetSize()doublereadDouble(int index) double[]readDoubles(int startIndex, int length) voidremoveElement(@NotNull org.spartan.api.engine.context.element.SpartanContextElement element) voidremoveElementsByIdentifier(@NotNull String identifier) voidremoveElementsOfType(@NotNull Class<? extends org.spartan.api.engine.context.element.SpartanContextElement> type) voidsyncCleanSizes(long agentIdentifier) Synchronizes the clean sizes buffer with the native engine.voidupdate()Updates all elements and flattens their data into the shared dataSegment.
-
Constructor Details
-
SpartanContextImpl
-
SpartanContextImpl
-
-
Method Details
-
getData
-
getArena
Internal: Get the Arena used by this context. This arena is shared across all models using this context,- Returns:
- the Arena used for memory allocation
-
getSize
public int getSize()- Specified by:
getSizein interfaceorg.spartan.api.engine.context.SpartanContext
-
readDouble
public double readDouble(int index) - Specified by:
readDoublein interfaceorg.spartan.api.engine.context.SpartanContext
-
readDoubles
public double[] readDoubles(int startIndex, int length) - Specified by:
readDoublesin interfaceorg.spartan.api.engine.context.SpartanContext
-
update
public void update()Updates all elements and flattens their data into the shared dataSegment.Algorithm:
- Rebuild caches if structural modifications occurred
- Call update() on each element (computes new data)
- Calculate total valid size from all elements
- Ensure dataSegment has sufficient capacity
- Copy each element's data into dataSegment at correct offset
- Write variable element sizes to cleanSizesSegment
Zero-GC: Uses pre-cached arrays with indexed loops. No iterators, no boxing, no intermediate allocations. Safe to call 20+ times per second per agent.
- Specified by:
updatein interfaceorg.spartan.api.engine.context.SpartanContext
-
syncCleanSizes
public void syncCleanSizes(long agentIdentifier) Synchronizes the clean sizes buffer with the native engine. Call this afterupdate()when the agent is registered with C++.This communicates the valid data sizes for each variable element slot, allowing C++ to process only the meaningful portion of variable-length arrays.
- Parameters:
agentIdentifier- the unique identifier for this agent in the native registry
-
addElement
public void addElement(@NotNull @NotNull org.spartan.api.engine.context.element.SpartanContextElement element, int index) - Specified by:
addElementin interfaceorg.spartan.api.engine.context.SpartanContext
-
removeElement
public void removeElement(@NotNull @NotNull org.spartan.api.engine.context.element.SpartanContextElement element) - Specified by:
removeElementin interfaceorg.spartan.api.engine.context.SpartanContext
-
removeElementsByIdentifier
- Specified by:
removeElementsByIdentifierin interfaceorg.spartan.api.engine.context.SpartanContext
-
removeElementsOfType
public void removeElementsOfType(@NotNull @NotNull Class<? extends org.spartan.api.engine.context.element.SpartanContextElement> type) - Specified by:
removeElementsOfTypein interfaceorg.spartan.api.engine.context.SpartanContext
-
getElements
@NotNull public @NotNull Collection<org.spartan.api.engine.context.element.SpartanContextElement> getElements()- Specified by:
getElementsin interfaceorg.spartan.api.engine.context.SpartanContext
-
getElementsOfType
@NotNull public <SpartanContextElementType extends org.spartan.api.engine.context.element.SpartanContextElement> @NotNull Collection<SpartanContextElementType> getElementsOfType(@NotNull @NotNull Class<SpartanContextElementType> type) - Specified by:
getElementsOfTypein interfaceorg.spartan.api.engine.context.SpartanContext
-
getElementByIdentifier
@NotNull public @NotNull Collection<org.spartan.api.engine.context.element.SpartanContextElement> getElementByIdentifier(@NotNull @NotNull String identifier) - Specified by:
getElementByIdentifierin interfaceorg.spartan.api.engine.context.SpartanContext
-
getIdentifier
- Specified by:
getIdentifierin interfaceorg.spartan.api.engine.context.SpartanContext
-