Skip to content

Commit e3ab01b

Browse files
committed
Pass the offset and length to Loader#bytesToName to avoid an extra copy
1 parent fbf7c14 commit e3ab01b

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

  • templates/java/api/target/generated-sources/java/org/ruby_lang/prism

templates/java/api/target/generated-sources/java/org/ruby_lang/prism/Loader.java.erb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public class Loader {
1919

2020
// Overridable methods
2121

22-
public <%= id_type %> bytesToName(byte[] bytes) {
22+
public <%= id_type %> bytesToName(byte[] serialized, int offset, int length) {
2323
<%- if id_type == "byte[]" -%>
24+
byte[] bytes = new byte[length];
25+
System.arraycopy(serialized, offset, bytes, 0, length);
2426
return bytes;
2527
<%- else -%>
2628
throw new AbstractMethodError("Loader.bytesToName(<%= id_type %>) is not implemented");
@@ -37,9 +39,9 @@ public class Loader {
3739
this.loader = loader;
3840
this.bufferOffset = bufferOffset;
3941
<%- if id_type == "String" -%>
40-
cache = new <%= id_type %>[length];
42+
this.cache = new <%= id_type %>[length];
4143
<%- else -%>
42-
cache = new byte[length][];
44+
this.cache = new byte[length][];
4345
<%- end -%>
4446
}
4547

@@ -52,10 +54,7 @@ public class Loader {
5254
int start = buffer.getInt(offset);
5355
int length = buffer.getInt(offset + 4);
5456

55-
byte[] bytes = new byte[length];
56-
buffer.get(start, bytes);
57-
58-
constant = loader.bytesToName(bytes);
57+
constant = loader.bytesToName(loader.serialized, start, length);
5958
cache[index] = constant;
6059
}
6160

@@ -64,12 +63,14 @@ public class Loader {
6463

6564
}
6665

66+
private final byte[] serialized;
6767
private final ByteBuffer buffer;
6868
protected String encodingName;
6969
private ConstantPool constantPool;
7070
private Nodes.Source source = null;
7171

7272
protected Loader(byte[] serialized) {
73+
this.serialized = serialized;
7374
this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
7475
}
7576

0 commit comments

Comments
 (0)