-
Notifications
You must be signed in to change notification settings - Fork 153
Use bulk writes instead of per-element writes in vector serialization #681
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
Open
r-devulap
wants to merge
7
commits into
main
Choose a base branch
from
bulk-writes-memorySegment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−4
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
943d4bd
Use bulk writes instead of per-element writes in vector serialization
r-devulap 13bde4a
Adding essential tests.
tlwillke df2aed3
Use .asByteBuffer() instead of .heapBase().get() for MemorySegmentByt…
r-devulap cd46c3a
Move MemorySegmentVectorProviderTest to jvector-native module
r-devulap 3c0057b
Add license
r-devulap 29e3ef9
Add microbenchmark to measure MemorySegmentVectorProvider's writeFloa…
r-devulap c8ee4bb
Moved teset to the vector package.
tlwillke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...h/src/main/java/io/github/jbellis/jvector/bench/MemorySegmentVectorProviderBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.bench; | ||
|
|
||
| import io.github.jbellis.jvector.vector.MemorySegmentVectorProvider; | ||
| import io.github.jbellis.jvector.vector.types.VectorFloat; | ||
| import io.github.jbellis.jvector.vector.types.ByteSequence; | ||
| import io.github.jbellis.jvector.disk.IndexWriter; | ||
| import org.openjdk.jmh.annotations.*; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.DataOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 5) | ||
| @Fork(2) | ||
| @State(Scope.Benchmark) | ||
| public class MemorySegmentVectorProviderBenchmark { | ||
|
|
||
| @Param({"512","1024","1536"}) | ||
| public int length; | ||
|
|
||
| private MemorySegmentVectorProvider provider; | ||
| private VectorFloat<?> fvector; | ||
| private ByteSequence<?> bvector; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| provider = new MemorySegmentVectorProvider(); | ||
| float[] fdata = new float[length]; | ||
| byte[] bdata = new byte[length]; | ||
| for (int i = 0; i < length; i++) { | ||
| fdata[i] = (float) i; | ||
| bdata[i] = (byte) i; | ||
| } | ||
| fvector = provider.createFloatVector(fdata); | ||
| bvector = provider.createByteSequence(bdata); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void writeFloatVector() throws IOException { | ||
| try (MemoryIndexWriter w = new MemoryIndexWriter(length * 4)) { | ||
| provider.writeFloatVector(w, fvector); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void writeByteVector() throws IOException { | ||
| try (MemoryIndexWriter w = new MemoryIndexWriter(length * 4)) { | ||
| provider.writeByteSequence(w, bvector); | ||
| } | ||
| } | ||
|
|
||
| static final class MemoryIndexWriter implements IndexWriter { | ||
| private final java.io.ByteArrayOutputStream bos; | ||
| private final java.io.DataOutputStream out; | ||
|
|
||
| MemoryIndexWriter(int capacity) { | ||
| this.bos = new java.io.ByteArrayOutputStream(capacity); | ||
| this.out = new java.io.DataOutputStream(bos); | ||
| } | ||
|
|
||
| byte[] toByteArray() { return bos.toByteArray(); } | ||
|
|
||
| @Override public long position() { return bos.size(); } | ||
| @Override public void close() throws IOException { out.close(); } | ||
|
|
||
| @Override public void write(int b) throws IOException { out.write(b); } | ||
| @Override public void write(byte[] b) throws IOException { out.write(b); } | ||
| @Override public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); } | ||
| @Override public void writeFloat(float v) throws IOException { out.writeFloat(v); } | ||
|
|
||
| @Override public void writeBoolean(boolean v) throws IOException { out.writeBoolean(v); } | ||
| @Override public void writeByte(int v) throws IOException { out.writeByte(v); } | ||
| @Override public void writeShort(int v) throws IOException { out.writeShort(v); } | ||
| @Override public void writeChar(int v) throws IOException { out.writeChar(v); } | ||
| @Override public void writeInt(int v) throws IOException { out.writeInt(v); } | ||
| @Override public void writeLong(long v) throws IOException { out.writeLong(v); } | ||
| @Override public void writeDouble(double v) throws IOException { out.writeDouble(v); } | ||
| @Override public void writeBytes(String s) throws IOException { out.writeBytes(s); } | ||
| @Override public void writeChars(String s) throws IOException { out.writeChars(s); } | ||
| @Override public void writeUTF(String s) throws IOException { out.writeUTF(s); } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...ative/src/test/java/io/github/jbellis/jvector/vector/MemorySegmentVectorProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.vector; | ||
|
|
||
| import io.github.jbellis.jvector.disk.IndexWriter; | ||
| import io.github.jbellis.jvector.vector.types.ByteSequence; | ||
|
|
||
| import java.io.IOException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class MemorySegmentVectorProviderTest { | ||
|
|
||
| @Test | ||
| void testWriteByteSequenceSlice() throws IOException { | ||
| MemorySegmentVectorProvider provider = new MemorySegmentVectorProvider(); | ||
|
|
||
| byte[] originalBytes = {10, 20, 30, 40, 50}; | ||
| ByteSequence<?> original = provider.createByteSequence(originalBytes); | ||
|
|
||
| ByteSequence<?> slice = original.slice(2, 3); | ||
|
|
||
| MockIndexWriter dummyWriter = new MockIndexWriter(); | ||
|
|
||
| // Proves sliced writes function perfectly | ||
| provider.writeByteSequence(dummyWriter, slice); | ||
|
|
||
| byte[] expected = {30, 40, 50}; | ||
| org.junit.jupiter.api.Assertions.assertArrayEquals(expected, dummyWriter.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWriteByteSequenceFull() throws IOException { | ||
| MemorySegmentVectorProvider provider = new MemorySegmentVectorProvider(); | ||
|
|
||
| byte[] expectedBytes = {1, 2, 3, 4, 5}; | ||
| ByteSequence<?> sequence = provider.createByteSequence(expectedBytes); | ||
|
|
||
| MockIndexWriter dummyWriter = new MockIndexWriter(); | ||
|
|
||
| // Proves standard, non-sliced writes function perfectly | ||
| provider.writeByteSequence(dummyWriter, sequence); | ||
|
|
||
| org.junit.jupiter.api.Assertions.assertArrayEquals(expectedBytes, dummyWriter.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWriteByteSequenceZeroLength() throws IOException { | ||
| MemorySegmentVectorProvider provider = new MemorySegmentVectorProvider(); | ||
|
|
||
| byte[] originalBytes = {10, 20, 30}; | ||
| ByteSequence<?> original = provider.createByteSequence(originalBytes); | ||
|
|
||
| // Create a logical empty slice | ||
| ByteSequence<?> emptySlice = original.slice(1, 0); | ||
|
|
||
| MockIndexWriter dummyWriter = new MockIndexWriter(); | ||
|
|
||
| // Proves edge cases don't throw IndexOutOfBoundsException | ||
| provider.writeByteSequence(dummyWriter, emptySlice); | ||
|
|
||
| org.junit.jupiter.api.Assertions.assertArrayEquals(new byte[0], dummyWriter.toByteArray()); | ||
| } | ||
|
|
||
| /** | ||
| * A lightweight mock to capture IndexWriter output without boilerplate. | ||
| */ | ||
| private static class MockIndexWriter implements IndexWriter { | ||
| private final java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); | ||
| private final java.io.DataOutputStream out = new java.io.DataOutputStream(bos); | ||
|
|
||
| public byte[] toByteArray() { return bos.toByteArray(); } | ||
|
|
||
| @Override public long position() { return bos.size(); } | ||
| @Override public void close() throws IOException { out.close(); } | ||
|
|
||
| // DataOutput delegation | ||
| @Override public void write(int b) throws IOException { out.write(b); } | ||
| @Override public void write(byte[] b) throws IOException { out.write(b); } | ||
| @Override public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); } | ||
| @Override public void writeBoolean(boolean v) throws IOException { out.writeBoolean(v); } | ||
| @Override public void writeByte(int v) throws IOException { out.writeByte(v); } | ||
| @Override public void writeShort(int v) throws IOException { out.writeShort(v); } | ||
| @Override public void writeChar(int v) throws IOException { out.writeChar(v); } | ||
| @Override public void writeInt(int v) throws IOException { out.writeInt(v); } | ||
| @Override public void writeLong(long v) throws IOException { out.writeLong(v); } | ||
| @Override public void writeFloat(float v) throws IOException { out.writeFloat(v); } | ||
| @Override public void writeDouble(double v) throws IOException { out.writeDouble(v); } | ||
| @Override public void writeBytes(String s) throws IOException { out.writeBytes(s); } | ||
| @Override public void writeChars(String s) throws IOException { out.writeChars(s); } | ||
| @Override public void writeUTF(String s) throws IOException { out.writeUTF(s); } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.