-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-10399][SPARK-23879][CORE][SQL] Introduce multiple MemoryBlocks to choose several types of memory block #19222
Changes from all commits
f427ca2
5d7ccdb
251fa09
790bbe7
93a792e
0beab03
fcf764c
d2d2e50
f5e10bb
1905e8c
7778e58
4f96c82
d1d6ae9
914dcd1
336e4b7
5be9ccb
43e6b57
05f024e
9071cf6
37ee9fa
d0b5d59
5cdad44
91028fa
0210bd1
df6dad3
1fa47a8
01f9c8e
2ed8f82
5e3afd1
95fbdee
9b4975b
9e2697c
8cd4853
1bed048
c79585f
77cdb81
ee5a798
c9f401a
cf2d532
eb0cc6d
6f57994
3a93d61
abf6ba0
4567781
95ffd0f
9cbb876
291203c
a62770b
f9bc4d6
c6d45ea
5284593
b1750a1
38ddf48
4e46a18
511d58d
1939fda
cb4b30b
c53b6b8
45aa736
8690e43
59fd393
b69cb64
94c9648
e4a7016
3d03f92
50326ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
|
||
package org.apache.spark.unsafe.array; | ||
|
||
import org.apache.spark.unsafe.Platform; | ||
import org.apache.spark.unsafe.memory.MemoryBlock; | ||
|
||
/** | ||
|
@@ -33,16 +32,12 @@ public final class LongArray { | |
private static final long WIDTH = 8; | ||
|
||
private final MemoryBlock memory; | ||
private final Object baseObj; | ||
private final long baseOffset; | ||
|
||
private final long length; | ||
|
||
public LongArray(MemoryBlock memory) { | ||
assert memory.size() < (long) Integer.MAX_VALUE * 8: "Array size >= Integer.MAX_VALUE elements"; | ||
this.memory = memory; | ||
this.baseObj = memory.getBaseObject(); | ||
this.baseOffset = memory.getBaseOffset(); | ||
this.length = memory.size() / WIDTH; | ||
} | ||
|
||
|
@@ -51,11 +46,11 @@ public MemoryBlock memoryBlock() { | |
} | ||
|
||
public Object getBaseObject() { | ||
return baseObj; | ||
return memory.getBaseObject(); | ||
} | ||
|
||
public long getBaseOffset() { | ||
return baseOffset; | ||
return memory.getBaseOffset(); | ||
} | ||
|
||
/** | ||
|
@@ -69,8 +64,8 @@ public long size() { | |
* Fill this all with 0L. | ||
*/ | ||
public void zeroOut() { | ||
for (long off = baseOffset; off < baseOffset + length * WIDTH; off += WIDTH) { | ||
Platform.putLong(baseObj, off, 0); | ||
for (long off = 0; off < length * WIDTH; off += WIDTH) { | ||
memory.putLong(off, 0); | ||
} | ||
} | ||
|
||
|
@@ -80,7 +75,7 @@ public void zeroOut() { | |
public void set(int index, long value) { | ||
assert index >= 0 : "index (" + index + ") should >= 0"; | ||
assert index < length : "index (" + index + ") should < length (" + length + ")"; | ||
Platform.putLong(baseObj, baseOffset + index * WIDTH, value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update it to use 0-based offset. |
||
memory.putLong(index * WIDTH, value); | ||
} | ||
|
||
/** | ||
|
@@ -89,6 +84,6 @@ public void set(int index, long value) { | |
public long get(int index) { | ||
assert index >= 0 : "index (" + index + ") should >= 0"; | ||
assert index < length : "index (" + index + ") should < length (" + length + ")"; | ||
return Platform.getLong(baseObj, baseOffset + index * WIDTH); | ||
return memory.getLong(index * WIDTH); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.unsafe.memory; | ||
|
||
import com.google.common.primitives.Ints; | ||
|
||
import org.apache.spark.unsafe.Platform; | ||
|
||
/** | ||
* A consecutive block of memory with a byte array on Java heap. | ||
*/ | ||
public final class ByteArrayMemoryBlock extends MemoryBlock { | ||
|
||
private final byte[] array; | ||
|
||
public ByteArrayMemoryBlock(byte[] obj, long offset, long size) { | ||
super(obj, offset, size); | ||
this.array = obj; | ||
assert(offset + size <= Platform.BYTE_ARRAY_OFFSET + obj.length) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we require There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I agree with you. However, to add this assertion causes a failure (assertion error at here) at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, from here and the accessors below, looks like the given |
||
"The sum of size " + size + " and offset " + offset + " should not be larger than " + | ||
"the size of the given memory space " + (obj.length + Platform.BYTE_ARRAY_OFFSET); | ||
} | ||
|
||
public ByteArrayMemoryBlock(long length) { | ||
this(new byte[Ints.checkedCast(length)], Platform.BYTE_ARRAY_OFFSET, length); | ||
} | ||
|
||
@Override | ||
public MemoryBlock subBlock(long offset, long size) { | ||
checkSubBlockRange(offset, size); | ||
if (offset == 0 && size == this.size()) return this; | ||
return new ByteArrayMemoryBlock(array, this.offset + offset, size); | ||
} | ||
|
||
public byte[] getByteArray() { return array; } | ||
|
||
/** | ||
* Creates a memory block pointing to the memory used by the byte array. | ||
*/ | ||
public static ByteArrayMemoryBlock fromArray(final byte[] array) { | ||
return new ByteArrayMemoryBlock(array, Platform.BYTE_ARRAY_OFFSET, array.length); | ||
} | ||
|
||
@Override | ||
public final int getInt(long offset) { | ||
return Platform.getInt(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putInt(long offset, int value) { | ||
Platform.putInt(array, this.offset + offset, value); | ||
} | ||
|
||
@Override | ||
public final boolean getBoolean(long offset) { | ||
return Platform.getBoolean(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putBoolean(long offset, boolean value) { | ||
Platform.putBoolean(array, this.offset + offset, value); | ||
} | ||
|
||
@Override | ||
public final byte getByte(long offset) { | ||
return array[(int)(this.offset + offset - Platform.BYTE_ARRAY_OFFSET)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion from @cloud-fan. The Java array access is faster than |
||
} | ||
|
||
@Override | ||
public final void putByte(long offset, byte value) { | ||
array[(int)(this.offset + offset - Platform.BYTE_ARRAY_OFFSET)] = value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
@Override | ||
public final short getShort(long offset) { | ||
return Platform.getShort(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putShort(long offset, short value) { | ||
Platform.putShort(array, this.offset + offset, value); | ||
} | ||
|
||
@Override | ||
public final long getLong(long offset) { | ||
return Platform.getLong(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putLong(long offset, long value) { | ||
Platform.putLong(array, this.offset + offset, value); | ||
} | ||
|
||
@Override | ||
public final float getFloat(long offset) { | ||
return Platform.getFloat(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putFloat(long offset, float value) { | ||
Platform.putFloat(array, this.offset + offset, value); | ||
} | ||
|
||
@Override | ||
public final double getDouble(long offset) { | ||
return Platform.getDouble(array, this.offset + offset); | ||
} | ||
|
||
@Override | ||
public final void putDouble(long offset, double value) { | ||
Platform.putDouble(array, this.offset + offset, value); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no one is calling it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is called from
UTF8String
class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this only works for
ByteArrayMemoryBlock
, doesn't it?