Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage VarHandle API #267

Open
qwwdfsad opened this issue Feb 26, 2024 · 2 comments
Open

Leverage VarHandle API #267

qwwdfsad opened this issue Feb 26, 2024 · 2 comments

Comments

@qwwdfsad
Copy link
Member

qwwdfsad commented Feb 26, 2024

Currently, all operations in Buffer operate with byte[] abstraction, meaning that any compound operation incurs overhead from reading and combining individual bytes.

Apart from (potentially non-optimizable) overhead, such an implementation also limits us from providing more efficient primitives -- e.g. SWAR-based indexOf.

It's worth considering VarHandles API for all our bulk operations that operate with anything bigger than byte, ideally isolated through a clear (internal) API boundary via a multi-release jar.

Here is my experiment from qwwdfsad/varhandles that gives a taste of a potential performance difference:

@Benchmark
fun write(): Buffer {
    val buffer = Buffer()
    for (long in data) {
        buffer.writeLong(long)
    }
    return buffer
}

@Benchmark
fun writeVh(): Buffer {
    val buffer = Buffer()
    for (long in data) {
        buffer.writeLongVh(long)
    }
    return buffer
}

@Benchmark
fun readLongArray(): LongArray {
    val copy = buffer.copy()
    for (index in data.indices) {
        data[index] = copy.readLong()
    }
    return data
}

@Benchmark
fun readLongArrayVh(): LongArray {
    val copy = buffer.copy()
    for (index in data.indices) {
        data[index] = copy.readLongVh()
    }
    return data
}

BulkWriteBenchmark.readLongArray    avgt    5  329.162 ± 20.283  us/op
BulkWriteBenchmark.readLongArrayVh  avgt    5  272.699 ±  7.798  us/op
BulkWriteBenchmark.write            avgt    5  360.114 ± 26.572  us/op
BulkWriteBenchmark.writeVh          avgt    5  241.212 ± 75.791  us/op
@fzhinkin
Copy link
Collaborator

@qwwdfsad nice!

@qwwdfsad
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants