mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Java: Calculation of vtable and vtable size moved to the __init method. (#5210)
vtable and vtable size depends only on `Table#bb_pos` but calculated in `Table#_offset` method on each field lookup. Doing this with every call of `Table#__offset` is redundant. These values can be read once with change of `Table#bb_pos` and reused for any field lookup.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
dc61512f20
commit
4e5152d886
@@ -37,6 +37,10 @@ public class Table {
|
||||
protected int bb_pos;
|
||||
/** The underlying ByteBuffer to hold the data of the Table. */
|
||||
protected ByteBuffer bb;
|
||||
/** Used to hold the vtable position. */
|
||||
protected int vtable_start;
|
||||
/** Used to hold the vtable size. */
|
||||
protected int vtable_size;
|
||||
Utf8 utf8 = Utf8.getDefault();
|
||||
|
||||
/**
|
||||
@@ -53,8 +57,7 @@ public class Table {
|
||||
* @return Returns an offset into the object, or `0` if the field is not present.
|
||||
*/
|
||||
protected int __offset(int vtable_offset) {
|
||||
int vtable = bb_pos - bb.getInt(bb_pos);
|
||||
return vtable_offset < bb.getShort(vtable) ? bb.getShort(vtable + vtable_offset) : 0;
|
||||
return vtable_offset < vtable_size ? bb.getShort(vtable_start + vtable_offset) : 0;
|
||||
}
|
||||
|
||||
protected static int __offset(int vtable_offset, int offset, ByteBuffer bb) {
|
||||
@@ -169,6 +172,8 @@ public class Table {
|
||||
offset += bb_pos;
|
||||
t.bb_pos = offset + bb.getInt(offset);
|
||||
t.bb = bb;
|
||||
t.vtable_start = t.bb_pos - bb.getInt(t.bb_pos);
|
||||
t.vtable_size = bb.getShort(t.vtable_start);
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -268,6 +273,8 @@ public class Table {
|
||||
public void __reset() {
|
||||
bb = null;
|
||||
bb_pos = 0;
|
||||
vtable_start = 0;
|
||||
vtable_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user