Skip to content

Commit

Permalink
Avoid creating map allocator when writing empty maps to StreamOutput (e…
Browse files Browse the repository at this point in the history
…lastic#105071)

It's in the title. We already have the size here and allocating the
iterator isn't free. In fact it's 10G of allocations during http_logs
indexing that we can avoid with a simple condition.
  • Loading branch information
original-brownbear authored Feb 2, 2024
1 parent 5408883 commit 7d9253e
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,13 @@ public final <K extends Writeable, V extends Writeable> void writeMap(final Map<
* @param valueWriter The value writer
*/
public final <K, V> void writeMap(final Map<K, V> map, final Writer<K> keyWriter, final Writer<V> valueWriter) throws IOException {
writeVInt(map.size());
for (final Map.Entry<K, V> entry : map.entrySet()) {
keyWriter.write(this, entry.getKey());
valueWriter.write(this, entry.getValue());
int size = map.size();
writeVInt(size);
if (size > 0) {
for (final Map.Entry<K, V> entry : map.entrySet()) {
keyWriter.write(this, entry.getKey());
valueWriter.write(this, entry.getValue());
}
}
}

Expand Down

0 comments on commit 7d9253e

Please sign in to comment.