-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative runtime without struct
#21
Comments
I will reach out to python mailing list, the guys there are very helpful with advice, and knowledgable too. There is a way to pre-compile a formatstring into a packer object, but it does also return a tuple. >>> timeit.timeit("struct.unpack('=b', b'x')", "import struct")
0.18020300199714256
>>> timeit.timeit("p.unpack(b'x')", "import struct; p = struct.Struct('=b')")
0.11721895999653498 |
IMHO no. 1 C is fast, but I wonder if Rust may be better here. meta:
id: l_l_l
seq:
- id: a
type: u4
- id: b
type: u4
- id: c
type: aa
- id: d
type: f8
types:
aa:
seq:
- id: b
type: u1 now (simplified, only conducts sense) class LLL(...):
...
a=unpack("I", ...)
b=unpack("I", ...)
c=Aa(...)
d=unpack("d") with precompilation class LLL(...):
ab_unp=Struct("II")
d_unp=Struct("d") # in fact we can precompile for single bytes once and reuse.
...
a, b=self.__class__.ab_unp.unpack(...)
c=Aa(...)
d = self.__class__.d_unp.unpack(...) with flattening: class LLL(...):
abcd_unp=Struct("IIBd")
...
a, b, c_b, d=self.__class__.abcd_unp.unpack(...)
c=Aa._from_unpacked_tuple((c_b,)) |
I suggest closing this topic. I think we have already arrived at a conclusion: Implementing Python parser (not runtime) in C would be a major hurdle that would not even be worth the effort. And we already exhausted what can be done in Pure python. |
If dropping Python 2 support is actually an option, then it might be worth looking at the
(default: unsigned) for example:
It also allows converting arbitrary length byte strings, so something like implementing
https://docs.python.org/3/library/stdtypes.html#int.from_bytes |
For arrays of numbers we probably should use |
Python's
struct
module seems to be pretty inefficient for our purposes. Namely, in all APIs it provides, it requires passing a format string intounpack
-like function, which then parses that format string in runtime, calls relevant unpack methods, and then constructs a tuple with a single value, which we extract right away.Actually,
struct
even has everything we need — for example, these are functions which read ("unpack") integers, but it's not exposed as Python API.Would it make sense / be faster to introduce alternative, native Kaitai Struct API which would be written in C, but would be faster than existing one?
Cc @koczkatamas @KOLANICH @arekbulski
The text was updated successfully, but these errors were encountered: