Skip to content

Give option to reuse byte[] or add apis supporting ByteBuffer #3099

@vachagan-balayan-bullish

Description

It is absurd to me that people to this day do the exact same mistakes every serialization framework did for 20 years. We know at this point 2 biggest allocation sources in almost every application are serialization/deserialization and loggging. For the love of god implement some basic mechanisms to avoid these. You already have done the hardest part.

1) easiest laziest way, add optional argument so i can choose where you allocate byte[] from if you absolutely have to allocate those.

instead of
public inline fun <reified T> kotlinx.serialization.BinaryFormat.encodeToByteArray(value: T): kotlin.ByteArray { /* compiled code */ }

add an optional supplier (or a separate method) that allows me to control how those allocations happen, maybe i have a way to reuse byte arrays in a thread safe way.

supplier : (Int)-> ByteArray = { ByteArray(it) }

something like this

inline fun <reified T> kotlinx.serialization.BinaryFormat.encodeToByteArray(
    value: T,
    supplier : (Int)-> ByteArray = { ByteArray(it) }
): Int

inline fun <reified T> kotlinx.serialization.BinaryFormat.decodeFromByteArray(bytes: ByteArray, from: Int, to: Int): T

2) or give an option for the user to supply a buffer


public inline fun <reified T> kotlinx.serialization.BinaryFormat.encodeTo(value: T, buffer: ByteBuffer) {/* compiled code */ }

public inline fun <reified T> kotlinx.serialization.BinaryFormat.decodeFrom(buffer: ByteBuffer): T { /* compiled code */ }
public inline fun <reified T> kotlinx.serialization.BinaryFormat.decodeFrom(buffer: ByteBuffer, supplier: () -> T): T { /* compiled code */ }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions