Skip to content

Commit 727631e

Browse files
Using LongBuffer in LongStorage. 2,78ms
1 parent 32c670d commit 727631e

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

std-bits/table/src/main/java/org/enso/table/data/column/builder/LongBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.enso.table.data.column.builder;
22

3+
import java.nio.LongBuffer;
34
import java.util.Objects;
45
import org.enso.base.polyglot.NumericConverter;
56
import org.enso.table.data.column.storage.ColumnBooleanStorage;
@@ -185,6 +186,7 @@ public LongBuilder append(Object o) {
185186

186187
@Override
187188
public ColumnStorage<Long> seal() {
188-
return new LongStorage(data, currentSize, isNothing, getType());
189+
var buf = LongBuffer.wrap(data);
190+
return new LongStorage(buf, isNothing, getType());
189191
}
190192
}

std-bits/table/src/main/java/org/enso/table/data/column/storage/LongStorage.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.enso.table.data.column.storage;
22

3+
import java.nio.LongBuffer;
34
import java.util.BitSet;
45
import java.util.NoSuchElementException;
56
import org.enso.table.data.column.storage.iterators.ColumnLongStorageIterator;
@@ -10,29 +11,24 @@ public final class LongStorage extends AbstractLongStorage implements ColumnStor
1011
// TODO [RW] at some point we will want to add separate storage classes for byte, short and int,
1112
// for more compact storage and more efficient handling of smaller integers; for now we will be
1213
// handling this just by checking the bounds
13-
final long[] data;
14+
private final LongBuffer data;
1415
final BitSet isNothing;
1516

1617
/**
1718
* @param data the underlying data
18-
* @param size the number of items stored
1919
* @param isNothing a bit set denoting at index {@code i} whether or not the value at index {@code
2020
* i} is missing.
2121
* @param type the type specifying the bit-width of integers that are allowed in this storage
2222
*/
23-
public LongStorage(long[] data, int size, BitSet isNothing, IntegerType type) {
24-
super(size, type);
23+
public LongStorage(LongBuffer data, BitSet isNothing, IntegerType type) {
24+
super(data.limit(), type);
2525
this.data = data;
2626
this.isNothing = isNothing;
2727
}
2828

29-
public LongStorage(long[] data, IntegerType type) {
30-
this(data, data.length, new BitSet(), type);
31-
}
32-
3329
@Override
3430
public long getItemAsLong(long index) {
35-
return data[(int) index];
31+
return data.get((int) index);
3632
}
3733

3834
@Override
@@ -52,71 +48,75 @@ public BitSet getIsNothingMap() {
5248
@Override
5349
public LongStorage widen(IntegerType widerType) {
5450
assert widerType.fits(getType());
55-
return new LongStorage(data, (int) getSize(), getIsNothingMap(), widerType);
51+
return new LongStorage(data, getIsNothingMap(), widerType);
5652
}
5753

5854
/** Allow access to the underlying data array for copying. */
59-
public long[] getData() {
60-
return data;
55+
public LongBuffer getData() {
56+
return data.asReadOnlyBuffer();
6157
}
6258

6359
@Override
6460
public ColumnLongStorageIterator iteratorWithIndex() {
65-
return new LongStorageIterator(data, isNothing, (int) getSize());
61+
return new LongStorageIterator(data.asReadOnlyBuffer(), isNothing);
6662
}
6763

6864
private static class LongStorageIterator implements ColumnLongStorageIterator {
69-
private final long[] data;
65+
private final LongBuffer data;
7066
private final BitSet isNothing;
71-
private final int size;
72-
private int index = -1;
7367

74-
public LongStorageIterator(long[] data, BitSet isNothing, int size) {
68+
public LongStorageIterator(LongBuffer data, BitSet isNothing) {
7569
this.data = data;
7670
this.isNothing = isNothing;
77-
this.size = size;
7871
}
7972

8073
@Override
8174
public Long getItemBoxed() {
82-
return isNothing.get(index) ? null : data[index];
75+
var index = data.position();
76+
var item = data.get(index);
77+
return isNothing.get(index) ? null : item;
8378
}
8479

8580
@Override
8681
public long getItemAsLong() {
87-
return data[index];
82+
var index = data.position();
83+
var item = data.get(index);
84+
return item;
8885
}
8986

9087
@Override
9188
public boolean isNothing() {
89+
var index = data.position();
9290
return isNothing.get(index);
9391
}
9492

9593
@Override
9694
public boolean hasNext() {
97-
return index + 1 < size;
95+
var index = data.position();
96+
return index + 1 < data.limit();
9897
}
9998

10099
@Override
101100
public Long next() {
102101
if (!hasNext()) {
103102
throw new NoSuchElementException();
104103
}
105-
index++;
104+
var index = data.position();
105+
data.position(index + 1);
106106
return getItemBoxed();
107107
}
108108

109109
@Override
110110
public long getIndex() {
111-
return index;
111+
return data.position();
112112
}
113113

114114
@Override
115115
public boolean moveNext() {
116116
if (!hasNext()) {
117117
return false;
118118
}
119-
index++;
119+
data.position(data.position() + 1);
120120
return true;
121121
}
122122
}

0 commit comments

Comments
 (0)