Skip to content

Import time#1727

Merged
paul-nechifor merged 4 commits intodevfrom
import-time
Apr 1, 2026
Merged

Import time#1727
paul-nechifor merged 4 commits intodevfrom
import-time

Conversation

@Dreamsorcerer
Copy link
Copy Markdown
Collaborator

@Dreamsorcerer Dreamsorcerer commented Apr 1, 2026

Problem

This improves import time for some dimos.core modules, which are needed for #1543. Separating the import improvements for easier review.

Solution

To avoid having too many repetitive inline imports harming readability, JpegEncoderMixin is moved to a separate module to avoid the heavy import of Image when importing the other classes in that module,

@Dreamsorcerer Dreamsorcerer changed the base branch from main to dev April 1, 2026 18:30
@bri-tong
Copy link
Copy Markdown

bri-tong commented Apr 1, 2026

Code Review

Summary

Clean refactoring that defers heavy imports (scipy, langchain_core, Image/cv2/rerun via JPEG codecs) to improve module load time. The approach is sound — splitting JpegEncoderMixin/JpegLCM into jpeg_lcm.py and converting top-level imports to deferred inline imports is a well-targeted strategy. However, there is one regression that will cause a runtime crash.


🔴 Blocking Issue

NameError at runtime in dimos/core/transport.py

The from dimos.utils import colors import was removed (line 26), but colors.green() and colors.blue() are still used in PubSubTransport.__str__():

def __str__(self) -> str:
    return (
        colors.green(f"{self.__class__.__name__}(")  # ❌ NameError
        + colors.blue(self.topic)
        + colors.green(")")
    )

Any call to str() on a transport instance (e.g. during logging in Blueprint._connect_streams) will crash.

Fix: Either restore the import, or defer it into __str__() if it has undesirable transitive deps:

def __str__(self) -> str:
    from dimos.utils import colors
    return (
        colors.green(f"{self.__class__.__name__}(")
        + colors.blue(self.topic)
        + colors.green(")")
    )

Non-blocking Suggestions

  1. Quaternion.py — duplicate deferred import: from scipy.spatial.transform import Rotation appears identically in both from_rotation_matrix() and to_euler(). Python caches module imports so there's no performance penalty, but a shared private helper like _scipy_rotation() would keep the deferral in one place and reduce visual noise. Minor readability preference.

  2. Backwards-compat re-export: JpegEncoderMixin was removed from encoders.py and JpegLCM from lcmpubsub.py. I verified all consumers on dev import via dimos.core.transport.JpegLcmTransport, so nothing is broken today. But if external users or downstream forks import from the old module paths, they'll get ImportError. Consider adding a deprecation re-export in lcmpubsub.py if backwards compat matters for these internal symbols.

  3. New module jpeg_lcm.py: Well-structured — good docstring explaining why it exists, correct MRO composition. No issues.


Questions / Clarifications

None — the intent is clear from the PR description and code.

@paul-nechifor paul-nechifor merged commit cff21f4 into dev Apr 1, 2026
14 of 15 checks passed
@paul-nechifor paul-nechifor deleted the import-time branch April 1, 2026 19:19
jeff-hykin pushed a commit that referenced this pull request Apr 4, 2026
Co-authored-by: Sam Bull <Sam.B@snowfalltravel.com>
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.

3 participants