Repository navigation
feat(java): add retained memory estimates #1161
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
base: develop
Are you sure you want to change the base?
Changes from all commits
1814de8
632677e
6c3d421
822a2f0
30368f7
a6fe2b1
7b95a66
4ff096c
b00cc56
5080ffd
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.apache.tsfile.file.metadata.enums.TSEncoding; | ||
| import org.apache.tsfile.i18n.Messages; | ||
| import org.apache.tsfile.utils.Binary; | ||
| import org.apache.tsfile.utils.RamUsageEstimator; | ||
| import org.apache.tsfile.utils.ReadWriteForEncodingUtils; | ||
|
|
||
| import org.slf4j.Logger; | ||
|
|
@@ -44,12 +45,17 @@ | |
| * }</pre> | ||
| */ | ||
| public class DictionaryEncoder extends Encoder { | ||
|
|
||
| private static final long BOXED_INDEX_SIZE = | ||
| RamUsageEstimator.shallowSizeOfInstance(Integer.class); | ||
| private static final Logger logger = LoggerFactory.getLogger(DictionaryEncoder.class); | ||
|
|
||
| private HashMap<Binary, Integer> entryIndex; | ||
| private List<Binary> indexEntry; | ||
| private IntRleEncoder valuesEncoder; | ||
| private long mapSize; | ||
| private long dictionaryEntriesRetainedSize; | ||
| private int maximumDictionarySize; | ||
|
|
||
| public DictionaryEncoder() { | ||
| super(TSEncoding.DICTIONARY); | ||
|
|
@@ -66,9 +72,15 @@ public void encode(Binary value, ByteArrayOutputStream out) { | |
| entryIndex.computeIfAbsent( | ||
| value, | ||
| v -> { | ||
| int index = indexEntry.size(); | ||
| indexEntry.add(v); | ||
| mapSize += v.getLength(); | ||
| return entryIndex.size(); | ||
| dictionaryEntriesRetainedSize += v.ramBytesUsed(); | ||
| if (index > RamUsageEstimator.INTEGER_CACHE_MAX_VALUE) { | ||
| dictionaryEntriesRetainedSize += BOXED_INDEX_SIZE; | ||
| } | ||
| maximumDictionarySize = Math.max(maximumDictionarySize, indexEntry.size()); | ||
| return index; | ||
| }); | ||
| valuesEncoder.encode(i, out); | ||
| } | ||
|
|
@@ -96,6 +108,28 @@ public long getMaxByteSize() { | |
| return 4 + mapSize + valuesEncoder.getMaxByteSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getRetainedSizeInBytes() { | ||
| return RamUsageEstimator.shallowSizeOf(this) | ||
| + RamUsageEstimator.shallowSizeOf(entryIndex) | ||
| + (long) entryIndex.size() * RamUsageEstimator.SHALLOW_SIZE_OF_HASHMAP_ENTRY | ||
| + dictionaryEntriesRetainedSize | ||
| + estimateHashMapTableSize(maximumDictionarySize) | ||
|
Member
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. major
Fix: Base the table estimate on the live map (
Contributor
Author
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. Thanks for the suggestion. No change is needed: |
||
| + estimateArrayListRetainedSize(maximumDictionarySize, 0) | ||
| + valuesEncoder.getRetainedSizeInBytes(); | ||
| } | ||
|
|
||
| private long estimateHashMapTableSize(int size) { | ||
| if (size == 0) { | ||
| return 0; | ||
| } | ||
| int capacity = 16; | ||
| while ((long) size > (long) capacity * 3 / 4 && capacity < (1 << 30)) { | ||
| capacity <<= 1; | ||
| } | ||
| return RamUsageEstimator.sizeOfObjectArray(capacity); | ||
| } | ||
|
|
||
| private void writeMap(ByteArrayOutputStream out) throws IOException { | ||
| ReadWriteForEncodingUtils.writeVarInt(indexEntry.size(), out); | ||
| for (Binary value : indexEntry) { | ||
|
|
@@ -113,5 +147,6 @@ private void reset() { | |
| indexEntry.clear(); | ||
| valuesEncoder.reset(); | ||
| mapSize = 0; | ||
| dictionaryEntriesRetainedSize = 0; | ||
| } | ||
| } | ||
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.
May cache the shallow size if it is static.
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.
The boxed Integer shallow size is already cached in the static final
BOXED_VALUE_SIZEfield, initialized once when the class loads, so no further change was needed.