2121import java .util .Arrays ;
2222import java .util .Random ;
2323import java .util .concurrent .TimeUnit ;
24- import org .apache .lucene .codecs .lucene99 .GroupVIntReader ;
25- import org .apache .lucene .codecs .lucene99 .GroupVIntWriter ;
2624import org .apache .lucene .store .ByteArrayDataInput ;
2725import org .apache .lucene .store .ByteArrayDataOutput ;
26+ import org .apache .lucene .store .ByteBuffersDataInput ;
27+ import org .apache .lucene .store .ByteBuffersDataOutput ;
28+ import org .apache .lucene .store .DataInput ;
2829import org .apache .lucene .store .Directory ;
2930import org .apache .lucene .store .IOContext ;
3031import org .apache .lucene .store .IndexInput ;
3132import org .apache .lucene .store .IndexOutput ;
3233import org .apache .lucene .store .MMapDirectory ;
34+ import org .apache .lucene .store .NIOFSDirectory ;
35+ import org .apache .lucene .util .GroupVIntUtil ;
3336import org .openjdk .jmh .annotations .Benchmark ;
3437import org .openjdk .jmh .annotations .BenchmarkMode ;
3538import org .openjdk .jmh .annotations .Fork ;
@@ -86,35 +89,49 @@ public class GroupVIntBenchmark {
8689 final long [] values = new long [maxSize ];
8790
8891 IndexInput byteBufferGVIntIn ;
92+ IndexInput nioGVIntIn ;
8993 IndexInput byteBufferVIntIn ;
94+ ByteBuffersDataInput byteBuffersGVIntIn ;
9095
9196 ByteArrayDataInput byteArrayVIntIn ;
9297 ByteArrayDataInput byteArrayGVIntIn ;
9398
94- // @Param({"16", "32", "64", "128", "248"})
9599 @ Param ({"64" })
96100 public int size ;
97101
98102 void initArrayInput (long [] docs ) throws Exception {
99103 byte [] gVIntBytes = new byte [Integer .BYTES * maxSize * 2 ];
100104 byte [] vIntBytes = new byte [Integer .BYTES * maxSize * 2 ];
101105 ByteArrayDataOutput vIntOut = new ByteArrayDataOutput (vIntBytes );
102- GroupVIntWriter w = new GroupVIntWriter ( );
103- w . writeValues ( new ByteArrayDataOutput ( gVIntBytes ), docs , docs .length );
106+ ByteArrayDataOutput out = new ByteArrayDataOutput ( gVIntBytes );
107+ out . writeGroupVInts ( docs , docs .length );
104108 for (long v : docs ) {
105109 vIntOut .writeVInt ((int ) v );
106110 }
107111 byteArrayVIntIn = new ByteArrayDataInput (vIntBytes );
108112 byteArrayGVIntIn = new ByteArrayDataInput (gVIntBytes );
109113 }
110114
115+ void initNioInput (long [] docs ) throws Exception {
116+ Directory dir = new NIOFSDirectory (Files .createTempDirectory ("groupvintdata" ));
117+ IndexOutput out = dir .createOutput ("gvint" , IOContext .DEFAULT );
118+ out .writeGroupVInts (docs , docs .length );
119+ out .close ();
120+ nioGVIntIn = dir .openInput ("gvint" , IOContext .DEFAULT );
121+ }
122+
123+ void initByteBuffersInput (long [] docs ) throws Exception {
124+ ByteBuffersDataOutput buffer = new ByteBuffersDataOutput ();
125+ buffer .writeGroupVInts (docs , docs .length );
126+ byteBuffersGVIntIn = buffer .toDataInput ();
127+ }
128+
111129 void initByteBufferInput (long [] docs ) throws Exception {
112- Directory dir = MMapDirectory . open (Files .createTempDirectory ("groupvintdata" ));
130+ Directory dir = new MMapDirectory (Files .createTempDirectory ("groupvintdata" ));
113131 IndexOutput vintOut = dir .createOutput ("vint" , IOContext .DEFAULT );
114132 IndexOutput gvintOut = dir .createOutput ("gvint" , IOContext .DEFAULT );
115133
116- GroupVIntWriter w = new GroupVIntWriter ();
117- w .writeValues (gvintOut , docs , docs .length );
134+ gvintOut .writeGroupVInts (docs , docs .length );
118135 for (long v : docs ) {
119136 vintOut .writeVInt ((int ) v );
120137 }
@@ -124,6 +141,16 @@ void initByteBufferInput(long[] docs) throws Exception {
124141 byteBufferVIntIn = dir .openInput ("vint" , IOContext .DEFAULT );
125142 }
126143
144+ private void readGroupVIntsBaseline (DataInput in , long [] dst , int limit ) throws IOException {
145+ int i ;
146+ for (i = 0 ; i <= limit - 4 ; i += 4 ) {
147+ GroupVIntUtil .readGroupVInt (in , dst , i );
148+ }
149+ for (; i < limit ; ++i ) {
150+ dst [i ] = in .readVInt ();
151+ }
152+ }
153+
127154 @ Setup (Level .Trial )
128155 public void init () throws Exception {
129156 long [] docs = new long [maxSize ];
@@ -140,10 +167,12 @@ public void init() throws Exception {
140167 }
141168 initByteBufferInput (docs );
142169 initArrayInput (docs );
170+ initNioInput (docs );
171+ initByteBuffersInput (docs );
143172 }
144173
145174 @ Benchmark
146- public void byteBufferReadVInt (Blackhole bh ) throws IOException {
175+ public void benchMMapDirectoryInputs_readVInt (Blackhole bh ) throws IOException {
147176 byteBufferVIntIn .seek (0 );
148177 for (int i = 0 ; i < size ; i ++) {
149178 values [i ] = byteBufferVIntIn .readVInt ();
@@ -152,14 +181,21 @@ public void byteBufferReadVInt(Blackhole bh) throws IOException {
152181 }
153182
154183 @ Benchmark
155- public void byteBufferReadGroupVInt (Blackhole bh ) throws IOException {
184+ public void benchMMapDirectoryInputs_readGroupVInt (Blackhole bh ) throws IOException {
156185 byteBufferGVIntIn .seek (0 );
157- GroupVIntReader . readValues ( byteBufferGVIntIn , values , size );
186+ byteBufferGVIntIn . readGroupVInts ( values , size );
158187 bh .consume (values );
159188 }
160189
161190 @ Benchmark
162- public void byteArrayReadVInt (Blackhole bh ) {
191+ public void benchMMapDirectoryInputs_readGroupVIntBaseline (Blackhole bh ) throws IOException {
192+ byteBufferGVIntIn .seek (0 );
193+ this .readGroupVIntsBaseline (byteBufferGVIntIn , values , size );
194+ bh .consume (values );
195+ }
196+
197+ @ Benchmark
198+ public void benchByteArrayDataInput_readVInt (Blackhole bh ) {
163199 byteArrayVIntIn .rewind ();
164200 for (int i = 0 ; i < size ; i ++) {
165201 values [i ] = byteArrayVIntIn .readVInt ();
@@ -168,9 +204,37 @@ public void byteArrayReadVInt(Blackhole bh) {
168204 }
169205
170206 @ Benchmark
171- public void byteArrayReadGroupVInt (Blackhole bh ) throws IOException {
207+ public void benchByteArrayDataInput_readGroupVInt (Blackhole bh ) throws IOException {
172208 byteArrayGVIntIn .rewind ();
173- GroupVIntReader .readValues (byteArrayGVIntIn , values , size );
209+ byteArrayGVIntIn .readGroupVInts (values , size );
210+ bh .consume (values );
211+ }
212+
213+ @ Benchmark
214+ public void benchNIOFSDirectoryInputs_readGroupVInt (Blackhole bh ) throws IOException {
215+ nioGVIntIn .seek (0 );
216+ nioGVIntIn .readGroupVInts (values , size );
217+ bh .consume (values );
218+ }
219+
220+ @ Benchmark
221+ public void benchNIOFSDirectoryInputs_readGroupVIntBaseline (Blackhole bh ) throws IOException {
222+ nioGVIntIn .seek (0 );
223+ this .readGroupVIntsBaseline (nioGVIntIn , values , size );
224+ bh .consume (values );
225+ }
226+
227+ @ Benchmark
228+ public void benchByteBuffersIndexInput_readGroupVInt (Blackhole bh ) throws IOException {
229+ byteBuffersGVIntIn .seek (0 );
230+ byteBuffersGVIntIn .readGroupVInts (values , size );
231+ bh .consume (values );
232+ }
233+
234+ @ Benchmark
235+ public void benchByteBuffersIndexInput_readGroupVIntBaseline (Blackhole bh ) throws IOException {
236+ byteBuffersGVIntIn .seek (0 );
237+ this .readGroupVIntsBaseline (byteBuffersGVIntIn , values , size );
174238 bh .consume (values );
175239 }
176240}
0 commit comments