diff options
author | Kulikov Alexey <kulikov.alexey90@gmail.com> | 2019-02-26 05:45:29 +0700 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-02-25 23:45:29 +0100 |
commit | 4e5152d886de963162ae81282240bc5377fa10ce (patch) | |
tree | b61e9ae17778f1449bcb2a81f46c63fa9bd798e0 /java/com | |
parent | dc61512f20c034a3b949a9e5dfc06ec448a05f20 (diff) | |
download | flatbuffers-4e5152d886de963162ae81282240bc5377fa10ce.tar.gz flatbuffers-4e5152d886de963162ae81282240bc5377fa10ce.tar.bz2 flatbuffers-4e5152d886de963162ae81282240bc5377fa10ce.zip |
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.
Diffstat (limited to 'java/com')
-rw-r--r-- | java/com/google/flatbuffers/Table.java | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/java/com/google/flatbuffers/Table.java b/java/com/google/flatbuffers/Table.java index 31df0082..cedbb8eb 100644 --- a/java/com/google/flatbuffers/Table.java +++ b/java/com/google/flatbuffers/Table.java @@ -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; } } |