Skip to content

DFReader Speedup#1029

Merged
tridge merged 11 commits intoArduPilot:masterfrom
robertlong13:pr/dfreader-speedup
May 19, 2025
Merged

DFReader Speedup#1029
tridge merged 11 commits intoArduPilot:masterfrom
robertlong13:pr/dfreader-speedup

Conversation

@robertlong13
Copy link
Contributor

@robertlong13 robertlong13 commented May 18, 2025

I frequently work with ~2GB logs, and it's nice to be able to use mavutil to knock together quick analysis scripts, but it can take on the order of 30 seconds just to load the log before I can even start calling recv_msg or recv_match.

This PR is mainly a rewrite of init_arrays in Cython for about a 10X speedup of the initial load.

@peterbarker
Copy link
Contributor

Ping @stephendade - any chance you could test this on Windows?

@peterbarker
Copy link
Contributor

pbarker@crun:~/rc/pymavlink((HEAD detached at robertlong13/pr/dfreader-speedup))$ mavlogdump.py $DFLOG
Traceback (most recent call last):
  File "/home/pbarker/venv-ardupilot/lib/python3.12/site-packages/pymavlink/DFReader.py", line 28, in <module>
    from . import dfindexer
ImportError: cannot import name 'dfindexer' from 'pymavlink' (/home/pbarker/venv-ardupilot/lib/python3.12/site-packages/pymavlink/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pbarker/venv-ardupilot/bin/mavlogdump.py", line 82, in <module>
    mlog = mavutil.mavlink_connection(filename, planner_format=args.planner,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pbarker/venv-ardupilot/lib/python3.12/site-packages/pymavlink/mavutil.py", line 1894, in mavlink_connection
    from pymavlink import DFReader
  File "/home/pbarker/venv-ardupilot/lib/python3.12/site-packages/pymavlink/DFReader.py", line 32, in <module>
    from pymavlink import dfindexer
ImportError: cannot import name 'dfindexer' from 'pymavlink' (/home/pbarker/venv-ardupilot/lib/python3.12/site-packages/pymavlink/__init__.py)
pbarker@crun:~/rc/pymavlink((HEAD detached at robertlong13/pr/dfreader-speedup))$ 

@robertlong13
Copy link
Contributor Author

Need to set PYTHONPATH. I think it's trying to grab dfindexer from your pip installed pymavlink.

@stephendade
Copy link
Contributor

Got the same error message as Peter on Windows. Used pip install -U . to install. Using Win11/Python 3.11

I think it's not building the c extensions? Hence not finding the dfindexer module

WSL + ardupilot environment install script makes it so I can't compile
Cython modules without it trying to use visual c as the compile.
This utilizes a barebones Cython module to substantially speed up the
time it takes to build the offsets arrays. The C code does the absolute
minimum FMT parsing: just the type-length relation. After the arrays are
built, they are used to parse FMT, UNIT, MULT, and FMTU very quickly.
Assume modern GPS format and scan for it first with recv_match. If not,
fall back to the slower way of parsing every message.
@robertlong13 robertlong13 force-pushed the pr/dfreader-speedup branch from 3bb7768 to e3da9ed Compare May 19, 2025 03:02
@robertlong13
Copy link
Contributor Author

Got the same error message as Peter on Windows. Used pip install -U . to install. Using Win11/Python 3.11

I think it's not building the c extensions? Hence not finding the dfindexer module

I had only tested with setup.py. I have fixed it now so that it works correctly through pip install. However, on Windows, I've made it not install the Cython module by default (to avoid having users need to go install Visual C build tools). Set an environment variable PYMAVLINK_FAST_INDEX=1 before running the pip install. I'll need to open a PR in MAVProxy to set that flag when it builds MAVExplorer.exe in CI.

@robertlong13
Copy link
Contributor Author

robertlong13 commented May 19, 2025

Unfortunately, due to me not fully understanding the messages dictionary, I broke it. MAVExplorer relies on that for tab complete. I can very easily fix 90% of it, but I just realized that it tracks messages and their indices, which is a harder problem to solve (solvable, still). Hopefully I'll have something for that in the next day or so.

Edit: eh, I got it faster than I expected.

Previously, we've been parsing every single message after the last
instance of the type we're looking for.
This is needed for MAVExplorer's tab-complete to work properly.
skip_to_type adds all these extra messages to the parsed types just in
case the user wants to check conditions on those messages. This is a
waste in many cases, but especially when recv_match has not been given a
conditions argument. We could actually try to parse the conditions
for message types, but this is easier for now.
@robertlong13 robertlong13 force-pushed the pr/dfreader-speedup branch from e3da9ed to a001f48 Compare May 19, 2025 04:19
@tridge
Copy link
Contributor

tridge commented May 19, 2025

@robertlong13 really nice work!
an environment variable PYMAVLINK_DISABLE_FAST_INDEX would be nice for testing

@robertlong13
Copy link
Contributor Author

@robertlong13 really nice work! an environment variable PYMAVLINK_DISABLE_FAST_INDEX would be nice for testing

Done, though I made it PYMAVLINK_FAST_INDEX to 0 to disable (instead of PYMAVLINK_DISABLE_FAST_INDEX to 1).

@robertlong13 robertlong13 force-pushed the pr/dfreader-speedup branch 3 times, most recently from 9af95a9 to 1676b1e Compare May 19, 2025 09:09
We use this at install time to determine whether to compile the fast
indexer, but we'll also check it at runtime to allow someone to disable
it if they encounter issues.
@robertlong13 robertlong13 force-pushed the pr/dfreader-speedup branch from 1676b1e to 616191e Compare May 19, 2025 09:10
@robertlong13
Copy link
Contributor Author

robertlong13 commented May 19, 2025

I have tested MAVExplorer on Windows too, using the github CI, so I'm ready with a MAVProxy PR as soon as this PR makes its way into PyPI. Only the environment variable portion of the following commit will be needed for the MAVProxy build, the rest is temporary checking out and building pymavlink so I can test. robertlong13/MAVProxy@ffe2cf2

@tridge tridge merged commit 0b02800 into ArduPilot:master May 19, 2025
13 checks passed
@tridge
Copy link
Contributor

tridge commented May 19, 2025

@robertlong13 great work Bob!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants