Replace cast(...) in generated Python code with type annotations#1089
Conversation
|
CI failure in the |
|
Impressive. I had heard about this issue but had never seen anyone benchmark it. Are your packets/sec benchmarks in this repo, or is that custom code that you created? I find the proposed changes much easier to read. I like that several lines have been moved outside the try / except block. I am puzzled why I am not a maintainer, so please wait for other reviews, but if a maintainer agrees with your changes, then we should disable mypy on legacy Python 3.7 by replacing: pymavlink/.github/workflows/test.yml Line 60 in 0e3222c with +
+ - name: Type check generated python code
+ if: matrix.python-version != '3.7'
+ run: |
+ mypy --config-file ./.github/workflows/mypy-generated.ini dialects/v10/*.py dialects/v20/*.pyAnd + python_version = 3.8 |
Custom code; not secret at all but it requires a large pickle file (about 68 MB) that contains actual signed MAVLink packets plus noise (fragmented packets, serial line noise etc) that we captured from ~1000 drones running at a same time on an UDP/IP network, and the benchmark kinda doesn't make sense without real data. I can publish it somewhere if needed. Usually the casts don't cause a problem in normal Python code, but this was special because it was on a hot path and contained a complex type signature. |
|
Nice work! I'm happy with disabling mypy on 3.7 |
|
@ntamas we try to avoid merge commits in all of our repos... would be nice to have this rebased on master. |
31459b1 to
a183613
Compare
|
@peterbarker Done. |
|
@ntamas sorry, it has conflicts, I github is not allowing me to rebase it (maybe you unticked the allow maintainer edits button?) |
No, I didn't, but it's a cross-organization pull request (our fork is under an org account) so GitHub does not show this checkbox. Conflicts fixed - would you like me to rebase to get rid of the merge commit? |
yes please, and the PR still shows conflicts for me "This branch cannot be rebased due to conflicts" |
d42086e to
f429d93
Compare
This patch removes the occurrences of
cast(some_type, expr)in the generated Python code and replaces these expressions with type annotations. The patch increases the speed of the MAVLink parser from about 93K packets/sec to 111K packets/sec in my benchmarks on a Macbook Pro M1 while still maintaining type safety (allmypychecks pass).The reason for the runtime cost of
cast(some_type, expr)is thatcast()is a proper function in Python that simply ignores the first argument and returns the second argument.cast()only has a special meaning for type checkers. However,some_type(the first argument) still needs to be constructed whencast()is called even though it will be discarded immediately, and the cost is non-negligible for complex types likeTuple[bytes, int, int, int, int, int, int, int, int]. In contrast, inline type annotations used in this patch are completely stripped away when the source is compiled into bytecode so they have zero runtime cost.