Skip to content

Commit 174dd2f

Browse files
committed
Make type lookup faster.
Use a map so we don't have to continually do a search in the inner loop. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
1 parent c39ebce commit 174dd2f

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

ros2param/ros2param/verb/list.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,20 @@ def main(self, *, args): # noqa: D102
101101
if not args.node_name:
102102
print(f'{node_name.full_name}:')
103103
response = future.result()
104+
sorted_names = sorted(response.result.names)
104105
# get descriptors for the node if needs to print parameter type
106+
name_to_type_map = {}
105107
if args.param_type is True:
106108
resp = call_describe_parameters(
107109
node=node, node_name=node_name.full_name,
108-
parameter_names=sorted(response.result.names))
109-
for name in sorted(response.result.names):
110+
parameter_names=sorted_names)
111+
for descriptor in resp.descriptors:
112+
name_to_type_map[descriptor.name] = get_parameter_type_string(
113+
descriptor.type)
114+
115+
for name in sorted_names:
110116
if args.param_type is True:
111-
param_type_str = None
112-
for descriptor in resp.descriptors:
113-
if descriptor.name == name:
114-
param_type_str = get_parameter_type_string(descriptor.type)
117+
param_type_str = name_to_type_map[name]
115118
print(f' {name} (type: {param_type_str})')
116119
else:
117120
print(f' {name}')

0 commit comments

Comments
 (0)