-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
ARROW-5909: [Java] Optimize ByteFunctionHelpers equals & compare logic #4852
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
98 changes: 98 additions & 0 deletions
98
...performance/src/test/java/org/apache/arrow/vector/util/ByteFunctionHelpersBenchmarks.java
This file contains 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,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.arrow.vector.util; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.junit.Test; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import io.netty.buffer.ArrowBuf; | ||
|
||
/** | ||
* Benchmarks for {@link ByteFunctionHelpers}. | ||
*/ | ||
@State(Scope.Benchmark) | ||
public class ByteFunctionHelpersBenchmarks { | ||
|
||
private static final int BUFFER_CAPACITY = 7; | ||
|
||
private static final int ALLOCATOR_CAPACITY = 1024 * 1024; | ||
|
||
private BufferAllocator allocator; | ||
|
||
private ArrowBuf buffer1; | ||
|
||
private ArrowBuf buffer2; | ||
|
||
/** | ||
* Setup benchmarks. | ||
*/ | ||
@Setup | ||
public void prepare() { | ||
allocator = new RootAllocator(ALLOCATOR_CAPACITY); | ||
buffer1 = allocator.buffer(BUFFER_CAPACITY); | ||
buffer2 = allocator.buffer(BUFFER_CAPACITY); | ||
|
||
for (int i = 0; i < BUFFER_CAPACITY; i++) { | ||
buffer1.setByte(i, i); | ||
buffer2.setByte(i, i); | ||
} | ||
} | ||
|
||
/** | ||
* Tear down benchmarks. | ||
*/ | ||
@TearDown | ||
public void tearDown() { | ||
buffer1.close(); | ||
buffer2.close(); | ||
allocator.close(); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public void equals() { | ||
ByteFunctionHelpers.equal(buffer1, 0, BUFFER_CAPACITY - 1, buffer2, 0, BUFFER_CAPACITY - 1); | ||
|
||
} | ||
|
||
@Test | ||
public void evaluate() throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(ByteFunctionHelpersBenchmarks.class.getSimpleName()) | ||
.forks(1) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
This file contains 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
114 changes: 114 additions & 0 deletions
114
java/vector/src/test/java/org/apache/arrow/vector/util/TestByteFunctionHelpers.java
This file contains 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,114 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.arrow.vector.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import io.netty.buffer.ArrowBuf; | ||
|
||
public class TestByteFunctionHelpers { | ||
|
||
private BufferAllocator allocator; | ||
|
||
private static final int SIZE = 100; | ||
|
||
@Before | ||
public void init() { | ||
allocator = new RootAllocator(Long.MAX_VALUE); | ||
|
||
} | ||
|
||
@After | ||
public void terminate() throws Exception { | ||
allocator.close(); | ||
} | ||
|
||
@Test | ||
public void testEquals() { | ||
ArrowBuf buffer1 = allocator.buffer(SIZE); | ||
ArrowBuf buffer2 = allocator.buffer(SIZE); | ||
|
||
for (int i = 0; i < SIZE; i++) { | ||
buffer1.setByte(i, i); | ||
buffer2.setByte(i, i); | ||
} | ||
|
||
//test three cases, length>8, length>3, length<3 | ||
|
||
assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, | ||
buffer2, 0, SIZE - 1)); | ||
assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 6, | ||
buffer2, 0, 6)); | ||
assertEquals(1, ByteFunctionHelpers.equal(buffer1, 0, 2, | ||
buffer2, 0, 2)); | ||
|
||
//change value at index1 | ||
buffer1.setByte(1, 10); | ||
|
||
assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, SIZE - 1, | ||
buffer2, 0, SIZE - 1)); | ||
assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 6, | ||
buffer2, 0, 6)); | ||
assertEquals(0, ByteFunctionHelpers.equal(buffer1, 0, 2, | ||
buffer2, 0, 2)); | ||
|
||
buffer1.close(); | ||
buffer2.close(); | ||
|
||
} | ||
|
||
@Test | ||
public void testCompare() { | ||
ArrowBuf buffer1 = allocator.buffer(SIZE); | ||
ArrowBuf buffer2 = allocator.buffer(SIZE); | ||
|
||
for (int i = 0; i < SIZE; i++) { | ||
buffer1.setByte(i, i); | ||
buffer2.setByte(i, i); | ||
} | ||
|
||
//test three cases, length>8, length>3, length<3 | ||
|
||
assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, | ||
buffer2, 0, SIZE - 1)); | ||
assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 6, | ||
buffer2, 0, 6)); | ||
assertEquals(0, ByteFunctionHelpers.compare(buffer1, 0, 2, | ||
buffer2, 0, 2)); | ||
|
||
//change value at index 1 | ||
buffer1.setByte(1, 0); | ||
|
||
assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, SIZE - 1, | ||
buffer2, 0, SIZE - 1)); | ||
assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 6, | ||
buffer2, 0, 6)); | ||
assertEquals(-1, ByteFunctionHelpers.compare(buffer1, 0, 2, | ||
buffer2, 0, 2)); | ||
|
||
buffer1.close(); | ||
buffer2.close(); | ||
|
||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add another test case for the + case.
In an ideal world we would explicitly test the three cases for different lengths >8, >3 and <=3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, fixed.