Skip to content

Commit

Permalink
avoiding allocations of PointerReader and PointerBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Oct 8, 2014
1 parent b884ad1 commit dce3c46
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 36 deletions.
21 changes: 10 additions & 11 deletions compiler/src/main/cpp/capnpc-java.c++
Original file line number Diff line number Diff line change
Expand Up @@ -981,16 +981,16 @@ private:

spaces(indent), " public ", type, ".Reader get", titleCase, "() {\n",
unionDiscrim.check,
spaces(indent), " return ", type, ".factory.fromPointerReader(",
"_getPointerField(", offset,"),", defaultParams, ");\n",
spaces(indent), " return ",
"_getPointerField(", type, ".factory,", offset,",", defaultParams, ");\n",
spaces(indent), " }\n", "\n"),

kj::strTree(
kj::mv(unionDiscrim.builderIsDecl),
spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n",
unionDiscrim.check,
spaces(indent), " return ", type, ".factory.fromPointerBuilder("
"_getPointerField(", offset, "),", defaultParams,");\n",
spaces(indent), " return ",
"_getPointerField(", type, ".factory, ", offset, ", ", defaultParams, ");\n",
spaces(indent), " }\n",
spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n",
unionDiscrim.set,
Expand Down Expand Up @@ -1069,8 +1069,7 @@ private:

spaces(indent), " public final ", readerClass,
" get", titleCase, "() {\n",
spaces(indent), " return (", listFactory, ").fromPointerReader(_getPointerField(", offset, "),",
defaultParams, ");\n",
spaces(indent), " return _getPointerField(", listFactory, ", ", offset, ", ", defaultParams, ");\n",
spaces(indent), " }\n",
"\n"),

Expand All @@ -1082,8 +1081,7 @@ private:

spaces(indent), " public final ", builderClass,
" get", titleCase, "() {\n",
spaces(indent), " return (", listFactory, ").fromPointerBuilder(_getPointerField(", offset, "),",
defaultParams, ");\n",
spaces(indent), " return _getPointerField(", listFactory, ", ", offset, ", ", defaultParams, ");\n",
spaces(indent), " }\n",

spaces(indent), " public final void set", titleCase, "(", readerClass, " value) {\n",
Expand Down Expand Up @@ -1301,9 +1299,10 @@ private:
true,
kj::strTree(spaces(indent),
"public static final ", typeName_, ".Reader ", upperCase, " =\n",
spaces(indent), " ", typeName_, ".factory.fromPointerReader"
"(new org.capnproto.PointerReader(Schemas.b_",
kj::hex(proto.getId()), ",", schema.getValueSchemaOffset(), ",0x7fffffff), null, 0);\n")
spaces(indent), " ",
"new org.capnproto.AnyPointer.Reader(new org.capnproto.PointerReader(Schemas.b_",
kj::hex(proto.getId()), ",", schema.getValueSchemaOffset(), ",0x7fffffff)).getAs(",
typeName_, ".factory);\n")
};
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/main/java/org/capnproto/AnyPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public Reader(PointerReader reader) {
}

public final <T> T getAs(FromPointerReader<T> factory) {
return factory.fromPointerReader(this.reader, null, 0);
return factory.fromPointerReader(this.reader.segment, this.reader.pointer, null, 0, this.reader.nestingLimit);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.capnproto;

public interface FromPointerBuilder<T> {
T fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset);
T fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset);
}
2 changes: 1 addition & 1 deletion runtime/src/main/java/org/capnproto/FromPointerReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.capnproto;

public interface FromPointerReader<T> {
T fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset);
T fromPointerReader(SegmentReader segment, int pointer, SegmentReader defaultSegment, int defaultOffset, int nestingLimit);
}
6 changes: 6 additions & 0 deletions runtime/src/main/java/org/capnproto/ListBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,10 @@ protected final PointerBuilder _getPointerElement(int index) {
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD);
}

protected final <T> T _getPointerElement(FromPointerBuilder<T> factory, int index, SegmentReader defaultSegment, int defaultOffset) {
return factory.fromPointerBuilder(
this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
defaultSegment, defaultOffset);
}
}
16 changes: 9 additions & 7 deletions runtime/src/main/java/org/capnproto/ListFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ public abstract class ListFactory<Builder, Reader> implements ListBuilder.Factor
final byte elementSize;
ListFactory(byte elementSize) {this.elementSize = elementSize;}

public final Reader fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset) {
public final Reader fromPointerReader(SegmentReader segment, int pointer,
SegmentReader defaultSegment, int defaultOffset,
int nestingLimit) {
return WireHelpers.readListPointer(this,
reader.segment,
reader.pointer,
segment,
pointer,
defaultSegment,
defaultOffset,
this.elementSize,
reader.nestingLimit);
nestingLimit);
}

public Builder fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) {
public Builder fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset) {
return WireHelpers.getWritableListPointer(this,
builder.pointer,
builder.segment,
pointer,
segment,
this.elementSize,
defaultSegment,
defaultOffset);
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/main/java/org/capnproto/ListList.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Reader(FromPointerReader<T> factory,
}

public T get(int index) {
return this.factory.fromPointerReader(_getPointerElement(index), null, 0);
return _getPointerElement(this.factory, index, null, 0);
}

}
Expand All @@ -62,7 +62,7 @@ public final T init(int index, int size) {
}

public final T get(int index) {
return this.factory.fromPointerBuilder(_getPointerElement(index), null, 0);
return _getPointerElement(this.factory, index, null, 0);
}
}
}
8 changes: 8 additions & 0 deletions runtime/src/main/java/org/capnproto/ListReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ protected PointerReader _getPointerElement(int index) {
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
this.nestingLimit);
}

protected <T> T _getPointerElement(FromPointerReader<T> factory, int index, SegmentReader defaultSegment, int defaultOffset) {
return factory.fromPointerReader(this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
defaultSegment,
defaultOffset,
this.nestingLimit);
}
}
4 changes: 4 additions & 0 deletions runtime/src/main/java/org/capnproto/StructBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,8 @@ protected final PointerBuilder _getPointerField(int index) {
return new PointerBuilder(this.segment, this.pointers + index);
}

protected final <T> T _getPointerField(FromPointerBuilder<T> factory, int index, SegmentReader defaultSegment, int defaultOffset) {
return factory.fromPointerBuilder(this.segment, this.pointers + index, defaultSegment, defaultOffset);
}

}
23 changes: 13 additions & 10 deletions runtime/src/main/java/org/capnproto/StructFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
public abstract class StructFactory<Builder, Reader> implements FromPointerBuilder<Builder>, StructBuilder.Factory<Builder>,
InitFromPointerBuilder<Builder>,
FromPointerReader<Reader>, StructReader.Factory<Reader> {
public Builder fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) {
return WireHelpers.getWritableStructPointer(this, builder.pointer, builder.segment, this.structSize(),
public final Reader fromPointerReader(SegmentReader segment, int pointer,
SegmentReader defaultSegment, int defaultOffset,
int nestingLimit) {
return WireHelpers.readStructPointer(this,
segment,
pointer,
defaultSegment, defaultOffset,
nestingLimit);
}
public final Builder fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset) {
return WireHelpers.getWritableStructPointer(this, pointer, segment, this.structSize(),
defaultSegment, defaultOffset);
}
public Builder initFromPointerBuilder(PointerBuilder builder) {
public final Builder initFromPointerBuilder(PointerBuilder builder) {
return WireHelpers.initStructPointer(this, builder.pointer, builder.segment, this.structSize());
}
public Reader fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset) {
return WireHelpers.readStructPointer(this,
reader.segment,
reader.pointer,
defaultSegment, defaultOffset,
reader.nestingLimit);
}

public abstract Reader asReader(Builder builder);
}
7 changes: 4 additions & 3 deletions runtime/src/main/java/org/capnproto/StructList.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public final Builder<ElementBuilder> constructBuilder(SegmentBuilder segment,
return new Builder<ElementBuilder> (factory, segment, ptr, elementCount, step, structDataSize, structPointerCount);
}

public final Builder<ElementBuilder> fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) {
public final Builder<ElementBuilder> fromPointerBuilder(SegmentBuilder segment, int pointer,
SegmentReader defaultSegment, int defaultOffset) {
return WireHelpers.getWritableStructListPointer(this,
builder.pointer,
builder.segment,
pointer,
segment,
factory.structSize(),
defaultSegment,
defaultOffset);
Expand Down
16 changes: 16 additions & 0 deletions runtime/src/main/java/org/capnproto/StructReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,20 @@ protected final PointerReader _getPointerField(int ptrIndex) {
return new PointerReader();
}
}

protected final <T> T _getPointerField(FromPointerReader<T> factory, int ptrIndex, SegmentReader defaultSegment, int defaultOffset) {
if (ptrIndex < this.pointerCount) {
return factory.fromPointerReader(this.segment,
this.pointers + ptrIndex,
defaultSegment,
defaultOffset,
this.nestingLimit);
} else {
return factory.fromPointerReader(SegmentReader.EMPTY,
0,
defaultSegment,
defaultOffset,
this.nestingLimit);
}
}
}

0 comments on commit dce3c46

Please sign in to comment.