-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add BufferedMurmur3Hasher to reduce allocations when hashing Strings #133226
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
56271f7
Add BufferedMurmur3Hasher to reduce allocations when hashing Strings
felixbarny b5663a0
[CI] Auto commit changes from spotless
ba6125f
Test with larger range of string values
felixbarny b736312
Reduce number of flushes
felixbarny 84c08d4
Merge remote-tracking branch 'origin/main' into buffered-murmur3
felixbarny b277cef
Fix offsets
felixbarny 2d8cb1c
More randomized testing and fixes
felixbarny 082bfe0
Merge remote-tracking branch 'origin/main' into buffered-murmur3
felixbarny 9b645ba
Merge branch 'main' into buffered-murmur3
felixbarny 6467d0c
Merge branch 'main' into buffered-murmur3
felixbarny 8b46142
Merge remote-tracking branch 'origin/main' into buffered-murmur3
felixbarny 35e22af
Use BufferedMurmur3Hasher in TsidBuilder
felixbarny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
server/src/main/java/org/elasticsearch/common/hash/BufferedMurmur3Hasher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.hash; | ||
|
|
||
| import org.apache.lucene.util.UnicodeUtil; | ||
| import org.elasticsearch.common.util.ByteUtils; | ||
|
|
||
| /** | ||
| * A buffered Murmur3 hasher that allows hashing strings and longs efficiently. | ||
| * It uses a byte array buffer to reduce allocations for converting strings and longs to bytes before passing them to the hasher. | ||
| * The buffer also allows for more efficient execution by minimizing the number of times the underlying hasher is updated, | ||
| * and by maximizing the amount of data processed in each update call. | ||
| */ | ||
| public class BufferedMurmur3Hasher extends Murmur3Hasher { | ||
|
|
||
| public static final int DEFAULT_BUFFER_SIZE = 32 * 4; // 32 characters, each character may take up to 4 bytes in UTF-8 | ||
| /** | ||
| * The buffer used for holding the UTF-8 encoded strings before passing them to the hasher. | ||
| * Should be sized so that it can hold the longest UTF-8 encoded string that is expected to be hashed, | ||
| * to avoid re-sizing the buffer. | ||
| * But should also be small enough to not waste memory in case the keys are short. | ||
| */ | ||
| private byte[] buffer; | ||
| private int pos; | ||
|
|
||
| public BufferedMurmur3Hasher(long seed) { | ||
| this(seed, DEFAULT_BUFFER_SIZE); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a BufferedMurmur3Hasher with a specified seed and buffer size. | ||
| * | ||
| * @param seed the seed for the Murmur3 hash function | ||
| * @param bufferSize the size of the buffer in bytes, must be at least 32 | ||
| */ | ||
| public BufferedMurmur3Hasher(long seed, int bufferSize) { | ||
| super(seed); | ||
| if (bufferSize < 32) { | ||
| throw new IllegalArgumentException("Buffer size must be at least 32 bytes"); | ||
| } | ||
| this.buffer = new byte[bufferSize]; | ||
| } | ||
|
|
||
| @Override | ||
| public MurmurHash3.Hash128 digestHash(MurmurHash3.Hash128 hash) { | ||
| flush(); | ||
| return super.digestHash(hash); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| super.reset(); | ||
| pos = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a string to the hasher. | ||
| * The string is converted to UTF-8 and written into the buffer. | ||
| * The buffer is resized if necessary to accommodate the UTF-8 encoded string. | ||
| * | ||
| * @param value the string value to add | ||
| */ | ||
| public void addString(String value) { | ||
| int requiredBufferLength = UnicodeUtil.maxUTF8Length(value.length()); | ||
| ensureCapacity(requiredBufferLength); | ||
| flushIfRemainingCapacityLowerThan(requiredBufferLength); | ||
| pos = UnicodeUtil.UTF16toUTF8(value, 0, value.length(), buffer, pos); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a long value to the hasher. | ||
| * The long is written in little-endian format. | ||
| * | ||
| * @param value the long value to add | ||
| */ | ||
| public void addLong(long value) { | ||
| flushIfRemainingCapacityLowerThan(Long.BYTES); | ||
| ByteUtils.writeLongLE(value, buffer, pos); | ||
| pos += Long.BYTES; | ||
| } | ||
|
|
||
| /** | ||
| * Adds two long values to the hasher. | ||
| * Each long is written in little-endian format. | ||
| * | ||
| * @param v1 the first long value to add | ||
| * @param v2 the second long value to add | ||
| */ | ||
| public void addLongs(long v1, long v2) { | ||
| flushIfRemainingCapacityLowerThan(Long.BYTES * 2); | ||
| ByteUtils.writeLongLE(v1, buffer, pos); | ||
| ByteUtils.writeLongLE(v2, buffer, pos + 8); | ||
| pos += Long.BYTES * 2; | ||
| } | ||
|
|
||
| /** | ||
| * Adds four long values to the hasher. | ||
| * Each long is written in little-endian format. | ||
| * | ||
| * @param v1 the first long value to add | ||
| * @param v2 the second long value to add | ||
| * @param v3 the third long value to add | ||
| * @param v4 the fourth long value to add | ||
| */ | ||
| public void addLongs(long v1, long v2, long v3, long v4) { | ||
| flushIfRemainingCapacityLowerThan(Long.BYTES * 4); | ||
| ByteUtils.writeLongLE(v1, buffer, pos); | ||
| ByteUtils.writeLongLE(v2, buffer, pos + 8); | ||
| ByteUtils.writeLongLE(v3, buffer, pos + 16); | ||
| ByteUtils.writeLongLE(v4, buffer, pos + 24); | ||
| pos += Long.BYTES * 4; | ||
| } | ||
|
|
||
| private void ensureCapacity(int requiredBufferLength) { | ||
| if (buffer.length < requiredBufferLength) { | ||
| flush(); | ||
| buffer = new byte[requiredBufferLength]; | ||
| } | ||
| } | ||
|
|
||
| private void flush() { | ||
| if (pos > 0) { | ||
| update(buffer, 0, pos); | ||
| pos = 0; | ||
| } | ||
| } | ||
|
|
||
| private void flushIfRemainingCapacityLowerThan(int requiredCapacity) { | ||
| if (buffer.length - pos < requiredCapacity) { | ||
| flush(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe
128 * 4to play it safe?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.
As this is the default size, I'd like to be a bit more conservative with memory usage. This should probably also be enough for the OTLP endpoint as this is used primarily for attribute names. The attribute values are already in utf8-encoded byte arrays and directly use the update method.