/** * Create a pooled ByteBuf allocator but disables the thread-local cache. Thread-local caches * are disabled for TransportClients because the ByteBufs are allocated by the event loop thread, * but released by the executor thread rather than the event loop thread. Those thread-local * caches actually delay the recycling of buffers, leading to larger memory usage. */ public static PooledByteBufAllocator createPooledByteBufAllocator( boolean allowDirectBufs, boolean allowCache, int numCores) { if (numCores == 0) { numCores = Runtime.getRuntime().availableProcessors(); } return new PooledByteBufAllocator( allowDirectBufs && PlatformDependent.directBufferPreferred(), Math.min(getPrivateStaticField("DEFAULT_NUM_HEAP_ARENA"), numCores), Math.min(getPrivateStaticField("DEFAULT_NUM_DIRECT_ARENA"), allowDirectBufs ? numCores : 0), getPrivateStaticField("DEFAULT_PAGE_SIZE"), getPrivateStaticField("DEFAULT_MAX_ORDER"), allowCache ? getPrivateStaticField("DEFAULT_TINY_CACHE_SIZE") : 0, allowCache ? getPrivateStaticField("DEFAULT_SMALL_CACHE_SIZE") : 0, allowCache ? getPrivateStaticField("DEFAULT_NORMAL_CACHE_SIZE") : 0 ); }
public void ensureAvailableBlocks(int count){ if(count > capacity){ final int blockWidth = this.blockWidth; final int sizeInBytes = Numbers.nextPowerOfTwo(count * blockWidth); final ArrowBuf oldBuf = buf; buf = allocator.buffer(sizeInBytes); // since blockWidth has to be a power of two and count final long firstBlock = buf.memoryAddress() + (capacity * blockWidth); final int maxBytes = blockWidth * count; final long maxBlock = buf.memoryAddress() + maxBytes; for(long l = firstBlock; l < maxBlock; l+= 8){ PlatformDependent.putLong(l, 0); } int remain = maxBytes % 8; if(remain != 0){ buf.setZero(maxBytes - remain, remain); } PlatformDependent.copyMemory(oldBuf.memoryAddress(), buf.memoryAddress(), capacity * blockWidth); oldBuf.release(); this.capacity = count; } }
public static final void handCopy(final long src, final long dst, int len) { int n = len; long lPos = src; long rPos = dst; while (n > 7) { PlatformDependent.putLong(rPos, PlatformDependent.getLong(lPos)); lPos += 8; rPos += 8; n -= 8; } while (n > 3) { PlatformDependent.putInt(rPos, PlatformDependent.getInt(lPos)); lPos += 4; rPos += 4; n -= 4; } while (n-- != 0) { PlatformDependent.putByte(rPos, PlatformDependent.getByte(lPos)); lPos++; rPos++; } }
private static final int memcmp(final long laddr, int lStart, int lEnd, final byte[] right, int rStart, final int rEnd) { int lLen = lEnd - lStart; int rLen = rEnd - rStart; int n = Math.min(rLen, lLen); long lPos = laddr + lStart; int rPos = rStart; while (n-- != 0) { byte leftByte = PlatformDependent.getByte(lPos); byte rightByte = right[rPos]; if (leftByte != rightByte) { return ((leftByte & 0xFF) - (rightByte & 0xFF)) > 0 ? 1 : -1; } lPos++; rPos++; } if (lLen == rLen) { return 0; } return lLen > rLen ? 1 : -1; }
@Override public void finishRecord() { this.recordStart = characterData; if(fieldOpen){ endField(); } if(repeatedOffset >= repeatedOffsetMax){ expandRepeatedOffsets(); } int newOffset = ((int) (charLengthOffset - charLengthOffsetOriginal))/4; PlatformDependent.putInt(repeatedOffset, newOffset); repeatedOffset += 4; // if there were no defined fields, skip. if(fieldIndex > -1){ batchIndex++; recordCount++; } }
@Override public void copy(long offsetAddr, int count) { if(allocateAsFixed){ targetAlt.allocateNew(count); } final long srcAddr = source.getFieldBuffers().get(bufferOrdinal).memoryAddress(); final long dstAddr = target.getFieldBuffers().get(bufferOrdinal).memoryAddress(); final long maxAddr = offsetAddr + count * STEP_SIZE; int targetIndex = 0; for(; offsetAddr < maxAddr; offsetAddr += STEP_SIZE, targetIndex++){ final int recordIndex = (char) PlatformDependent.getShort(offsetAddr); final int byteValue = PlatformDependent.getByte(srcAddr + (recordIndex >>> 3)); final int bitVal = ((byteValue >>> (recordIndex & 7)) & 1) << (targetIndex & 7); final long addr = dstAddr + (targetIndex >>> 3); PlatformDependent.putByte(addr, (byte) (PlatformDependent.getByte(addr) | bitVal)); } }
public ByteBuf setBytes(int index, ByteBuffer src, int srcIndex, int length) { if (src.isDirect()) { checkIndex(index, length); PlatformDependent.copyMemory(PlatformDependent.directBufferAddress(src) + srcIndex, this.memoryAddress() + index, length); } else { if (srcIndex == 0 && src.capacity() == length) { b.setBytes(index + offset, src); } else { ByteBuffer newBuf = src.duplicate(); newBuf.position(srcIndex); newBuf.limit(srcIndex + length); b.setBytes(index + offset, src); } } return this; }
@Override protected int doReadMessages(List<Object> buf) throws Exception { DatagramChannel ch = javaChannel(); ChannelConfig config = config(); RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf data = allocHandle.allocate(config.getAllocator()); allocHandle.attemptedBytesRead(data.writableBytes()); boolean free = true; try { ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes()); int pos = nioData.position(); int read = ch.read(nioData); if (read <= 0) { return read; } allocHandle.lastBytesRead(nioData.position() - pos); buf.add(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead())); free = false; return 1; } catch (Throwable cause) { PlatformDependent.throwException(cause); return -1; } finally { if (free) { data.release(); } } }
@Override protected int doReadMessages(List<Object> buf) throws Exception { DatagramChannel ch = javaChannel(); UkcpServerChannelConfig config = config(); RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf data = allocHandle.allocate(config.getAllocator()); allocHandle.attemptedBytesRead(data.writableBytes()); boolean free = true; try { ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes()); int pos = nioData.position(); InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData); if (remoteAddress == null) { return 0; } allocHandle.lastBytesRead(nioData.position() - pos); buf.add(UkcpPacket.newInstance(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), remoteAddress)); free = false; return 1; } catch (Throwable cause) { PlatformDependent.throwException(cause); return -1; } finally { if (free) { data.release(); } } }
/** * Checks to see if we can go over the end of our bytes constraint on the data. If so, * adjusts so that we can only read to the last character of the first line that crosses * the split boundary. */ private void updateLengthBasedOnConstraint() { final long max = bStart + length; for(long m = bStart + (endPos - streamPos); m < max; m++) { for (int i = 0; i < lineSeparator.length; i++) { long mPlus = m + i; if (mPlus < max) { // we found a line separator and don't need to consult the next byte. if (lineSeparator[i] == PlatformDependent.getByte(mPlus) && i == lineSeparator.length - 1) { length = (int) (mPlus - bStart) + 1; endFound = true; return; } } else { // the last N characters of the read were remnant bytes. We'll hold off on dealing with these bytes until the next read. remByte = i; length = length - i; return; } } } }
private final boolean checkBom(ByteOrderMark bom) { int bomLength = bom.length(); if (bufferPtr + bomLength >= length) { // Not enough bytes from the current position to the end of the buffer return false; } if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { buffer.checkBytes(bufferPtr - 1, bufferPtr + bomLength); } byte[] bomBytes = bom.getBytes(); for (int i = 0; i < bomLength; i++) { byte nextChar = PlatformDependent.getByte(bStartMinus1 + bufferPtr + i); if (nextChar != bomBytes[i]) { // No BOM. Position is unchanged return false; } } return true; }
public void accumulate(final long memoryAddr, final int count) { final long maxMemAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomaxgBit = buffers.get(0).memoryAddress(); final long incomaxgValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomaxgIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxMemAddr; ordinalAddr += 4, incomaxgIndex++){ final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomaxgValue + (incomaxgIndex * WIDTH))); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * 4; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomaxgBit + ((incomaxgIndex >>> 3))) >>> (incomaxgIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putInt(maxAddr, Float.floatToIntBits(max(Float.intBitsToFloat(PlatformDependent.getInt(maxAddr)), newVal, bitVal))); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxMemAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomaxgBit = buffers.get(0).memoryAddress(); final long incomaxgValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomaxgIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxMemAddr; ordinalAddr += 4, incomaxgIndex++){ final long newVal = PlatformDependent.getLong(incomaxgValue + (incomaxgIndex * WIDTH)); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomaxgBit + ((incomaxgIndex >>> 3))) >>> (incomaxgIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(maxAddr, max(PlatformDependent.getLong(maxAddr), newVal, bitVal)); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxMemAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomaxgBit = buffers.get(0).memoryAddress(); final long incomaxgValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomaxgIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxMemAddr; ordinalAddr += 4, incomaxgIndex++){ final double newVal = Double.longBitsToDouble(PlatformDependent.getLong(incomaxgValue + (incomaxgIndex * WIDTH))); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomaxgBit + ((incomaxgIndex >>> 3))) >>> (incomaxgIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(maxAddr, Double.doubleToLongBits(max(Double.longBitsToDouble(PlatformDependent.getLong(maxAddr)), newVal, bitVal))); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int newVal = PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH)) * bitVal; final int tableIndex = PlatformDependent.getInt(ordinalAddr); final long sumAddr = valueAddresses[tableIndex >>> LBlockHashTable.BITS_IN_CHUNK] + (tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK) * 8; PlatformDependent.putLong(sumAddr, PlatformDependent.getLong(sumAddr) + newVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH)) * bitVal); final int tableIndex = PlatformDependent.getInt(ordinalAddr); final long sumAddr = valueAddresses[tableIndex >>> LBlockHashTable.BITS_IN_CHUNK] + (tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK) * 8; PlatformDependent.putLong(sumAddr, Double.doubleToLongBits(Double.longBitsToDouble(PlatformDependent.getLong(sumAddr)) + newVal)); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH)) * bitVal; final int tableIndex = PlatformDependent.getInt(ordinalAddr); final long sumAddr = valueAddresses[tableIndex >>> LBlockHashTable.BITS_IN_CHUNK] + (tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK) * 8; PlatformDependent.putLong(sumAddr, PlatformDependent.getLong(sumAddr) + newVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int newVal = PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH)); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * 4; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putInt(minAddr, min(PlatformDependent.getInt(minAddr), newVal, bitVal)); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH))); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * 4; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putInt(minAddr, Float.floatToIntBits(min(Float.intBitsToFloat(PlatformDependent.getInt(minAddr)), newVal, bitVal))); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH)); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(minAddr, min(PlatformDependent.getLong(minAddr), newVal, bitVal)); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final double newVal = Double.longBitsToDouble(PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH))); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(minAddr, Double.doubleToLongBits(min(Double.longBitsToDouble(PlatformDependent.getLong(minAddr)), newVal, bitVal))); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final int newVal = PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH)) * bitVal; final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long sumAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(sumAddr, PlatformDependent.getLong(sumAddr) + newVal); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH)) * bitVal); final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long sumAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(sumAddr, Double.doubleToLongBits(Double.longBitsToDouble(PlatformDependent.getLong(sumAddr)) + newVal)); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * 4; List<ArrowBuf> buffers = getInput().getFieldBuffers(); final long incomingBit = buffers.get(0).memoryAddress(); final long incomingValue = buffers.get(1).memoryAddress(); final long[] bitAddresses = this.bitAddresses; final long[] valueAddresses = this.valueAddresses; int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){ final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH)) * bitVal; final int tableIndex = PlatformDependent.getInt(ordinalAddr); int chunkIndex = tableIndex >>> LBlockHashTable.BITS_IN_CHUNK; int chunkOffset = tableIndex & LBlockHashTable.CHUNK_OFFSET_MASK; final long sumAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8; final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4); final int bitUpdateVal = bitVal << (chunkOffset & 31); PlatformDependent.putLong(sumAddr, PlatformDependent.getLong(sumAddr) + newVal); PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal); } }
@Override public void copy(long compoundAddr, final int srcStart, final int count) { copyWatch.start(); final long[] dstAddrs = this.dstAddrs; long srcAddr = source.getFieldBuffers().get(VALUE_BUFFER_ORDINAL).memoryAddress() + srcStart * SIZE; final long max = compoundAddr + count * OFFSET_SIZE; for (; compoundAddr < max; compoundAddr +=OFFSET_SIZE, srcAddr += SIZE) { final int compoundIdx = PlatformDependent.getInt(compoundAddr); final int batchIdx = compoundIdx >>> 16; final int rowIdx = compoundIdx & 65535; PlatformDependent.putLong(dstAddrs[batchIdx] + rowIdx * SIZE, PlatformDependent.getLong(srcAddr)); } copyWatch.stop(); }
@Override public void copy(long compoundAddr, final int srcStart, final int count) { copyWatch.start(); final long[] dstAddrs = this.dstAddrs; long srcAddr = source.getFieldBuffers().get(VALUE_BUFFER_ORDINAL).memoryAddress() + srcStart * SIZE; final long max = compoundAddr + count * OFFSET_SIZE; for (; compoundAddr < max; compoundAddr +=OFFSET_SIZE, srcAddr += SIZE) { final int compoundIdx = PlatformDependent.getInt(compoundAddr); final int batchIdx = compoundIdx >>> 16; final int rowIdx = compoundIdx & 65535; final long dstAddr = dstAddrs[batchIdx] + rowIdx * SIZE; PlatformDependent.putLong(dstAddr, PlatformDependent.getLong(srcAddr)); PlatformDependent.putLong(dstAddr + 8, PlatformDependent.getLong(srcAddr + 8)); } copyWatch.stop(); }
@Override public void insert(long findAddr, int records) { try(FixedBlockVector fbv = new FixedBlockVector(allocator, buildPivot.getBlockWidth()); VariableBlockVector var = new VariableBlockVector(allocator, buildPivot.getVariableCount()); ){ // first we pivot. pivotBuildWatch.start(); Pivots.pivot(buildPivot, records, fbv, var); pivotBuildWatch.stop(); final long keyFixedAddr = fbv.getMemoryAddress(); final long keyVarAddr = var.getMemoryAddress(); try(ArrowBuf offsets = allocator.buffer(records * 4)){ insertWatch.start(); for(int i =0 ; i < records; i++, findAddr += 4){ PlatformDependent.putInt(findAddr, table.add(keyFixedAddr, keyVarAddr, i)); } insertWatch.stop(); } } }
@Override public void copy(long offsetAddr, int count) { targetAlt.allocateNew(count); final List<ArrowBuf> targetBuffers = target.getFieldBuffers(); final long max = offsetAddr + count * BUILD_RECORD_LINK_SIZE; final long[] srcAddrs = this.srcAddrs; long dstAddr = targetBuffers.get(VALUE_BUFFER_ORDINAL).memoryAddress(); for(long addr = offsetAddr; addr < max; addr += BUILD_RECORD_LINK_SIZE, dstAddr += SIZE){ final int batchIndex = PlatformDependent.getInt(addr); if(batchIndex != SKIP){ final int batchOffset = PlatformDependent.getShort(addr + 4); final long srcAddr = srcAddrs[batchIndex] + batchOffset * SIZE; PlatformDependent.putInt(dstAddr, PlatformDependent.getInt(srcAddr)); } } }
public static PooledByteBufAllocator createPooledByteBufAllocator(boolean allowDirectBufs, boolean allowCache, int numCores) { if (numCores == 0) { numCores = Runtime.getRuntime().availableProcessors(); } return new PooledByteBufAllocator(allowDirectBufs && PlatformDependent.directBufferPreferred(), Math.min(getPrivateStaticField("DEFAULT_NUM_HEAP_ARENA"), numCores), Math.min( getPrivateStaticField("DEFAULT_NUM_DIRECT_ARENA"), allowDirectBufs ? numCores : 0), getPrivateStaticField("DEFAULT_PAGE_SIZE"), getPrivateStaticField("DEFAULT_MAX_ORDER"), allowCache ? getPrivateStaticField("DEFAULT_TINY_CACHE_SIZE") : 0, allowCache ? getPrivateStaticField("DEFAULT_SMALL_CACHE_SIZE") : 0, allowCache ? getPrivateStaticField("DEFAULT_NORMAL_CACHE_SIZE") : 0); }
@Override public Object decode(ByteBuf buf, State state) throws IOException { List<Object> result = new ArrayList<Object>(); int keyLen; if (PlatformDependent.isWindows()) { keyLen = buf.readIntLE(); } else { keyLen = (int) buf.readLongLE(); } ByteBuf keyBuf = buf.readSlice(keyLen); Object key = codec.getMapKeyDecoder().decode(keyBuf, state); result.add(key); int valueLen; if (PlatformDependent.isWindows()) { valueLen = buf.readIntLE(); } else { valueLen = (int) buf.readLongLE(); } ByteBuf valueBuf = buf.readSlice(valueLen); Object value = codec.getMapValueDecoder().decode(valueBuf, state); result.add(value); if (sync) { double syncId = buf.order(ByteOrder.LITTLE_ENDIAN).readDouble(); result.add(syncId); } return result; }
public static void unpivotBytes4(final long srcFixedAddr, final int blockWidth, final long target, final int byteOffset, int count) { final long startAddr = srcFixedAddr; long maxAddr = startAddr + (count * blockWidth); long targetAddr = target; for(long srcAddr = startAddr; srcAddr < maxAddr; srcAddr += blockWidth, targetAddr+=4){ final int value = PlatformDependent.getInt(srcAddr + byteOffset); PlatformDependent.putInt(targetAddr, value); } }
@Test public void get() throws Exception { List<String> nodeList = new ArrayList<>(); Map<String, List<String>> mm = PlatformDependent.newConcurrentHashMap(); for (int i = 0; i < 100; i++) { nodeList.add(("Host" + i)); mm.put(("Host" + i), new ArrayList<>()); } RendezvousHash rendezvousHash = new RendezvousHash(Funnels.stringFunnel(Charset.defaultCharset()), nodeList); Random r = new Random(); for (int i = 0; i < 100000; i++) { String thing = (Integer.toString(r.nextInt(123456789))); List<String> hosts = rendezvousHash.get(thing.getBytes(), 3); hosts.forEach( xs -> { mm.get(xs).add(thing); }); } List<Integer> xx = new ArrayList<>(); mm.keySet() .forEach( xs -> { xx.add(mm.get(xs).size()); }); Double xd = xx.stream().mapToInt(x -> x).average().orElse(-1); assertEquals(3000, xd.intValue()); }
private static final int memcmp(final long laddr, int lStart, int lEnd, final long raddr, int rStart, final int rEnd) { int lLen = lEnd - lStart; int rLen = rEnd - rStart; int n = Math.min(rLen, lLen); long lPos = laddr + lStart; long rPos = raddr + rStart; while (n > 7) { long leftLong = PlatformDependent.getLong(lPos); long rightLong = PlatformDependent.getLong(rPos); if (leftLong != rightLong) { return UnsignedLongs.compare(Long.reverseBytes(leftLong), Long.reverseBytes(rightLong)); } lPos += 8; rPos += 8; n -= 8; } while (n-- != 0) { byte leftByte = PlatformDependent.getByte(lPos); byte rightByte = PlatformDependent.getByte(rPos); if (leftByte != rightByte) { return ((leftByte & 0xFF) - (rightByte & 0xFF)) > 0 ? 1 : -1; } lPos++; rPos++; } if (lLen == rLen) { return 0; } return lLen > rLen ? 1 : -1; }
@Override public void copy(long offsetAddr, int count) { dst.allocateNew(); final long max = offsetAddr + count * STEP_SIZE; int target = 0; for(long addr = offsetAddr; addr < max; addr += STEP_SIZE) { int index = (char) PlatformDependent.getShort(addr); transfer.copyValueSafe(index, target); target++; } }
@Override public boolean endField() { fieldOpen = false; if(charLengthOffset >= charLengthOffsetMax){ expandVarCharOffsets(); } int newOffset = (int) (characterData - characterDataOriginal); PlatformDependent.putInt(charLengthOffset, newOffset); charLengthOffset += 4; return fieldIndex < maxField; }
@Override public void append(byte data) { if(!collect){ return; } if(characterData >= characterDataMax){ expandVarCharData(); } PlatformDependent.putByte(characterData, data); characterData++; }
private void internalInit(final int capacity) { initTimer.start(); this.capacity = capacity; this.maxSize = !QHashCapacities.isMaxCapacity(capacity, false) ? config.maxSize(capacity) : capacity - 1; this.batches = (int) Math.ceil( capacity / (MAX_VALUES_PER_BATCH * 1.0d) ); final ControlBlock[] newControlBlocks = new ControlBlock[batches]; tableControlAddresses = new long[batches]; try(RollbackCloseable rollbackable = new RollbackCloseable()) { for(int i =0; i < batches; i++){ newControlBlocks[i] = new ControlBlock(allocator); rollbackable.add(newControlBlocks[i]); tableControlAddresses[i] = newControlBlocks[i].getMemoryAddress(); final long addr = newControlBlocks[i].getMemoryAddress(); final long max = addr + MAX_VALUES_PER_BATCH * CONTROL_WIDTH; for(long l = addr; l < max; l+= QBlockHashTable.CONTROL_WIDTH){ PlatformDependent.putLong(l, QBlockHashTable.LFREE); } } this.controlBlocks = newControlBlocks; rollbackable.commit(); } catch (Exception e) { throw Throwables.propagate(e); } initTimer.stop(); }
@Override public void copy(long offsetAddr, int count) { dst.allocateNew(); final long max = offsetAddr + count * BUILD_RECORD_LINK_SIZE; int target = 0; for(long addr = offsetAddr; addr < max; addr += BUILD_RECORD_LINK_SIZE) { final int batchIndex = PlatformDependent.getInt(addr); final int batchOffset = PlatformDependent.getShort(addr + 4); transfer[batchIndex].copyValueSafe(batchOffset, target); target++; } }
@Override public ByteBuf writeShort(int value) { ensure(2); PlatformDependent.putShort(addr(writerIndex), (short) value); writerIndex += 2; return this; }
@Override public ByteBuf writeInt(int value) { ensure(4); PlatformDependent.putInt(addr(writerIndex), value); writerIndex += 4; return this; }