一. Tool Launguage
python.
二. Cause
__offset()
method is not fast enough.- for get a varaible may need to calls
__offset()
method many times.
三. Effect
- Add has set method for flatbuffer file.
- Add member variables cache (include array variables).
四. Exp
origin:
1 | public int top() { int o = __offset(4); return o != 0 ? bb.getInt(o + bb_pos) : 0; } |
convert to:
1 | public boolean has_top_cache = false; |
origin:
1 | public String fontFamily(int j) { int o = __offset(10); return o != 0 ? __string(__vector(o) + j * 4) : null; } |
convert to:
1 | public int list_fontFamily_offset = -1; |
五. Tips
Flatbuffer is very fast from flatbuffer instream to avaliable object, but __offset()
is very slow, so very frequently invoked almost not recommended. I tested 100,000 times __offset()
, consuming greater than 50ms, but Java object directly accesse the same time only need 2~3ms. So such tool is come, but you need to pay attention to the increase of the GC.
六. Tool Source
GitHub: https://github.com/Jacksgong/FlatBuffer-Optimize
1 | import os |