Feasibility of a Span class in java

Gavin Ray ray.gavin97 at gmail.com
Mon Nov 7 18:10:14 UTC 2022


I do something like this in my database WIP, here is some similar code to
give an idea:
===================================================================

sealed interface Span<T> {
    val value: T
    fun layout(): MemoryLayout
}

@JvmInline value class IntSpan(override val value: Int) : Span<Int> {
    override fun layout(): MemoryLayout = ValueLayout.JAVA_INT
}

@JvmInline value class LongSpan(override val value: Long) : Span<Long> {
    override fun layout(): MemoryLayout = ValueLayout.JAVA_LONG
}

@JvmInline value class StringSpan(override val value: String) :
Span<String> {
    override fun layout(): MemoryLayout =
MemoryLayout.sequenceLayout(value.length.toLong(), ValueLayout.JAVA_BYTE)
}

// <SNIPPED>

// Convert a MemorySegment to a Span<T> where T is the type of the value
stored in the segment
inline fun <reified T> MemorySegment.toSpan(): Span<T> {
    return when (T::class) {
        Int::class -> IntSpan(this.get(ValueLayout.JAVA_INT, 0)) as Span<T>
        Long::class -> LongSpan(this.get(ValueLayout.JAVA_LONG, 0)) as
Span<T>
        String::class -> StringSpan(this.asSlice(0,
this.byteSize()).getUtf8String(0)) as Span<T>
        Boolean::class -> BooleanSpan(this.get(ValueLayout.JAVA_BOOLEAN,
0)) as Span<T>
        Double::class -> DoubleSpan(this.get(ValueLayout.JAVA_DOUBLE, 0))
as Span<T>
        Float::class -> FloatSpan(this.get(ValueLayout.JAVA_FLOAT, 0)) as
Span<T>
        Short::class -> ShortSpan(this.get(ValueLayout.JAVA_SHORT, 0)) as
Span<T>
        Byte::class -> ByteSpan(this.get(ValueLayout.JAVA_BYTE, 0)) as
Span<T>
        Char::class -> CharSpan(this.get(ValueLayout.JAVA_CHAR, 0)) as
Span<T>
        else -> throw IllegalArgumentException("Unsupported type
${T::class}")
    }
}

// Convert a Span<T> to a MemorySegment where T is the type of the value
stored in the segment
fun <T> Span<T>.toSegment(): MemorySegment {
    val segment = MemorySession.global().allocate(this.layout())
    when (this) {
        is IntSpan -> segment.set(ValueLayout.JAVA_INT, 0, this.value)
        is LongSpan -> segment.set(ValueLayout.JAVA_LONG, 0, this.value)
        is StringSpan -> segment.asSlice(0,
segment.byteSize()).setUtf8String(0, this.value)
        is BooleanSpan -> segment.set(ValueLayout.JAVA_BOOLEAN, 0,
this.value)
        is DoubleSpan -> segment.set(ValueLayout.JAVA_DOUBLE, 0, this.value)
        is FloatSpan -> segment.set(ValueLayout.JAVA_FLOAT, 0, this.value)
        is ShortSpan -> segment.set(ValueLayout.JAVA_SHORT, 0, this.value)
        is ByteSpan -> segment.set(ValueLayout.JAVA_BYTE, 0, this.value)
        is CharSpan -> segment.set(ValueLayout.JAVA_CHAR, 0, this.value)
    }
    return segment
}

On Mon, Nov 7, 2022 at 12:34 PM Remi Forax <forax at univ-mlv.fr> wrote:

> It's on my TODO list :)
>
> You create an interface
>   interface MutableSpan {
>     int value();
>     void value(int value);
>   }
>
> and you can map it to any Panama memory because this is implemented as
> value class with two fields, a memory segment (or whatever the actual name)
> and an index.
> Then you need to see a contiguous space of memory as a List of span
> objects where the value class instances representing the value are created
> on the fly when asked.
>
> Rémi
>
> ------------------------------
>
> *From: *"Red IO" <redio.development at gmail.com>
> *To: *"panama-dev" <panama-dev at openjdk.org>
> *Sent: *Monday, November 7, 2022 5:59:28 PM
> *Subject: *Feasibility of a Span class in java
>
> Following the development and plans for both Panama's memory access api
> and the required changes provided by Valhalla while looking at c#'s
> directions for the last few years I was wondering if writing a class that
> serves as a universal and easy-to-use interface for accessing memory of all
> sorts of "value based" types inspired by c#' s Span struct would be
> beneficial for Java. Of course this assumes the completion of the memory
> api (panama) , the generic unification (Valhalla) and custom value based
> types (Valhalla). The idea is to speed up array and string manipulation by
> using short lived slicing objects (Span). I already wrote a poorly
> implemented slicer for strings and saw incredible performance gains in
> comon operations like substring and split.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20221107/975ce991/attachment-0001.htm>


More information about the panama-dev mailing list