Skip to content

Commit 44778c0

Browse files
committed
add VariableWidthViewVectorInlineValueBenchmarks
1 parent 7e5ab4f commit 44778c0

File tree

3 files changed

+270
-12
lines changed

3 files changed

+270
-12
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.arrow.vector;
18+
19+
import org.apache.arrow.memory.ArrowBuf;
20+
import org.apache.arrow.memory.BufferAllocator;
21+
import org.apache.arrow.memory.RootAllocator;
22+
import org.apache.arrow.vector.holders.NullableVarCharHolder;
23+
import org.openjdk.jmh.annotations.Benchmark;
24+
import org.openjdk.jmh.annotations.BenchmarkMode;
25+
import org.openjdk.jmh.annotations.Level;
26+
import org.openjdk.jmh.annotations.Mode;
27+
import org.openjdk.jmh.annotations.OutputTimeUnit;
28+
import org.openjdk.jmh.annotations.Param;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.Setup;
31+
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.TearDown;
33+
import org.openjdk.jmh.runner.Runner;
34+
import org.openjdk.jmh.runner.RunnerException;
35+
import org.openjdk.jmh.runner.options.Options;
36+
import org.openjdk.jmh.runner.options.OptionsBuilder;
37+
38+
import java.util.concurrent.TimeUnit;
39+
40+
/** Benchmarks for {@link BaseVariableWidthVector}. */
41+
@State(Scope.Benchmark)
42+
public class VariableWidthVectorInlineValueBenchmarks {
43+
// checkstyle:off: MissingJavadocMethod
44+
45+
private static final int VECTOR_CAPACITY = 16 * 1024;
46+
47+
private static final int VECTOR_LENGTH = 1024;
48+
49+
private static final int ALLOCATOR_CAPACITY = 1024 * 1024;
50+
51+
private static final byte[] bytes = "InlineValue".getBytes();
52+
private ArrowBuf arrowBuff;
53+
54+
private BufferAllocator allocator;
55+
56+
private VarCharVector vector;
57+
58+
@Param({"1", "2", "10", "40"})
59+
private int step;
60+
61+
/** Setup benchmarks. */
62+
@Setup(Level.Iteration)
63+
public void prepare() {
64+
allocator = new RootAllocator(ALLOCATOR_CAPACITY);
65+
vector = new VarCharVector("vector", allocator);
66+
vector.allocateNew(VECTOR_CAPACITY, VECTOR_LENGTH);
67+
arrowBuff = allocator.buffer(VECTOR_LENGTH);
68+
arrowBuff.setBytes(0, bytes, 0, bytes.length);
69+
}
70+
71+
/** Tear down benchmarks. */
72+
@TearDown(Level.Iteration)
73+
public void tearDown() {
74+
arrowBuff.close();
75+
vector.close();
76+
allocator.close();
77+
}
78+
79+
/**
80+
* Test {@link BaseVariableWidthVector#getValueCapacity()}.
81+
*
82+
* @return useless. To avoid DCE by JIT.
83+
*/
84+
@Benchmark
85+
@BenchmarkMode(Mode.AverageTime)
86+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
87+
public int getValueCapacity() {
88+
return vector.getValueCapacity();
89+
}
90+
91+
@Benchmark
92+
@BenchmarkMode(Mode.AverageTime)
93+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
94+
public int setSafeFromArray() {
95+
for (int i = 0; i < 500; ++i) {
96+
vector.setSafe(i * step, bytes);
97+
}
98+
return vector.getBufferSize();
99+
}
100+
101+
@Benchmark
102+
@BenchmarkMode(Mode.AverageTime)
103+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
104+
public int setSafeFromNullableVarcharHolder() {
105+
NullableVarCharHolder nvch = new NullableVarCharHolder();
106+
nvch.buffer = arrowBuff;
107+
nvch.start = 0;
108+
nvch.end = bytes.length;
109+
for (int i = 0; i < 50; ++i) {
110+
nvch.isSet = 0;
111+
for (int j = 0; j < 9; ++j) {
112+
int idx = 10 * i + j;
113+
vector.setSafe(idx, nvch);
114+
}
115+
nvch.isSet = 1;
116+
vector.setSafe(10 * (i + 1), nvch);
117+
}
118+
return vector.getBufferSize();
119+
}
120+
121+
public static void main(String[] args) throws RunnerException {
122+
Options opt =
123+
new OptionsBuilder()
124+
.include(VariableWidthVectorInlineValueBenchmarks.class.getSimpleName() + ".setSafeFromArray")
125+
.forks(1)
126+
.build();
127+
128+
new Runner(opt).run();
129+
}
130+
// checkstyle:on: MissingJavadocMethod
131+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.arrow.vector;
18+
19+
import org.apache.arrow.memory.ArrowBuf;
20+
import org.apache.arrow.memory.BufferAllocator;
21+
import org.apache.arrow.memory.RootAllocator;
22+
import org.apache.arrow.vector.holders.NullableViewVarCharHolder;
23+
import org.openjdk.jmh.annotations.Benchmark;
24+
import org.openjdk.jmh.annotations.BenchmarkMode;
25+
import org.openjdk.jmh.annotations.Level;
26+
import org.openjdk.jmh.annotations.Mode;
27+
import org.openjdk.jmh.annotations.OutputTimeUnit;
28+
import org.openjdk.jmh.annotations.Param;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.Setup;
31+
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.TearDown;
33+
import org.openjdk.jmh.runner.Runner;
34+
import org.openjdk.jmh.runner.RunnerException;
35+
import org.openjdk.jmh.runner.options.Options;
36+
import org.openjdk.jmh.runner.options.OptionsBuilder;
37+
38+
import java.util.concurrent.TimeUnit;
39+
40+
/** Benchmarks for {@link BaseVariableWidthVector}. */
41+
@State(Scope.Benchmark)
42+
public class VariableWidthViewVectorInlineValueBenchmarks {
43+
// checkstyle:off: MissingJavadocMethod
44+
45+
private static final int VECTOR_CAPACITY = 16 * 1024;
46+
47+
private static final int VECTOR_LENGTH = 1024;
48+
49+
private static final int ALLOCATOR_CAPACITY = 1024 * 1024;
50+
51+
private static byte[] bytes = "InlineValue".getBytes();
52+
private ArrowBuf arrowBuff;
53+
54+
private BufferAllocator allocator;
55+
56+
private ViewVarCharVector vector;
57+
58+
@Param({"1", "2", "10", "40"})
59+
private int step;
60+
61+
/** Setup benchmarks. */
62+
@Setup(Level.Iteration)
63+
public void prepare() {
64+
allocator = new RootAllocator();
65+
vector = new ViewVarCharVector("vector", allocator);
66+
vector.allocateNew(VECTOR_CAPACITY, VECTOR_LENGTH);
67+
arrowBuff = allocator.buffer(VECTOR_LENGTH);
68+
arrowBuff.setBytes(0, bytes, 0, bytes.length);
69+
}
70+
71+
/** Tear down benchmarks. */
72+
@TearDown(Level.Iteration)
73+
public void tearDown() {
74+
arrowBuff.close();
75+
vector.close();
76+
allocator.close();
77+
}
78+
79+
/**
80+
* Test {@link BaseVariableWidthVector#getValueCapacity()}.
81+
*
82+
* @return useless. To avoid DCE by JIT.
83+
*/
84+
@Benchmark
85+
@BenchmarkMode(Mode.AverageTime)
86+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
87+
public int getValueCapacity() {
88+
return vector.getValueCapacity();
89+
}
90+
91+
@Benchmark
92+
@BenchmarkMode(Mode.AverageTime)
93+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
94+
public int setSafeFromArray() {
95+
for (int i = 0; i < 500; ++i) {
96+
vector.setSafe(i * step, bytes);
97+
}
98+
return vector.getBufferSize();
99+
}
100+
101+
@Benchmark
102+
@BenchmarkMode(Mode.AverageTime)
103+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
104+
public int setSafeFromNullableVarcharHolder() {
105+
NullableViewVarCharHolder nvch = new NullableViewVarCharHolder();
106+
nvch.buffer = arrowBuff;
107+
nvch.start = 0;
108+
nvch.end = bytes.length;
109+
for (int i = 0; i < 50; ++i) {
110+
nvch.isSet = 0;
111+
for (int j = 0; j < 9; ++j) {
112+
int idx = 10 * i + j;
113+
vector.setSafe(idx, nvch);
114+
}
115+
nvch.isSet = 1;
116+
vector.setSafe(10 * (i + 1), nvch);
117+
}
118+
return vector.getBufferSize();
119+
}
120+
121+
public static void main(String[] args) throws RunnerException {
122+
Options opt =
123+
new OptionsBuilder()
124+
.include(VariableWidthViewVectorInlineValueBenchmarks.class.getSimpleName() + ".setSafeFromArray")
125+
.forks(1)
126+
.build();
127+
128+
new Runner(opt).run();
129+
}
130+
// checkstyle:on: MissingJavadocMethod
131+
}

vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,13 +1367,11 @@ protected ArrowBuf allocateOrGetLastDataBuffer(int length) {
13671367
protected final void setBytes(int index, byte[] value, int start, int length) {
13681368
int writePosition = index * ELEMENT_SIZE;
13691369

1370-
if (length <= INLINE_SIZE) {
1371-
// to clear the memory segment of view being written to
1372-
// if it has been set
1373-
if (viewBuffer.getLong(writePosition) != 0 || viewBuffer.getLong(writePosition + 8) != 0) {
1374-
viewBuffer.setZero(writePosition, ELEMENT_SIZE);
1375-
}
1370+
// to clear the memory segment of view being written to
1371+
// this is helpful in case of overwriting the value
1372+
viewBuffer.setZero(writePosition, ELEMENT_SIZE);
13761373

1374+
if (length <= INLINE_SIZE) {
13771375
// allocate inline buffer
13781376
// set length
13791377
viewBuffer.setInt(writePosition, length);
@@ -1413,13 +1411,11 @@ protected final void setBytes(int index, byte[] value, int start, int length) {
14131411
protected final void setBytes(int index, ArrowBuf valueBuf, int start, int length) {
14141412
int writePosition = index * ELEMENT_SIZE;
14151413

1416-
if (length <= INLINE_SIZE) {
1417-
// to clear the memory segment of view being written to
1418-
// if it has been set
1419-
if (viewBuffer.getLong(writePosition) != 0 || viewBuffer.getLong(writePosition + 8) != 0) {
1420-
viewBuffer.setZero(writePosition, ELEMENT_SIZE);
1421-
}
1414+
// to clear the memory segment of view being written to
1415+
// this is helpful in case of overwriting the value
1416+
viewBuffer.setZero(writePosition, ELEMENT_SIZE);
14221417

1418+
if (length <= INLINE_SIZE) {
14231419
// allocate inline buffer
14241420
// set length
14251421
viewBuffer.setInt(writePosition, length);

0 commit comments

Comments
 (0)