Skip to content

Commit

Permalink
AVRO-2731: initialize hashmap with good capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec authored and dkulp committed Jul 31, 2023
1 parent 8664fa1 commit 33c763e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ $I }
$I long size${nv} = in.readMapStart();
$I $t m${nv} = ${var}; // Need fresh name due to limitation of macro system
$I if (m${nv} == null) {
$I m${nv} = new java.util.HashMap<${kt},${vt}>((int)size${nv});
$I m${nv} = new java.util.HashMap<${kt},${vt}>((int)(size${nv} * 4)/3 + 1);
$I $var = m${nv};
$I } else m${nv}.clear();
$I for ( ; 0 < size${nv}; size${nv} = in.mapNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.apache.avro.Schema;
import org.apache.avro.io.Encoder;
Expand Down Expand Up @@ -71,8 +73,9 @@ void withoutSchemaMigration() throws IOException {

@Test
void withSchemaMigration() throws IOException {
Map<CharSequence, CharSequence> map = new HashMap<>();
FullRecordV2 src = new FullRecordV2(true, 731, 87231, 38L, 54.2832F, "Hi there",
ByteBuffer.wrap(Utf8.getBytesFor("Hello, world!")));
ByteBuffer.wrap(Utf8.getBytesFor("Hello, world!")), map);
assertTrue(((SpecificRecordBase) src).hasCustomCoders(), "Test schema must allow for custom coders.");

ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
Expand Down
3 changes: 2 additions & 1 deletion lang/java/compiler/src/test/resources/full_record_v2.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
}, {
"name" : "h",
"type" : "bytes"
} ]
},
{ "name" : "myMap", "type" : { "type" : "map", "values" : "string" } }]
}

0 comments on commit 33c763e

Please sign in to comment.