Skip to content

Commit 90f2e84

Browse files
committed
[Java] Avoid allocation when encoding an empty string.
1 parent 5a1082a commit 90f2e84

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def validationXsdPath = project(':sbe-tool').projectDir.toString() + '/src/main/
161161

162162
project(':sbe-tool') {
163163
dependencies {
164-
compile 'org.agrona:agrona:0.9.12'
164+
compile 'org.agrona:agrona:0.9.13-SNAPSHOT'
165165

166166
testCompile files('build/classes/java/generated')
167167
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,8 @@ private void generateDataEncodeMethods(
873873
indent + " final byte[] bytes;\n" +
874874
indent + " try\n" +
875875
indent + " {\n" +
876-
indent + " bytes = value.isEmpty() ? new byte[0] : value.getBytes(\"%3$s\");\n" +
876+
indent + " bytes = value.isEmpty() ?" +
877+
" org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes(\"%3$s\");\n" +
877878
indent + " }\n" +
878879
indent + " catch (final java.io.UnsupportedEncodingException ex)\n" +
879880
indent + " {\n" +

sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/generated/FrameCodecEncoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public FrameCodecEncoder packageName(final String value)
255255
final byte[] bytes;
256256
try
257257
{
258-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
258+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
259259
}
260260
catch (final java.io.UnsupportedEncodingException ex)
261261
{
@@ -342,7 +342,7 @@ public FrameCodecEncoder namespaceName(final String value)
342342
final byte[] bytes;
343343
try
344344
{
345-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
345+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
346346
}
347347
catch (final java.io.UnsupportedEncodingException ex)
348348
{
@@ -429,7 +429,7 @@ public FrameCodecEncoder semanticVersion(final String value)
429429
final byte[] bytes;
430430
try
431431
{
432-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
432+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
433433
}
434434
catch (final java.io.UnsupportedEncodingException ex)
435435
{

sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/generated/TokenCodecEncoder.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public TokenCodecEncoder name(final String value)
415415
final byte[] bytes;
416416
try
417417
{
418-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
418+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
419419
}
420420
catch (final java.io.UnsupportedEncodingException ex)
421421
{
@@ -502,7 +502,7 @@ public TokenCodecEncoder constValue(final String value)
502502
final byte[] bytes;
503503
try
504504
{
505-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
505+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
506506
}
507507
catch (final java.io.UnsupportedEncodingException ex)
508508
{
@@ -589,7 +589,7 @@ public TokenCodecEncoder minValue(final String value)
589589
final byte[] bytes;
590590
try
591591
{
592-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
592+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
593593
}
594594
catch (final java.io.UnsupportedEncodingException ex)
595595
{
@@ -676,7 +676,7 @@ public TokenCodecEncoder maxValue(final String value)
676676
final byte[] bytes;
677677
try
678678
{
679-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
679+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
680680
}
681681
catch (final java.io.UnsupportedEncodingException ex)
682682
{
@@ -763,7 +763,7 @@ public TokenCodecEncoder nullValue(final String value)
763763
final byte[] bytes;
764764
try
765765
{
766-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
766+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
767767
}
768768
catch (final java.io.UnsupportedEncodingException ex)
769769
{
@@ -850,7 +850,7 @@ public TokenCodecEncoder characterEncoding(final String value)
850850
final byte[] bytes;
851851
try
852852
{
853-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
853+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
854854
}
855855
catch (final java.io.UnsupportedEncodingException ex)
856856
{
@@ -937,7 +937,7 @@ public TokenCodecEncoder epoch(final String value)
937937
final byte[] bytes;
938938
try
939939
{
940-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
940+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
941941
}
942942
catch (final java.io.UnsupportedEncodingException ex)
943943
{
@@ -1024,7 +1024,7 @@ public TokenCodecEncoder timeUnit(final String value)
10241024
final byte[] bytes;
10251025
try
10261026
{
1027-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
1027+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
10281028
}
10291029
catch (final java.io.UnsupportedEncodingException ex)
10301030
{
@@ -1111,7 +1111,7 @@ public TokenCodecEncoder semanticType(final String value)
11111111
final byte[] bytes;
11121112
try
11131113
{
1114-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
1114+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
11151115
}
11161116
catch (final java.io.UnsupportedEncodingException ex)
11171117
{
@@ -1198,7 +1198,7 @@ public TokenCodecEncoder description(final String value)
11981198
final byte[] bytes;
11991199
try
12001200
{
1201-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
1201+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
12021202
}
12031203
catch (final java.io.UnsupportedEncodingException ex)
12041204
{
@@ -1285,7 +1285,7 @@ public TokenCodecEncoder referencedName(final String value)
12851285
final byte[] bytes;
12861286
try
12871287
{
1288-
bytes = value.isEmpty() ? new byte[0] : value.getBytes("UTF-8");
1288+
bytes = value.isEmpty() ? org.agrona.collections.ArrayUtil.EMPTY_BYTE_ARRAY : value.getBytes("UTF-8");
12891289
}
12901290
catch (final java.io.UnsupportedEncodingException ex)
12911291
{

0 commit comments

Comments
 (0)