提交 0280bbde 编写于 作者: V Vlad Ilyushchenko

fix: remove read of array element via unsafe - it is a slower method compared...

fix: remove read of array element via unsafe - it is a slower method compared to conventional array read.
上级 3c60662e
......@@ -27,7 +27,6 @@ import io.questdb.griffin.TypeEx;
import io.questdb.std.CharSequenceIntHashMap;
import io.questdb.std.IntObjHashMap;
import io.questdb.std.Long256;
import io.questdb.std.Unsafe;
import io.questdb.std.str.StringSink;
public final class ColumnType {
......@@ -135,13 +134,13 @@ public final class ColumnType {
}
public static int pow2SizeOf(int columnType) {
return Unsafe.arrayGet(TYPE_SIZE_POW2, columnType);
return TYPE_SIZE_POW2[columnType];
}
public static int sizeOf(int columnType) {
if (columnType < 0 || columnType > ColumnType.PARAMETER) {
return -1;
}
return Unsafe.arrayGet(TYPE_SIZE, columnType);
return TYPE_SIZE[columnType];
}
}
......@@ -25,7 +25,10 @@ package io.questdb.cairo;
import io.questdb.cairo.sql.Record;
import io.questdb.cairo.sql.RecordCursor;
import io.questdb.std.*;
import io.questdb.std.BinarySequence;
import io.questdb.std.Long256;
import io.questdb.std.Mutable;
import io.questdb.std.Transient;
import io.questdb.std.str.CharSink;
import java.io.Closeable;
......@@ -352,7 +355,7 @@ public class RecordChain implements Closeable, RecordCursor, Mutable, RecordSink
}
private long fixedWithColumnOffset(int index) {
return fixedOffset + Unsafe.arrayGet(columnOffsets, index);
return fixedOffset + columnOffsets[index];
}
private void of(long offset) {
......@@ -361,7 +364,7 @@ public class RecordChain implements Closeable, RecordCursor, Mutable, RecordSink
}
private long varWidthColumnOffset(int index) {
return mem.getLong(baseOffset + Unsafe.arrayGet(columnOffsets, index));
return mem.getLong(baseOffset + columnOffsets[index]);
}
}
}
......@@ -482,8 +482,8 @@ public class VirtualMemory implements Closeable {
Unsafe.getUnsafe().putLong(appendPointer + 24, 0);
long o = 0;
for (int i = len / 2 - 1; i > 0; i--) {
final int d1 = Unsafe.arrayGet(Numbers.hexNumbers, hexString.charAt(i * 2));
final int d2 = Unsafe.arrayGet(Numbers.hexNumbers, hexString.charAt(i * 2 + 1));
final int d1 = Numbers.hexNumbers[(int) hexString.charAt(i * 2)];
final int d2 = Numbers.hexNumbers[(int) hexString.charAt(i * 2 + 1)];
Unsafe.getUnsafe().putByte(appendPointer + o++, (byte) ((d1 << 4) + d2));
}
}
......@@ -947,8 +947,8 @@ public class VirtualMemory implements Closeable {
putLong(0);
putLong(0);
for (int i = len / 2 - 1; i > 0; i--) {
int d1 = Unsafe.arrayGet(Numbers.hexNumbers, hexString.charAt(i * 2));
int d2 = Unsafe.arrayGet(Numbers.hexNumbers, hexString.charAt(i * 2 + 1));
int d1 = Numbers.hexNumbers[(int) hexString.charAt(i * 2)];
int d2 = Numbers.hexNumbers[(int) hexString.charAt(i * 2 + 1)];
putByte(offset++, (byte) ((d1 << 4) + d2));
}
}
......
......@@ -26,7 +26,10 @@ package io.questdb.cairo.map;
import io.questdb.cairo.*;
import io.questdb.cairo.sql.Record;
import io.questdb.cairo.sql.RecordCursor;
import io.questdb.std.*;
import io.questdb.std.BinarySequence;
import io.questdb.std.Long256;
import io.questdb.std.Misc;
import io.questdb.std.Numbers;
/**
* Storage structure to support queries such as "select distinct ...",
......@@ -634,7 +637,7 @@ public class CompactMap implements Map {
}
private long nextSlot(long slot, int distance) {
return (slot + Unsafe.arrayGet(jumpDistances, distance)) & mask;
return (slot + jumpDistances[distance]) & mask;
}
private void putEntryAt(long entryOffset, long slot, byte flag) {
......
......@@ -28,7 +28,6 @@ import io.questdb.cairo.VirtualMemory;
import io.questdb.cairo.sql.RecordCursor;
import io.questdb.std.BinarySequence;
import io.questdb.std.IntList;
import io.questdb.std.Unsafe;
class CompactMapRecord implements MapRecord {
......@@ -155,7 +154,7 @@ class CompactMapRecord implements MapRecord {
}
private long getColumnOffset(int columnIndex) {
return offset + Unsafe.arrayGet(columnOffsets, columnIndex);
return offset + columnOffsets[columnIndex];
}
long getNextRecordOffset() {
......
......@@ -24,7 +24,6 @@
package io.questdb.cairo.map;
import io.questdb.cairo.VirtualMemory;
import io.questdb.std.Unsafe;
class CompactMapValue implements MapValue {
......@@ -192,7 +191,7 @@ class CompactMapValue implements MapValue {
private long getValueColumnOffset(int columnIndex) {
assert currentValueOffset != -1;
return currentValueOffset + Unsafe.arrayGet(columnOffsets, columnIndex);
return currentValueOffset + columnOffsets[columnIndex];
}
void linkRecord(CompactMapRecord record) {
......
......@@ -227,7 +227,7 @@ final class FastMapRecord implements MapRecord {
@Override
public CharSequence getStr(int columnIndex) {
assert columnIndex < csA.length;
return getStr0(columnIndex, Unsafe.arrayGet(csA, columnIndex));
return getStr0(columnIndex, csA[columnIndex]);
}
@Override
......@@ -243,7 +243,7 @@ final class FastMapRecord implements MapRecord {
@Override
public CharSequence getStrB(int columnIndex) {
return getStr0(columnIndex, Unsafe.arrayGet(csB, columnIndex));
return getStr0(columnIndex, csB[columnIndex]);
}
@Override
......@@ -270,7 +270,7 @@ final class FastMapRecord implements MapRecord {
private long addressOfColumn(int index) {
if (index < split) {
return address0 + Unsafe.arrayGet(valueOffsets, index);
return address0 + valueOffsets[index];
}
if (index == split) {
......@@ -296,7 +296,7 @@ final class FastMapRecord implements MapRecord {
csB = new DirectCharSequence[n];
for (int i = 0; i < n; i++) {
if (Unsafe.arrayGet(this.csA, i) != null) {
if (this.csA[i] != null) {
Unsafe.arrayPut(csA, i, new DirectCharSequence());
Unsafe.arrayPut(csB, i, new DirectCharSequence());
}
......@@ -310,7 +310,7 @@ final class FastMapRecord implements MapRecord {
int n = this.bs.length;
bs = new DirectBinarySequence[n];
for (int i = 0; i < n; i++) {
if (Unsafe.arrayGet(this.bs, i) != null) {
if (this.bs[i] != null) {
Unsafe.arrayPut(bs, i, new DirectBinarySequence());
}
}
......@@ -324,7 +324,7 @@ final class FastMapRecord implements MapRecord {
long256B = new Long256Impl[n];
for (int i = 0; i < n; i++) {
if (Unsafe.arrayGet(this.long256A, i) != null) {
if (this.long256A[i] != null) {
Unsafe.arrayPut(csA, i, new Long256Impl());
Unsafe.arrayPut(csB, i, new Long256Impl());
}
......
......@@ -187,7 +187,7 @@ final class FastMapValue implements MapValue {
}
private long address0(int index) {
return address + Unsafe.arrayGet(valueOffsets, index);
return address + valueOffsets[index];
}
void linkRecord(FastMapRecord record) {
......
......@@ -74,7 +74,7 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
for (int i = 0; i < ENTRY_SIZE; i++) {
if (Unsafe.cas(e.allocations, i, UNALLOCATED, thread)) {
// got lock, allocate if needed
R r = Unsafe.arrayGet(e.readers, i);
R r = e.readers[i];
if (r == null) {
try {
......@@ -126,7 +126,7 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
Entry e = me.getValue();
do {
for (int i = 0; i < ENTRY_SIZE; i++) {
if (Unsafe.arrayGetVolatile(e.allocations, i) != UNALLOCATED && Unsafe.arrayGet(e.readers, i) != null) {
if (Unsafe.arrayGetVolatile(e.allocations, i) != UNALLOCATED && e.readers[i] != null) {
count++;
}
}
......@@ -153,13 +153,13 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
closeReader(thread, e, i, PoolListener.EV_LOCK_CLOSE, PoolConstants.CR_NAME_LOCK);
} else if (Unsafe.cas(e.allocations, i, thread, thread)) {
// same thread, don't need to order reads
if (Unsafe.arrayGet(e.readers, i) != null) {
if (e.readers[i] != null) {
// this thread has busy reader, it should close first
e.lockOwner = -1L;
return false;
}
} else {
LOG.info().$("could not lock, busy [table=`").utf8(name).$("`, at=").$(e.index).$(':').$(i).$(", owner=").$(Unsafe.arrayGet(e.allocations, i)).$(", thread=").$(thread).$(']').$();
LOG.info().$("could not lock, busy [table=`").utf8(name).$("`, at=").$(e.index).$(':').$(i).$(", owner=").$(e.allocations[i]).$(", thread=").$(thread).$(']').$();
e.lockOwner = -1L;
return false;
}
......@@ -216,6 +216,17 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
LOG.info().$("closed").$();
}
private void closeReader(long thread, Entry entry, int index, short ev, int reason) {
R r = entry.readers[index];
if (r != null) {
r.goodby();
r.close();
LOG.info().$("closed '").$(r.getTableName()).$("' [at=").$(entry.index).$(':').$(index).$(", reason=").$(PoolConstants.closeReasonText(reason)).$(']').$();
notifyListener(thread, r.getTableName(), ev, entry.index, index);
Unsafe.arrayPut(entry.readers, index, null);
}
}
@Override
protected boolean releaseAll(long deadline) {
long thread = Thread.currentThread().getId();
......@@ -230,10 +241,10 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
do {
for (int i = 0; i < ENTRY_SIZE; i++) {
R r;
if (deadline > Unsafe.arrayGetVolatile(e.releaseTimes, i) && (r = Unsafe.arrayGet(e.readers, i)) != null) {
if (deadline > Unsafe.arrayGetVolatile(e.releaseTimes, i) && (r = e.readers[i]) != null) {
if (Unsafe.cas(e.allocations, i, UNALLOCATED, thread)) {
// check if deadline violation still holds
if (deadline > Unsafe.arrayGet(e.releaseTimes, i)) {
if (deadline > e.releaseTimes[i]) {
removed = true;
closeReader(thread, e, i, PoolListener.EV_EXPIRE, closeReason);
}
......@@ -261,17 +272,6 @@ public class ReaderPool extends AbstractPool implements ResourcePool<TableReader
}
}
private void closeReader(long thread, Entry entry, int index, short ev, int reason) {
R r = Unsafe.arrayGet(entry.readers, index);
if (r != null) {
r.goodby();
r.close();
LOG.info().$("closed '").$(r.getTableName()).$("' [at=").$(entry.index).$(':').$(index).$(", reason=").$(PoolConstants.closeReasonText(reason)).$(']').$();
notifyListener(thread, r.getTableName(), ev, entry.index, index);
Unsafe.arrayPut(entry.readers, index, null);
}
}
private Entry getEntry(CharSequence name) {
checkClosed();
......
......@@ -123,7 +123,7 @@ public class CharacterStore extends AbstractCharSink implements CharacterStoreEn
@Override
public char charAt(int index) {
return Unsafe.arrayGet(chars, lo + index);
return chars[lo + index];
}
@Override
......
......@@ -25,8 +25,6 @@
package io.questdb.griffin.engine.functions.regex.impl;
import io.questdb.std.Unsafe;
import java.util.Arrays;
import java.util.Objects;
......@@ -630,11 +628,11 @@ public final class Matcher implements MatchResult {
}
public int firstEndQuick() {
return Unsafe.arrayGet(groups, 3);
return groups[3];
}
public int firstStartQuick() {
return Unsafe.arrayGet(groups, 2);
return groups[2];
}
/**
......@@ -665,7 +663,7 @@ public final class Matcher implements MatchResult {
}
public int groupQuick(int index) {
return Unsafe.arrayGet(groups, index);
return groups[index];
}
/**
......
......@@ -5333,10 +5333,10 @@ public final class Pattern
// Loop over pattern from right to left
for (int j = patternLength - 1; j > -1; j--) {
int ch = seq.charAt(i + j);
if (ch != Unsafe.arrayGet(src, j)) {
if (ch != src[j]) {
// Shift search to the right by the maximum of the
// bad character shift and the good suffix shift
i += Math.max(j + 1 - Unsafe.arrayGet(lastOcc, ch & 0x7F), Unsafe.arrayGet(optoSft, j));
i += Math.max(j + 1 - lastOcc[ch & 0x7F], optoSft[j]);
continue NEXT;
}
}
......
......@@ -443,7 +443,7 @@ public class LogFactory implements Closeable {
public void bind(ObjHashSet<LogWriter> jobs, int queueDepth, int recordLength) {
// create queues for processed channels
for (int i = 0, n = channels.length; i < n; i++) {
int index = Unsafe.arrayGet(channels, i);
int index = channels[i];
if (index > 0) {
int keyIndex = holderMap.keyIndex(index);
if (keyIndex > -1) {
......@@ -537,7 +537,7 @@ public class LogFactory implements Closeable {
for (int i = 0, n = channels.length; i < n; i++) {
if (((mask >> i) & 1) == 1) {
int that = Unsafe.arrayGet(channels, i);
int that = channels[i];
if (that == 0) {
Unsafe.arrayPut(channels, i, q);
}
......
......@@ -24,7 +24,6 @@
package io.questdb.mp;
import io.questdb.std.ObjectFactory;
import io.questdb.std.Unsafe;
public class RingQueue<T> {
private final int mask;
......@@ -41,7 +40,7 @@ public class RingQueue<T> {
}
public T get(long cursor) {
return Unsafe.arrayGet(buf, (int) (cursor & mask));
return buf[(int) (cursor & mask)];
}
public int getCapacity() {
......
......@@ -62,11 +62,11 @@ public abstract class AbstractCharSequenceHashSet implements Mutable {
public int keyIndex(CharSequence key) {
int index = Chars.hashCode(key) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
if (Chars.equals(key, Unsafe.arrayGet(keys, index))) {
if (Chars.equals(key, keys[index])) {
return -index - 1;
}
......@@ -76,10 +76,10 @@ public abstract class AbstractCharSequenceHashSet implements Mutable {
public int keyIndex(CharSequence key, int lo, int hi) {
int index = Chars.hashCode(key, lo, hi) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
CharSequence cs = Unsafe.arrayGet(keys, index);
CharSequence cs = keys[index];
if (Chars.equals(key, lo, hi, cs, 0, cs.length())) {
return -index - 1;
}
......@@ -110,14 +110,14 @@ public abstract class AbstractCharSequenceHashSet implements Mutable {
// After slot if freed these keys require re-hash
from = (from + 1) & mask;
for (
CharSequence key = Unsafe.arrayGet(keys, from);
CharSequence key = keys[from];
key != noEntryKey;
from = (from + 1) & mask, key = Unsafe.arrayGet(keys, from)
from = (from + 1) & mask, key = keys[from]
) {
int idealHit = Chars.hashCode(key) & mask;
if (idealHit != from) {
int to;
if (Unsafe.arrayGet(keys, idealHit) != noEntryKey) {
if (keys[idealHit] != noEntryKey) {
to = probe(key, idealHit);
} else {
to = idealHit;
......@@ -147,10 +147,10 @@ public abstract class AbstractCharSequenceHashSet implements Mutable {
private int probe(CharSequence key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
if (Chars.equals(key, Unsafe.arrayGet(keys, index))) {
if (Chars.equals(key, keys[index])) {
return -index - 1;
}
} while (true);
......@@ -159,10 +159,10 @@ public abstract class AbstractCharSequenceHashSet implements Mutable {
private int probe(CharSequence key, int lo, int hi, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
CharSequence cs = Unsafe.arrayGet(keys, index);
CharSequence cs = keys[index];
if (Chars.equals(key, lo, hi, cs, 0, cs.length())) {
return -index - 1;
}
......
......@@ -63,11 +63,11 @@ public abstract class AbstractIntHashSet implements Mutable {
public int keyIndex(int key) {
int index = key & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKeyValue) {
if (keys[index] == noEntryKeyValue) {
return index;
}
if (key == Unsafe.arrayGet(keys, index)) {
if (key == keys[index]) {
return -index - 1;
}
......@@ -98,14 +98,14 @@ public abstract class AbstractIntHashSet implements Mutable {
// After slot if freed these keys require re-hash
from = (from + 1) & mask;
for (
int key = Unsafe.arrayGet(keys, from);
int key = keys[from];
key != noEntryKeyValue;
from = (from + 1) & mask, key = Unsafe.arrayGet(keys, from)
from = (from + 1) & mask, key = keys[from]
) {
int idealHit = key & mask;
if (idealHit != from) {
int to;
if (Unsafe.arrayGet(keys, idealHit) != noEntryKeyValue) {
if (keys[idealHit] != noEntryKeyValue) {
to = probe(key, idealHit);
} else {
to = idealHit;
......@@ -130,10 +130,10 @@ public abstract class AbstractIntHashSet implements Mutable {
private int probe(int key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKeyValue) {
if (keys[index] == noEntryKeyValue) {
return index;
}
if (key == Unsafe.arrayGet(keys, index)) {
if (key == keys[index]) {
return -index - 1;
}
} while (true);
......
......@@ -63,11 +63,11 @@ public abstract class AbstractLongHashSet implements Mutable {
public int keyIndex(long key) {
int index = (int) (key & mask);
if (Unsafe.arrayGet(keys, index) == noEntryKeyValue) {
if (keys[index] == noEntryKeyValue) {
return index;
}
if (key == Unsafe.arrayGet(keys, index)) {
if (key == keys[index]) {
return -index - 1;
}
......@@ -98,14 +98,14 @@ public abstract class AbstractLongHashSet implements Mutable {
// After slot if freed these keys require re-hash
from = (from + 1) & mask;
for (
long key = Unsafe.arrayGet(keys, from);
long key = keys[from];
key != noEntryKeyValue;
from = (from + 1) & mask, key = Unsafe.arrayGet(keys, from)
from = (from + 1) & mask, key = keys[from]
) {
int idealHit = (int) (key & mask);
if (idealHit != from) {
int to;
if (Unsafe.arrayGet(keys, idealHit) != noEntryKeyValue) {
if (keys[idealHit] != noEntryKeyValue) {
to = probe(key, idealHit);
} else {
to = idealHit;
......@@ -130,10 +130,10 @@ public abstract class AbstractLongHashSet implements Mutable {
private int probe(long key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKeyValue) {
if (keys[index] == noEntryKeyValue) {
return index;
}
if (key == Unsafe.arrayGet(keys, index)) {
if (key == keys[index]) {
return -index - 1;
}
} while (true);
......
......@@ -63,11 +63,11 @@ public abstract class AbstractLowerCaseAsciiCharSequenceHashSet implements Mutab
public int keyIndex(CharSequence key) {
int index = Chars.lowerCaseAsciiHashCode(key) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
if (Chars.equalsLowerCaseAscii(key, Unsafe.arrayGet(keys, index))) {
if (Chars.equalsLowerCaseAscii(key, keys[index])) {
return -index - 1;
}
......@@ -77,11 +77,11 @@ public abstract class AbstractLowerCaseAsciiCharSequenceHashSet implements Mutab
public int keyIndex(CharSequence key, int lo, int hi) {
int index = Chars.lowerCaseAsciiHashCode(key, lo, hi) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
CharSequence cs = Unsafe.arrayGet(keys, index);
CharSequence cs = keys[index];
if (Chars.equalsLowerCaseAscii(key, lo, hi, cs, 0, cs.length())) {
return -index - 1;
}
......@@ -112,14 +112,14 @@ public abstract class AbstractLowerCaseAsciiCharSequenceHashSet implements Mutab
// After slot if freed these keys require re-hash
from = (from + 1) & mask;
for (
CharSequence key = Unsafe.arrayGet(keys, from);
CharSequence key = keys[from];
key != noEntryKey;
from = (from + 1) & mask, key = Unsafe.arrayGet(keys, from)
from = (from + 1) & mask, key = keys[from]
) {
int idealHit = Chars.lowerCaseAsciiHashCode(key) & mask;
if (idealHit != from) {
int to;
if (Unsafe.arrayGet(keys, idealHit) != noEntryKey) {
if (keys[idealHit] != noEntryKey) {
to = probe(key, idealHit);
} else {
to = idealHit;
......@@ -149,10 +149,10 @@ public abstract class AbstractLowerCaseAsciiCharSequenceHashSet implements Mutab
private int probe(CharSequence key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
if (Chars.equalsLowerCaseAscii(key, Unsafe.arrayGet(keys, index))) {
if (Chars.equalsLowerCaseAscii(key, keys[index])) {
return -index - 1;
}
} while (true);
......@@ -161,10 +161,10 @@ public abstract class AbstractLowerCaseAsciiCharSequenceHashSet implements Mutab
private int probe(CharSequence key, int lo, int hi, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
if (keys[index] == noEntryKey) {
return index;
}
CharSequence cs = Unsafe.arrayGet(keys, index);
CharSequence cs = keys[index];
if (Chars.equalsLowerCaseAscii(key, lo, hi, cs, 0, cs.length())) {
return -index - 1;
}
......
......@@ -63,7 +63,7 @@ public class AssociativeCache<V> implements Closeable {
if (index == NOT_FOUND) {
return null;
}
return Unsafe.arrayGet(values, index);
return values[index];
}
public V poll(CharSequence key) {
......@@ -71,7 +71,7 @@ public class AssociativeCache<V> implements Closeable {
if (index == NOT_FOUND) {
return null;
}
V value = Unsafe.arrayGet(values, index);
V value = values[index];
Unsafe.arrayPut(values, index, null);
Unsafe.arrayPut(keys, index, null);
return value;
......@@ -79,7 +79,7 @@ public class AssociativeCache<V> implements Closeable {
public CharSequence put(CharSequence key, V value) {
int lo = lo(key);
CharSequence ok = Unsafe.arrayGet(keys, lo + bmask);
CharSequence ok = keys[lo + bmask];
if (ok != null) {
free(lo + bmask);
}
......@@ -100,13 +100,13 @@ public class AssociativeCache<V> implements Closeable {
}
private void free(int lo) {
Unsafe.arrayPut(values, lo, Misc.free(Unsafe.arrayGet(values, lo)));
Unsafe.arrayPut(values, lo, Misc.free(values[lo]));
}
private int getIndex(CharSequence key) {
int lo = lo(key);
for (int i = lo, hi = lo + blocks; i < hi; i++) {
CharSequence k = Unsafe.arrayGet(keys, i);
CharSequence k = keys[i];
if (k == null) {
return NOT_FOUND;
}
......
......@@ -121,12 +121,9 @@ public class CharSequenceHashSet extends AbstractCharSequenceHashSet {
return -1;
}
public void removeAt(int index) {
if (index < 0) {
CharSequence key = Unsafe.arrayGet(keys, -index - 1);
super.removeAt(index);
list.remove(key);
}
public int getListIndexAt(int keyIndex) {
int index = -keyIndex - 1;
return list.indexOf(keys[index]);
}
@Override
......@@ -134,10 +131,9 @@ public class CharSequenceHashSet extends AbstractCharSequenceHashSet {
Unsafe.arrayPut(keys, index, noEntryKey);
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
erase(from);
public CharSequence keyAt(int index) {
int index1 = -index - 1;
return keys[index1];
}
public boolean contains(CharSequence key) {
......@@ -152,12 +148,19 @@ public class CharSequenceHashSet extends AbstractCharSequenceHashSet {
return list.getLast();
}
public int getListIndexAt(int keyIndex) {
return list.indexOf(Unsafe.arrayGet(keys, -keyIndex - 1));
public void removeAt(int index) {
if (index < 0) {
int index1 = -index - 1;
CharSequence key = keys[index1];
super.removeAt(index);
list.remove(key);
}
}
public CharSequence keyAt(int index) {
return Unsafe.arrayGet(keys, -index - 1);
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, keys[from]);
erase(from);
}
public int removeNull() {
......
......@@ -54,11 +54,13 @@ public class CharSequenceIntHashMap extends AbstractCharSequenceHashSet {
Arrays.fill(values, noEntryValue);
}
public void removeAt(int index) {
public void increment(CharSequence key) {
int index = keyIndex(key);
if (index < 0) {
CharSequence key = Unsafe.arrayGet(keys, -index - 1);
super.removeAt(index);
list.remove(key);
int index1 = -index - 1;
Unsafe.arrayPut(values, -index - 1, values[index1] + 1);
} else {
putAt0(index, key.toString(), 0);
}
}
......@@ -82,18 +84,18 @@ public class CharSequenceIntHashMap extends AbstractCharSequenceHashSet {
CharSequence[] otherKeys = other.keys;
int[] otherValues = other.values;
for (int i = 0, n = otherKeys.length; i < n; i++) {
if (Unsafe.arrayGet(otherKeys, i) != noEntryKey) {
put(Unsafe.arrayGet(otherKeys, i), Unsafe.arrayGet(otherValues, i));
if (otherKeys[i] != noEntryKey) {
put(otherKeys[i], otherValues[i]);
}
}
}
public void increment(CharSequence key) {
int index = keyIndex(key);
public void removeAt(int index) {
if (index < 0) {
Unsafe.arrayPut(values, -index - 1, Unsafe.arrayGet(values, -index - 1) + 1);
} else {
putAt0(index, key.toString(), 0);
int index1 = -index - 1;
CharSequence key = keys[index1];
super.removeAt(index);
list.remove(key);
}
}
......@@ -105,8 +107,8 @@ public class CharSequenceIntHashMap extends AbstractCharSequenceHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
......@@ -133,7 +135,8 @@ public class CharSequenceIntHashMap extends AbstractCharSequenceHashSet {
}
public int valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noEntryValue;
int index1 = -index - 1;
return index < 0 ? values[index1] : noEntryValue;
}
private void putAt0(int index, CharSequence key, int value) {
......@@ -159,11 +162,11 @@ public class CharSequenceIntHashMap extends AbstractCharSequenceHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
CharSequence key = Unsafe.arrayGet(oldKeys, i);
CharSequence key = oldKeys[i];
if (key != null) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -53,11 +53,13 @@ public class CharSequenceObjHashMap<V> extends AbstractCharSequenceHashSet {
list.clear();
}
public void removeAt(int index) {
if (index < 0) {
CharSequence key = Unsafe.arrayGet(keys, -index - 1);
super.removeAt(index);
list.remove(key);
public void putAll(CharSequenceObjHashMap<V> other) {
CharSequence[] otherKeys = other.keys;
V[] otherValues = other.values;
for (int i = 0, n = otherKeys.length; i < n; i++) {
if (otherKeys[i] != noEntryKey) {
put(otherKeys[i], otherValues[i]);
}
}
}
......@@ -67,11 +69,13 @@ public class CharSequenceObjHashMap<V> extends AbstractCharSequenceHashSet {
Unsafe.arrayPut(values, index, null);
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
erase(from);
public void removeAt(int index) {
if (index < 0) {
int index1 = -index - 1;
CharSequence key = keys[index1];
super.removeAt(index);
list.remove(key);
}
}
public V get(CharSequence key) {
......@@ -86,14 +90,11 @@ public class CharSequenceObjHashMap<V> extends AbstractCharSequenceHashSet {
return putAt(keyIndex(key), key, value);
}
public void putAll(CharSequenceObjHashMap<V> other) {
CharSequence[] otherKeys = other.keys;
V[] otherValues = other.values;
for (int i = 0, n = otherKeys.length; i < n; i++) {
if (Unsafe.arrayGet(otherKeys, i) != noEntryKey) {
put(Unsafe.arrayGet(otherKeys, i), Unsafe.arrayGet(otherValues, i));
}
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
public boolean putAt(int index, CharSequence key, V value) {
......@@ -109,7 +110,8 @@ public class CharSequenceObjHashMap<V> extends AbstractCharSequenceHashSet {
}
public V valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : null;
int index1 = -index - 1;
return index < 0 ? values[index1] : null;
}
public V valueQuick(int index) {
......@@ -146,11 +148,11 @@ public class CharSequenceObjHashMap<V> extends AbstractCharSequenceHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
CharSequence key = Unsafe.arrayGet(oldKeys, i);
CharSequence key = oldKeys[i];
if (key != null) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -103,7 +103,8 @@ public class IntHashSet extends AbstractIntHashSet {
public void removeAt(int index) {
if (index < 0) {
int key = Unsafe.arrayGet(keys, -index - 1);
int index1 = -index - 1;
int key = keys[index1];
super.removeAt(index);
list.remove(key);
}
......@@ -116,7 +117,7 @@ public class IntHashSet extends AbstractIntHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(keys, to, keys[from]);
erase(from);
}
......
......@@ -65,7 +65,8 @@ public class IntIntHashMap extends AbstractIntHashSet {
}
public int valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noEntryValue;
int index1 = -index - 1;
return index < 0 ? values[index1] : noEntryValue;
}
@Override
......@@ -75,8 +76,8 @@ public class IntIntHashMap extends AbstractIntHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
......@@ -95,11 +96,11 @@ public class IntIntHashMap extends AbstractIntHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
int key = Unsafe.arrayGet(oldKeys, i);
int key = oldKeys[i];
if (key != noEntryKeyValue) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -74,7 +74,7 @@ public class IntList implements Mutable {
}
int mid = (low + high - 1) >>> 1;
long midVal = Unsafe.arrayGet(buffer, mid);
long midVal = buffer[mid];
if (midVal < v)
low = mid + 1;
......@@ -114,14 +114,14 @@ public class IntList implements Mutable {
public int get(int index) {
if (index < pos) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
throw new ArrayIndexOutOfBoundsException(index);
}
public int getLast() {
if (pos > 0) {
return Unsafe.arrayGet(buffer, pos - 1);
return buffer[pos - 1];
}
return noEntryValue;
}
......@@ -136,7 +136,7 @@ public class IntList implements Mutable {
* @return element at the specified position.
*/
public int getQuick(int index) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
/**
......@@ -149,7 +149,7 @@ public class IntList implements Mutable {
*/
public int getQuiet(int index) {
if (index < pos) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
return noEntryValue;
}
......@@ -194,7 +194,7 @@ public class IntList implements Mutable {
}
public void increment(int index) {
Unsafe.arrayPut(buffer, index, Unsafe.arrayGet(buffer, index) + 1);
Unsafe.arrayPut(buffer, index, buffer[index] + 1);
}
public void remove(int key) {
......
......@@ -57,7 +57,7 @@ public class IntLongAssociativeCache {
if (index == NOT_FOUND) {
return NO_VALUE;
}
return Unsafe.arrayGet(values, index);
return values[index];
}
public long poll(int key) {
......@@ -65,7 +65,7 @@ public class IntLongAssociativeCache {
if (index == NOT_FOUND) {
return NO_VALUE;
}
long value = Unsafe.arrayGet(values, index);
long value = values[index];
// Unsafe.arrayPut(values, index, 0);
Unsafe.arrayPut(keys, index, UNUSED_KEY);
return value;
......@@ -73,7 +73,7 @@ public class IntLongAssociativeCache {
public int put(int key, long value) {
int lo = lo(key);
int ok = Unsafe.arrayGet(keys, lo + bmask);
int ok = keys[lo + bmask];
System.arraycopy(keys, lo, keys, lo + 1, bmask);
System.arraycopy(values, lo, values, lo + 1, bmask);
Unsafe.arrayPut(keys, lo, key);
......@@ -84,7 +84,7 @@ public class IntLongAssociativeCache {
private int getIndex(int key) {
int lo = lo(key);
for (int i = lo, hi = lo + blocks; i < hi; i++) {
int k = Unsafe.arrayGet(keys, i);
int k = keys[i];
if (k == UNUSED_KEY) {
return NOT_FOUND;
}
......
......@@ -64,7 +64,7 @@ public class IntLongHashMap extends AbstractIntHashSet {
}
public long valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noEntryValue;
return index < 0 ? values[-index - 1] : noEntryValue;
}
@Override
......@@ -74,8 +74,8 @@ public class IntLongHashMap extends AbstractIntHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
......@@ -94,11 +94,11 @@ public class IntLongHashMap extends AbstractIntHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
int key = Unsafe.arrayGet(oldKeys, i);
int key = oldKeys[i];
if (key != noEntryKeyValue) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -56,11 +56,8 @@ public class IntObjHashMap<V> extends AbstractIntHashSet {
Unsafe.arrayPut(values, index, noEntryValue);
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
erase(from);
public V valueAt(int index) {
return index < 0 ? values[-index - 1] : null;
}
public V get(int key) {
......@@ -83,8 +80,11 @@ public class IntObjHashMap<V> extends AbstractIntHashSet {
}
}
public V valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : null;
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
@SuppressWarnings({"unchecked"})
......@@ -103,11 +103,11 @@ public class IntObjHashMap<V> extends AbstractIntHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
int key = Unsafe.arrayGet(oldKeys, i);
int key = oldKeys[i];
if (key != noEntryKeyValue) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -45,7 +45,7 @@ public class IntPriorityQueue {
}
public int pop() {
int v = Unsafe.arrayGet(buffer, 0);
int v = buffer[0];
if (--limit > 0) {
System.arraycopy(buffer, 1, buffer, 0, limit);
}
......@@ -84,14 +84,14 @@ public class IntPriorityQueue {
}
int mid = (low + high - 1) >>> 1;
int midVal = Unsafe.arrayGet(buffer, mid);
int midVal = buffer[mid];
if (midVal < v)
low = mid + 1;
else if (midVal > v)
high = mid;
else {
for (; ++mid < high && Unsafe.arrayGet(buffer, mid) == v; ) ;
for (; ++mid < high && buffer[mid] == v; ) ;
return mid;
}
}
......@@ -106,7 +106,7 @@ public class IntPriorityQueue {
private int scanSearch(int v) {
for (int i = 0; i < limit; i++) {
if (Unsafe.arrayGet(buffer, i) > v) {
if (buffer[i] > v) {
return i;
}
}
......
......@@ -50,12 +50,12 @@ public class IntStack implements Mutable {
}
public int peek() {
return Unsafe.arrayGet(elements, head);
return elements[head];
}
public int pop() {
int h = head;
int result = Unsafe.arrayGet(elements, h);
int result = elements[h];
if (result == noEntryValue) {
return noEntryValue;
}
......
......@@ -92,7 +92,7 @@ public class LongHashSet extends AbstractLongHashSet {
public void removeAt(int index) {
if (index < 0) {
long key = Unsafe.arrayGet(keys, -index - 1);
long key = keys[-index - 1];
super.removeAt(index);
list.remove(key);
}
......@@ -105,7 +105,7 @@ public class LongHashSet extends AbstractLongHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(keys, to, keys[from]);
erase(from);
}
......
......@@ -64,8 +64,8 @@ public class LongIntHashMap implements Mutable {
public int get(long key) {
int index = (int) key & mask;
if (Unsafe.arrayGet(values, index) == noEntryValue || Unsafe.arrayGet(keys, index) == key) {
return Unsafe.arrayGet(values, index);
if (values[index] == noEntryValue || keys[index] == key) {
return values[index];
}
return probe(key, index);
}
......@@ -79,14 +79,14 @@ public class LongIntHashMap implements Mutable {
private void insertKey(long key, int value) {
int index = (int) key & mask;
if (Unsafe.arrayGet(values, index) == noEntryValue) {
if (values[index] == noEntryValue) {
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, value);
free--;
return;
}
if (Unsafe.arrayGet(keys, index) == key) {
if (keys[index] == key) {
Unsafe.arrayPut(values, index, value);
return;
}
......@@ -97,8 +97,8 @@ public class LongIntHashMap implements Mutable {
private int probe(long key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(values, index) == noEntryValue || Unsafe.arrayGet(keys, index) == key) {
return Unsafe.arrayGet(values, index);
if (values[index] == noEntryValue || keys[index] == key) {
return values[index];
}
} while (true);
}
......@@ -106,14 +106,14 @@ public class LongIntHashMap implements Mutable {
private void probeInsert(long key, int index, int value) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(values, index) == noEntryValue) {
if (values[index] == noEntryValue) {
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, value);
free--;
return;
}
if (key == Unsafe.arrayGet(keys, index)) {
if (key == keys[index]) {
Unsafe.arrayPut(values, index, value);
return;
}
......@@ -121,12 +121,9 @@ public class LongIntHashMap implements Mutable {
}
private void rehash() {
int newCapacity = values.length << 1;
mask = newCapacity - 1;
free = (int) (newCapacity * loadFactor);
int[] oldValues = values;
long[] oldKeys = keys;
this.keys = new long[newCapacity];
......@@ -134,8 +131,9 @@ public class LongIntHashMap implements Mutable {
Arrays.fill(values, noEntryValue);
for (int i = oldKeys.length; i-- > 0; ) {
if (Unsafe.arrayGet(oldValues, i) != noEntryValue) {
insertKey(Unsafe.arrayGet(oldKeys, i), Unsafe.arrayGet(oldValues, i));
int val = oldValues[i];
if (val != noEntryValue) {
insertKey(oldKeys[i], val);
}
}
}
......
......@@ -106,7 +106,7 @@ public class LongList implements Mutable {
}
int mid = (low + high - 1) >>> 1;
long midVal = Unsafe.arrayGet(buffer, mid);
long midVal = buffer[mid];
if (midVal < v)
low = mid + 1;
......@@ -155,7 +155,7 @@ public class LongList implements Mutable {
public long get(int index) {
if (index < pos) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
throw new ArrayIndexOutOfBoundsException(index);
}
......@@ -173,7 +173,7 @@ public class LongList implements Mutable {
*/
public long getLast() {
if (pos > 0) {
return Unsafe.arrayGet(buffer, pos - 1);
return buffer[pos - 1];
}
return noEntryValue;
}
......@@ -188,7 +188,7 @@ public class LongList implements Mutable {
* @return element at the specified position.
*/
public long getQuick(int index) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
/**
......@@ -231,7 +231,7 @@ public class LongList implements Mutable {
}
public void increment(int index) {
Unsafe.arrayPut(buffer, index, Unsafe.arrayGet(buffer, index) + 1);
Unsafe.arrayPut(buffer, index, buffer[index] + 1);
}
public void remove(long v) {
......@@ -335,7 +335,7 @@ public class LongList implements Mutable {
}
private void let(int a, int b) {
Unsafe.arrayPut(buffer, a, Unsafe.arrayGet(buffer, b));
Unsafe.arrayPut(buffer, a, buffer[b]);
}
private int scanSearch(long v) {
......
......@@ -64,7 +64,7 @@ public class LongLongHashMap extends AbstractLongHashSet {
}
public long valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noEntryValue;
return index < 0 ? values[-index - 1] : noEntryValue;
}
@Override
......@@ -74,8 +74,8 @@ public class LongLongHashMap extends AbstractLongHashSet {
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
......@@ -94,11 +94,11 @@ public class LongLongHashMap extends AbstractLongHashSet {
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
long key = Unsafe.arrayGet(oldKeys, i);
long key = oldKeys[i];
if (key != noEntryKeyValue) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -85,11 +85,11 @@ public class LongMatrix<T> {
}
public long get(int r, int c) {
return Unsafe.arrayGet(data, offset(r, c));
return data[offset(r, c)];
}
public T get(int r) {
return Unsafe.arrayGet(payload, r);
return payload[r];
}
public void set(int r, T obj) {
......
......@@ -69,7 +69,7 @@ public class LowerCaseAsciiCharSequenceHashSet extends AbstractLowerCaseAsciiCha
}
public CharSequence keyAt(int index) {
return Unsafe.arrayGet(keys, -index - 1);
return keys[-index - 1];
}
@Override
......@@ -79,7 +79,7 @@ public class LowerCaseAsciiCharSequenceHashSet extends AbstractLowerCaseAsciiCha
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(keys, to, keys[from]);
erase(from);
}
......@@ -95,10 +95,9 @@ public class LowerCaseAsciiCharSequenceHashSet extends AbstractLowerCaseAsciiCha
this.keys = newKeys;
free -= size;
for (int i = 0, n = oldKeys.length; i < n; i++) {
CharSequence key = Unsafe.arrayGet(oldKeys, i);
CharSequence key = oldKeys[i];
if (key != null) {
int keyIndex = keyIndex(key);
Unsafe.arrayPut(keys, keyIndex, key);
Unsafe.arrayPut(keys, keyIndex(key), key);
}
}
}
......
......@@ -57,11 +57,8 @@ public class LowerCaseAsciiCharSequenceIntHashMap extends AbstractLowerCaseAscii
Unsafe.arrayPut(values, index, noEntryValue);
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
erase(from);
public int valueAt(int index) {
return index < 0 ? values[-index - 1] : noEntryValue;
}
public boolean contains(CharSequence key) {
......@@ -92,8 +89,11 @@ public class LowerCaseAsciiCharSequenceIntHashMap extends AbstractLowerCaseAscii
}
}
public int valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noEntryValue;
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
private void putAt0(int index, CharSequence key, int value) {
......@@ -119,11 +119,11 @@ public class LowerCaseAsciiCharSequenceIntHashMap extends AbstractLowerCaseAscii
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
CharSequence key = Unsafe.arrayGet(oldKeys, i);
CharSequence key = oldKeys[i];
if (key != null) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -58,11 +58,8 @@ public class LowerCaseAsciiCharSequenceObjHashMap<T> extends AbstractLowerCaseAs
Unsafe.arrayPut(values, index, null);
}
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(values, to, Unsafe.arrayGet(values, from));
erase(from);
public T valueAt(int index) {
return index < 0 ? values[-index - 1] : null;
}
public boolean contains(CharSequence key) {
......@@ -93,8 +90,11 @@ public class LowerCaseAsciiCharSequenceObjHashMap<T> extends AbstractLowerCaseAs
}
}
public T valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : null;
@Override
protected void move(int from, int to) {
Unsafe.arrayPut(keys, to, keys[from]);
Unsafe.arrayPut(values, to, values[from]);
erase(from);
}
private void putAt0(int index, CharSequence key, T value) {
......@@ -121,11 +121,11 @@ public class LowerCaseAsciiCharSequenceObjHashMap<T> extends AbstractLowerCaseAs
free -= size;
for (int i = oldKeys.length; i-- > 0; ) {
CharSequence key = Unsafe.arrayGet(oldKeys, i);
CharSequence key = oldKeys[i];
if (key != null) {
final int index = keyIndex(key);
Unsafe.arrayPut(keys, index, key);
Unsafe.arrayPut(values, index, Unsafe.arrayGet(oldValues, i));
Unsafe.arrayPut(values, index, oldValues[i]);
}
}
}
......
......@@ -305,11 +305,11 @@ public final class Numbers {
d = -value;
}
long factor = Unsafe.arrayGet(pow10, scale);
long factor = pow10[scale];
long scaled = (long) (d * factor + 0.5);
int targetScale = scale;
//noinspection StatementWithEmptyBody
while (++targetScale < 20 && Unsafe.arrayGet(pow10, targetScale) <= scaled) {
while (++targetScale < 20 && pow10[targetScale] <= scaled) {
}
// factor overflow, fallback to slow method rather than throwing exception
......@@ -318,7 +318,7 @@ public final class Numbers {
return;
}
factor = Unsafe.arrayGet(pow10, targetScale - 1);
factor = pow10[targetScale - 1];
while (targetScale > 0) {
if (targetScale-- == scale) {
sink.put('.');
......@@ -526,7 +526,8 @@ public final class Numbers {
return;
}
int bit = 64 - Long.numberOfLeadingZeros(value - 1);
Unsafe.arrayGet(pad ? longHexAppenderPad64 : longHexAppender, bit).append(sink, value);
LongHexAppender[] array = pad ? longHexAppenderPad64 : longHexAppender;
array[bit].append(sink, value);
}
private static void appendLongHex4(CharSink sink, long value) {
......@@ -921,7 +922,7 @@ public final class Numbers {
}
public static int hexToDecimal(int c) throws NumericException {
int r = Unsafe.arrayGet(hexNumbers, c);
int r = hexNumbers[c];
if (r == -1) {
throw NumericException.INSTANCE;
}
......@@ -1568,19 +1569,19 @@ public final class Numbers {
}
private static double roundHalfUp0(double value, int scale) {
long val = (long) (value * Unsafe.arrayGet(pow10, scale + 2) + TOLERANCE);
long val = (long) (value * pow10[scale + 2] + TOLERANCE);
return val % 100 < 50 ? roundDown0(value, scale) : roundUp0(value, scale);
}
private static double roundHalfEven0(double value, int scale) {
long val = (long) (value * Unsafe.arrayGet(pow10, scale + 2) + TOLERANCE);
long val = (long) (value * pow10[scale + 2] + TOLERANCE);
long remainder = val % 100;
if (remainder < 50) {
return roundDown0(value, scale);
}
if (remainder == 50 && ((long) (value * Unsafe.arrayGet(pow10, scale)) & 1) == 0) {
if (remainder == 50 && ((long) (value * pow10[scale]) & 1) == 0) {
return roundDown0(value, scale);
}
......@@ -1588,7 +1589,7 @@ public final class Numbers {
}
private static double roundHalfDown0(double value, int scale) {
long val = (long) (value * Unsafe.arrayGet(pow10, scale + 2) + TOLERANCE);
long val = (long) (value * pow10[scale + 2] + TOLERANCE);
return val % 100 > 50 ? roundUp0(value, scale) : roundDown0(value, scale);
}
......@@ -1601,7 +1602,7 @@ public final class Numbers {
}
private static double roundUp00(double value, int scale) {
long powten = Unsafe.arrayGet(pow10, scale);
long powten = pow10[scale];
return ((double) (long) (value * powten + 1 - TOLERANCE)) / powten;
}
......@@ -1609,7 +1610,7 @@ public final class Numbers {
//////////////////////
private static double roundDown00(double value, int scale) {
long powten = Unsafe.arrayGet(pow10, scale);
long powten = pow10[scale];
return ((double) (long) (value * powten + TOLERANCE)) / powten;
}
......
......@@ -107,12 +107,27 @@ public class ObjHashSet<T> extends AbstractSet<T> implements Mutable {
return addAt(keyIndex(key), key);
}
public int keyIndex(T key) {
int index = idx(key);
T kv = keys[index];
if (kv == noEntryKey) {
return index;
}
if (kv == key || key.equals(kv)) {
return -index - 1;
}
return probe(key, index);
}
@Override
@SuppressWarnings("unchecked")
public boolean remove(Object key) {
int keyIndex = keyIndex((T) key);
if (keyIndex < 0) {
list.remove(Unsafe.arrayGet(keys, -keyIndex - 1));
list.remove(keys[-keyIndex - 1]);
removeAt(keyIndex);
return true;
}
......@@ -130,20 +145,6 @@ public class ObjHashSet<T> extends AbstractSet<T> implements Mutable {
return list.toString();
}
public int keyIndex(T key) {
int index = idx(key);
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
return index;
}
if (Unsafe.arrayGet(keys, index) == key || key.equals(Unsafe.arrayGet(keys, index))) {
return -index - 1;
}
return probe(key, index);
}
private boolean addAt0(int index, T key) {
if (index > -1) {
Unsafe.arrayPut(keys, index, key);
......@@ -164,17 +165,18 @@ public class ObjHashSet<T> extends AbstractSet<T> implements Mutable {
}
private void move(int from, int to) {
Unsafe.arrayPut(keys, to, Unsafe.arrayGet(keys, from));
Unsafe.arrayPut(keys, to, keys[from]);
erase(from);
}
private int probe(T key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryKey) {
final T kv = keys[index];
if (kv == noEntryKey) {
return index;
}
if (Unsafe.arrayGet(keys, index) == key || key.equals(Unsafe.arrayGet(keys, index))) {
if (kv == key || key.equals(kv)) {
return -index - 1;
}
} while (true);
......@@ -191,8 +193,8 @@ public class ObjHashSet<T> extends AbstractSet<T> implements Mutable {
Arrays.fill(keys, noEntryKey);
for (int i = oldKeys.length; i-- > 0; ) {
if (Unsafe.arrayGet(oldKeys, i) != noEntryKey) {
T key = Unsafe.arrayGet(oldKeys, i);
T key = oldKeys[i];
if (key != noEntryKey) {
addAt0(keyIndex(key), key);
}
}
......@@ -213,14 +215,14 @@ public class ObjHashSet<T> extends AbstractSet<T> implements Mutable {
// After slot if freed these keys require re-hash
from = (from + 1) & mask;
for (
T key = Unsafe.arrayGet(keys, from);
T key = keys[from];
key != noEntryKey;
from = (from + 1) & mask, key = Unsafe.arrayGet(keys, from)
from = (from + 1) & mask, key = keys[from]
) {
int idealHit = key.hashCode() & mask;
if (idealHit != from) {
int to;
if (Unsafe.arrayGet(keys, idealHit) != noEntryKey) {
if (keys[idealHit] != noEntryKey) {
to = probe(key, idealHit);
} else {
to = idealHit;
......
......@@ -80,11 +80,12 @@ public class ObjIntHashMap<K> implements Iterable<ObjIntHashMap.Entry<K>>, Mutab
public int keyIndex(K key) {
int index = key.hashCode() & mask;
if (Unsafe.arrayGet(keys, index) == noEntryValue) {
final K kv = keys[index];
if (kv == noEntryValue) {
return index;
}
if (Unsafe.arrayGet(keys, index) == key || key.equals(Unsafe.arrayGet(keys, index))) {
if (kv == key || key.equals(kv)) {
return -index - 1;
}
......@@ -117,16 +118,18 @@ public class ObjIntHashMap<K> implements Iterable<ObjIntHashMap.Entry<K>>, Mutab
}
public int valueAt(int index) {
return index < 0 ? Unsafe.arrayGet(values, -index - 1) : noKeyValue;
int index1 = -index - 1;
return index < 0 ? values[index1] : noKeyValue;
}
private int probe(K key, int index) {
do {
index = (index + 1) & mask;
if (Unsafe.arrayGet(keys, index) == noEntryValue) {
final K kv = keys[index];
if (kv == noEntryValue) {
return index;
}
if (Unsafe.arrayGet(keys, index) == key || key.equals(Unsafe.arrayGet(keys, index))) {
if (kv == key || key.equals(kv)) {
return -index - 1;
}
} while (true);
......@@ -153,8 +156,8 @@ public class ObjIntHashMap<K> implements Iterable<ObjIntHashMap.Entry<K>>, Mutab
Arrays.fill(keys, noEntryValue);
for (int i = oldKeys.length; i-- > 0; ) {
if (Unsafe.arrayGet(oldKeys, i) != noEntryValue) {
put(Unsafe.arrayGet(oldKeys, i), Unsafe.arrayGet(oldValues, i));
if (oldKeys[i] != noEntryValue) {
put(oldKeys[i], oldValues[i]);
}
}
}
......@@ -171,20 +174,21 @@ public class ObjIntHashMap<K> implements Iterable<ObjIntHashMap.Entry<K>>, Mutab
@Override
public boolean hasNext() {
return index < values.length && (Unsafe.arrayGet(keys, index) != noEntryValue || scan());
return index < values.length && (keys[index] != noEntryValue || scan());
}
@Override
public Entry<K> next() {
entry.key = Unsafe.arrayGet(keys, index);
entry.value = Unsafe.arrayGet(values, index++);
entry.key = keys[index];
int index1 = index++;
entry.value = values[index1];
return entry;
}
private boolean scan() {
do {
index++;
} while (index < values.length && Unsafe.arrayGet(keys, index) == noEntryValue);
} while (index < values.length && keys[index] == noEntryValue);
return index < values.length;
}
......
......@@ -80,13 +80,13 @@ public class ObjList<T> implements Mutable, Sinkable {
*/
public T get(int index) {
if (index < pos) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
throw new ArrayIndexOutOfBoundsException(index);
}
public T getAndSetQuick(int index, T value) {
T v = Unsafe.arrayGet(buffer, index);
T v = buffer[index];
Unsafe.arrayPut(buffer, index, value);
return v;
}
......@@ -98,7 +98,7 @@ public class ObjList<T> implements Mutable, Sinkable {
*/
public T getLast() {
if (pos > 0) {
return Unsafe.arrayGet(buffer, pos - 1);
return buffer[pos - 1];
}
return null;
}
......@@ -113,8 +113,7 @@ public class ObjList<T> implements Mutable, Sinkable {
* @return element at the specified position.
*/
public T getQuick(int index) {
assert index < pos;
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
/**
......@@ -127,7 +126,7 @@ public class ObjList<T> implements Mutable, Sinkable {
*/
public T getQuiet(int index) {
if (index < pos) {
return Unsafe.arrayGet(buffer, index);
return buffer[index];
}
return null;
}
......
......@@ -42,51 +42,9 @@ public final class Unsafe {
private static final AtomicLong MALLOC_COUNT = new AtomicLong(0);
private static final AtomicLong FREE_COUNT = new AtomicLong(0);
static {
try {
Field theUnsafe = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
UNSAFE = (sun.misc.Unsafe) theUnsafe.get(null);
OBJ_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(Object[].class);
OBJ_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(Object[].class));
INT_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(int[].class);
INT_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(int[].class));
LONG_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(long[].class);
LONG_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(long[].class));
CHAR_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(char[].class);
CHAR_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(char[].class));
} catch (Exception e) {
throw new FatalError(e);
}
}
private Unsafe() {
}
@SuppressWarnings("unchecked")
public static <T> T arrayGet(T[] array, int index) {
assert index > -1 && index < array.length : "index=" + index + ", len=" + array.length;
return (T) Unsafe.getUnsafe().getObject(array, OBJ_OFFSET + (index << OBJ_SCALE));
}
public static int arrayGet(int[] array, int index) {
assert index > -1 && index < array.length;
return Unsafe.getUnsafe().getInt(array, INT_OFFSET + (index << INT_SCALE));
}
public static long arrayGet(long[] array, int index) {
assert index > -1 && index < array.length : "index: " + index + ", len: " + array.length;
return Unsafe.getUnsafe().getLong(array, LONG_OFFSET + (index << LONG_SCALE));
}
public static char arrayGet(char[] array, int index) {
assert index > -1 && index < array.length;
return UNSAFE.getChar(array, CHAR_OFFSET + (index << CHAR_SCALE));
}
public static long arrayGetVolatile(long[] array, int index) {
assert index > -1 && index < array.length;
return Unsafe.getUnsafe().getLongVolatile(array, LONG_OFFSET + (index << LONG_SCALE));
......@@ -117,6 +75,12 @@ public final class Unsafe {
Unsafe.getUnsafe().putOrderedLong(array, LONG_OFFSET + (index << LONG_SCALE), value);
}
public static long calloc(long size) {
long ptr = malloc(size);
getUnsafe().setMemory(ptr, size, (byte) 0);
return ptr;
}
public static boolean cas(Object o, long offset, long expected, long value) {
return UNSAFE.compareAndSwapLong(o, offset, expected, value);
}
......@@ -183,13 +147,28 @@ public final class Unsafe {
MEM_USED.addAndGet(size);
}
public static long calloc(long size) {
long ptr = malloc(size);
getUnsafe().setMemory(ptr, size, (byte) 0);
return ptr;
}
private static int msb(int value) {
return 31 - Integer.numberOfLeadingZeros(value);
}
static {
try {
Field theUnsafe = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
UNSAFE = (sun.misc.Unsafe) theUnsafe.get(null);
OBJ_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(Object[].class);
OBJ_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(Object[].class));
INT_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(int[].class);
INT_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(int[].class));
LONG_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(long[].class);
LONG_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(long[].class));
CHAR_OFFSET = Unsafe.getUnsafe().arrayBaseOffset(char[].class);
CHAR_SCALE = msb(Unsafe.getUnsafe().arrayIndexScale(char[].class));
} catch (Exception e) {
throw new FatalError(e);
}
}
}
......@@ -145,8 +145,8 @@ public class TimeZoneRulesImpl implements TimeZoneRules {
}
if ((index & 1) == 0) {
int offsetBefore = Unsafe.arrayGet(wallOffsets, index / 2);
int offsetAfter = Unsafe.arrayGet(wallOffsets, index / 2 + 1);
int offsetBefore = wallOffsets[index / 2];
int offsetAfter = wallOffsets[index / 2 + 1];
int delta = offsetAfter - offsetBefore;
if (delta > 0) {
......@@ -156,7 +156,7 @@ public class TimeZoneRulesImpl implements TimeZoneRules {
return offsetBefore * Timestamps.SECOND_MICROS;
}
} else {
return Unsafe.arrayGet(wallOffsets, index / 2 + 1) * Timestamps.SECOND_MICROS;
return wallOffsets[index / 2 + 1] * Timestamps.SECOND_MICROS;
}
}
......
......@@ -52,10 +52,8 @@ public class TimestampLocale {
indexZones(symbols.getZoneStrings(), timeZoneRuleFactory, cache);
}
private static void index(String[] tokens, IntObjHashMap<ObjList<CharSequence>> map) {
for (int i = 0, n = tokens.length; i < n; i++) {
defineToken(Unsafe.arrayGet(tokens, i), i, map);
}
public String getAMPM(int index) {
return ampmArray[index];
}
private static void defineToken(String token, int pos, IntObjHashMap<ObjList<CharSequence>> map) {
......@@ -110,32 +108,34 @@ public class TimestampLocale {
throw NumericException.INSTANCE;
}
public String getAMPM(int index) {
return Unsafe.arrayGet(ampmArray, index);
}
public String getEra(int index) {
return Unsafe.arrayGet(eraArray, index);
return eraArray[index];
}
public String getMonth(int index) {
return Unsafe.arrayGet(monthArray, index);
return monthArray[index];
}
public TimeZoneRules getRules(CharSequence timeZoneName) throws NumericException {
return getZoneRules(Numbers.decodeLowInt(matchZone(timeZoneName, 0, timeZoneName.length())));
public String getShortMonth(int index) {
return shortMonthArray[index];
}
public String getShortMonth(int index) {
return Unsafe.arrayGet(shortMonthArray, index);
public TimeZoneRules getRules(CharSequence timeZoneName) throws NumericException {
return getZoneRules(Numbers.decodeLowInt(matchZone(timeZoneName, 0, timeZoneName.length())));
}
public String getShortWeekday(int index) {
return Unsafe.arrayGet(shortWeekdayArray, index);
return shortWeekdayArray[index];
}
public String getWeekday(int index) {
return Unsafe.arrayGet(weekdayArray, index);
return weekdayArray[index];
}
private static void index(String[] tokens, IntObjHashMap<ObjList<CharSequence>> map) {
for (int i = 0, n = tokens.length; i < n; i++) {
defineToken(tokens[i], i, map);
}
}
public TimeZoneRules getZoneRules(int index) {
......
......@@ -23,10 +23,12 @@
package io.questdb.std.str;
import io.questdb.std.*;
import io.questdb.std.Misc;
import io.questdb.std.Numbers;
import io.questdb.std.ObjHashSet;
import io.questdb.std.Sinkable;
import io.questdb.std.microtime.DateFormatUtils;
import java.lang.ThreadLocal;
import java.util.Set;
public abstract class AbstractCharSink implements CharSink {
......@@ -111,13 +113,13 @@ public abstract class AbstractCharSink implements CharSink {
StackTraceElement[] trace = e.getStackTrace();
for (int i = 0, n = trace.length; i < n; i++) {
put(Unsafe.arrayGet(trace, i));
put(trace[i]);
}
// Print suppressed exceptions, if any
Throwable[] suppressed = e.getSuppressed();
for (int i = 0, n = suppressed.length; i < n; i++) {
put(Unsafe.arrayGet(suppressed, i), trace, "Suppressed: ", "\t", dejaVu);
put(suppressed[i], trace, "Suppressed: ", "\t", dejaVu);
}
// Print cause, if any
......@@ -224,7 +226,7 @@ public abstract class AbstractCharSink implements CharSink {
for (int i = 0; i <= m; i++) {
put(prefix);
put(Unsafe.arrayGet(trace, i));
put(trace[i]);
}
if (framesInCommon != 0) {
put(prefix).put("\t...").put(framesInCommon).put(" more");
......@@ -233,7 +235,7 @@ public abstract class AbstractCharSink implements CharSink {
// Print suppressed exceptions, if any
Throwable[] suppressed = throwable.getSuppressed();
for (int i = 0, k = suppressed.length; i < k; i++) {
put(Unsafe.arrayGet(suppressed, i), trace, "Suppressed: ", prefix + '\t', dejaVu);
put(suppressed[i], trace, "Suppressed: ", prefix + '\t', dejaVu);
}
// Print cause, if any
......
......@@ -52,10 +52,8 @@ public class DateLocale {
indexZones(symbols.getZoneStrings(), timeZoneRuleFactory, cache);
}
private static void index(String[] tokens, IntObjHashMap<ObjList<CharSequence>> map) {
for (int i = 0, n = tokens.length; i < n; i++) {
defineToken(Unsafe.arrayGet(tokens, i), i, map);
}
public String getAMPM(int index) {
return ampmArray[index];
}
private static void defineToken(String token, int pos, IntObjHashMap<ObjList<CharSequence>> map) {
......@@ -110,32 +108,34 @@ public class DateLocale {
throw NumericException.INSTANCE;
}
public String getAMPM(int index) {
return Unsafe.arrayGet(ampmArray, index);
}
public String getEra(int index) {
return Unsafe.arrayGet(eraArray, index);
return eraArray[index];
}
public String getMonth(int index) {
return Unsafe.arrayGet(monthArray, index);
return monthArray[index];
}
public TimeZoneRules getRules(CharSequence timeZoneName) throws NumericException {
return getZoneRules(Numbers.decodeLowInt(matchZone(timeZoneName, 0, timeZoneName.length())));
public String getShortMonth(int index) {
return shortMonthArray[index];
}
public String getShortMonth(int index) {
return Unsafe.arrayGet(shortMonthArray, index);
public TimeZoneRules getRules(CharSequence timeZoneName) throws NumericException {
return getZoneRules(Numbers.decodeLowInt(matchZone(timeZoneName, 0, timeZoneName.length())));
}
public String getShortWeekday(int index) {
return Unsafe.arrayGet(shortWeekdayArray, index);
return shortWeekdayArray[index];
}
public String getWeekday(int index) {
return Unsafe.arrayGet(weekdayArray, index);
return weekdayArray[index];
}
private static void index(String[] tokens, IntObjHashMap<ObjList<CharSequence>> map) {
for (int i = 0, n = tokens.length; i < n; i++) {
defineToken(tokens[i], i, map);
}
}
public TimeZoneRules getZoneRules(int index) {
......
......@@ -145,8 +145,8 @@ public class TimeZoneRulesImpl implements TimeZoneRules {
}
if ((index & 1) == 0) {
int offsetBefore = Unsafe.arrayGet(wallOffsets, index / 2);
int offsetAfter = Unsafe.arrayGet(wallOffsets, index / 2 + 1);
int offsetBefore = wallOffsets[index / 2];
int offsetAfter = wallOffsets[index / 2 + 1];
int delta = offsetAfter - offsetBefore;
if (delta > 0) {
......@@ -156,7 +156,7 @@ public class TimeZoneRulesImpl implements TimeZoneRules {
return offsetBefore * Dates.SECOND_MILLIS;
}
} else {
return Unsafe.arrayGet(wallOffsets, index / 2 + 1) * Dates.SECOND_MILLIS;
return wallOffsets[index / 2 + 1] * Dates.SECOND_MILLIS;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册