From e2fb765827bb2b05caad1c0d1c39bda42d5da9b4 Mon Sep 17 00:00:00 2001 From: Ramon Roche Date: Mon, 23 Nov 2026 17:27:28 -0800 Subject: [PATCH] perf(cdrstream): drop comment text from the RIHS01 hash IDL Since Zenoh is part of px4_sitl_default (#29425), every SITL variant generates the RIHS01 type hashes. That step takes 23 s of a local build and 36 to 134 s per target in CI, nearly all of it rosidl_parser reading the IDL of every message. About 60% of that text is message comments, and a type hash only covers the type name and the names and types of its fields. Write the IDL that idl2rihs01.py reads without the comment text. Units and constants stay. The IDL that idlc compiles is a separate file and does not change, and neither do the hashes of the 271 messages or the generated Zenoh pub/sub factory. The hash step drops from 22.6 s to 7.8 s on an M3 Max. convert_msg_to_idl is no longer called, so the filter for the Reading/Writing lines it printed goes too. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Ramon Roche --- src/lib/cdrstream/msg2idl.py | 68 +++++++++++++++++------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/src/lib/cdrstream/msg2idl.py b/src/lib/cdrstream/msg2idl.py index 03a6a4bf375..8974b3a09d3 100644 --- a/src/lib/cdrstream/msg2idl.py +++ b/src/lib/cdrstream/msg2idl.py @@ -34,29 +34,19 @@ ############################################################################ import argparse -import builtins import pathlib import sys -from rosidl_adapter.msg import convert_msg_to_idl from rosidl_adapter.parser import parse_message_string from rosidl_adapter.resource import expand_template -def write_idlc_idl(package_dir, package_name, input_file, output_dir): - """Write the IDL that CycloneDDS idlc compiles. - - uORB field names such as 'sequence' or 'string' are IDL keywords, which - idlc rejects. A leading underscore escapes an IDL identifier and idlc strips - it again, so the generated C names stay unchanged. Escaping every name means - no keyword list has to track idlc. rosidl_parser rejects escaped names, so - the RIHS01 hash keeps using the plain IDL from convert_msg_to_idl. - """ +def write_idl(package_dir, package_name, input_file, output_dir, adapt): + """Convert a .msg file to IDL, after adapt() has changed the parsed message.""" content = (package_dir / input_file).read_text(encoding='utf-8') msg = parse_message_string(package_name, input_file.stem, content) - for member in msg.fields + msg.constants: - member.name = f'_{member.name}' + adapt(msg) data = { 'pkg_name': package_name, @@ -67,6 +57,30 @@ def write_idlc_idl(package_dir, package_name, input_file, output_dir): encoding='iso-8859-1') +def drop_comments(msg): + """Adapt a message for the IDL that idl2rihs01.py hashes. + + A type hash covers the type name and the names and types of its fields. + rosidl_parser takes time in proportion to the size of the IDL, and the + comment text is most of it, so it is left out. Units and constants stay. + """ + for item in [msg] + msg.fields + msg.constants: + item.annotations.pop('comment', None) + + +def escape_names(msg): + """Adapt a message for the IDL that CycloneDDS idlc compiles. + + uORB field names such as 'sequence' or 'string' are IDL keywords, which + idlc rejects. A leading underscore escapes an IDL identifier and idlc strips + it again, so the generated C names stay unchanged. Escaping every name means + no keyword list has to track idlc. rosidl_parser rejects escaped names, so + the RIHS01 hash has its own IDL. + """ + for member in msg.fields + msg.constants: + member.name = f'_{member.name}' + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=f'Convert px4 .msg files to .idl') @@ -78,26 +92,10 @@ if __name__ == '__main__': help='Directory for the keyword-escaped IDL compiled by idlc') args = parser.parse_args(sys.argv[1:]) - # convert_msg_to_idl prints a Reading/Writing line per file. Filter those - # via builtins.print instead of redirecting stdout, which the empy template - # engine relies on and breaks if replaced. Errors still surface on stderr. - real_print = builtins.print + for interface_file in args.interface_files: + interface_file = pathlib.Path(interface_file) + package_dir = interface_file.parent.absolute() + input_file = interface_file.absolute().relative_to(package_dir) - def quiet_print(*pargs, **kwargs): - if pargs and isinstance(pargs[0], str) and \ - pargs[0].startswith(('Reading input file:', 'Writing output file:')): - return - real_print(*pargs, **kwargs) - - builtins.print = quiet_print - try: - for interface_file in args.interface_files: - interface_file = pathlib.Path(interface_file) - package_dir = interface_file.parent.absolute() - input_file = interface_file.absolute().relative_to(package_dir) - - convert_msg_to_idl( - package_dir, 'px4_msgs', input_file, interface_file.parent) - write_idlc_idl(package_dir, 'px4_msgs', input_file, args.idlc_output_dir) - finally: - builtins.print = real_print + write_idl(package_dir, 'px4_msgs', input_file, interface_file.parent, drop_comments) + write_idl(package_dir, 'px4_msgs', input_file, args.idlc_output_dir, escape_names)