Skip to content

Refactor codebase to improve performance, maintainability and usability#68

Merged
flferretti merged 24 commits intomainfrom
refactor_repo
Mar 27, 2026
Merged

Refactor codebase to improve performance, maintainability and usability#68
flferretti merged 24 commits intomainfrom
refactor_repo

Conversation

@flferretti
Copy link
Copy Markdown
Collaborator

@flferretti flferretti commented May 15, 2025

This PR enhances the codebase by fixing typos, refactoring classes, and optimizing Gazebo SDF processing with caching and batch support. Moreover, it updates the build system to use hatchling and improves the command-line interface.

First loading run:

# %timeit -n1 -r1 rod.Sdf.load(model_path)
< 678 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
> 592 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

Successive loading run:

# %timeit rod.Sdf.load(model_path)
< 657 ms ± 39.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
> 12.1 ms ± 396 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Switching model frame convention to URDF:

# %timeit sdf.model.switch_frame_convention(frame_convention=rod.FrameConvention.Urdf)
< 196 ms ± 4.98 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
> 168 ms ± 9.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Exporting to URDF:

# %timeit UrdfExporter(pretty=True, gazebo_preserve_fixed_joints=True).to_urdf_string(sdf=sdf)
< 237 ms ± 2.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
> 206 ms ± 9.41 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Test Script

import icub_models
import rod
from rod.urdf.exporter import UrdfExporter

model_path = icub_models.get_model_file("iCubGazeboV2_5")

# First loading run
%timeit -n1 -r1 _ = rod.Sdf.load(model_path)

# Successive loading run
%timeit _ = rod.Sdf.load(model_path)

sdf = rod.Sdf.load(model_path)

# Switching model frame convention to URDF
%timeit sdf.model.switch_frame_convention(frame_convention=rod.FrameConvention.Urdf)

# Exporting to URDF
%timeit _ = UrdfExporter(pretty=True, gazebo_preserve_fixed_joints=True).to_urdf_string(sdf=sdf)

@flferretti flferretti self-assigned this May 15, 2025
@flferretti flferretti force-pushed the refactor_repo branch 2 times, most recently from 1729d04 to e9dae82 Compare May 15, 2025 17:10
@flferretti flferretti force-pushed the refactor_repo branch 3 times, most recently from 09c4db3 to 9a50870 Compare September 29, 2025 09:28
@flferretti flferretti changed the title Refactor codebase to improve maintainability and usability Refactor codebase to improve performance, maintainability and usability Sep 29, 2025
@flferretti flferretti requested a review from xela-95 September 29, 2025 10:59
@flferretti flferretti marked this pull request as ready for review September 29, 2025 10:59
- Add LRU cache for repeated SDF content processing to avoid redundant subprocess calls
- Optimize temporary file handling by writing directly to /tmp on Unix systems
- Add environment isolation (IGN_PARTITION/GZ_PARTITION) to prevent interference
- Implement batch processing support for multiple descriptions
- Add cache management with clear_cache() method
- Improve string parsing with early exit for SDF tag detection
- Use optimized subprocess bufsize for better I/O performance
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors key parts of ROD to improve performance and maintainability, notably optimizing Gazebo sdformat processing via caching and simplifying several internal transformation/export pipelines. It also modernizes packaging by switching to hatchling/hatch-vcs and improves the CLI’s UX and version reporting.

Changes:

  • Added cached processing + batch helpers for Gazebo sdformat model description processing.
  • Refactored frame convention switching and kinematics transform computation for simpler flow and caching.
  • Modernized packaging/build (move to hatchling, add console script), and improved CLI/help text and version handling.

Reviewed changes

Copilot reviewed 12 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/rod/utils/gazebo.py Adds cached sdformat processing and a batch API; changes temp-file strategy for speed.
src/rod/utils/frame_convention.py Refactors frame-convention switching into helper functions and uses updated TreeTransforms API.
src/rod/urdf/exporter.py Restructures URDF export code into smaller helpers, adds type aliases, and updates logging usage.
src/rod/sdf/common.py Moves imports to module scope for efficiency/clarity in pose transform computation.
src/rod/kinematics/tree_transforms.py Reworks transform computation to cache paths and adds cache management helpers.
src/rod/kinematics/kinematic_tree.py Refactors kinematic tree building into smaller helpers and changes indexing logic.
src/rod/main.py Improves CLI help text, adds robust version lookup, and ensures consistent exit behavior.
src/rod/init.py Exposes package version via rod._version when available.
setup.py Removes legacy setuptools entrypoint.
pyproject.toml Switches build backend to hatchling, adds console script entry, configures VCS version file.
pixi.lock Updates dependency lockfile.
environment.yml Adds dev/test and packaging tools; reorganizes dependency comments.
README.md Fixes spelling (“definitely”).
.gitignore Ignores hatch-generated _version.py and VS Code settings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@flferretti flferretti merged commit d98fc87 into main Mar 27, 2026
28 checks passed
@flferretti flferretti deleted the refactor_repo branch March 27, 2026 16:06
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.

2 participants