Skip to content

Implement lazy directives for Ramble - #1756

Draft
linsword13 wants to merge 4 commits into
Ramble-Project:developfrom
linsword13:lazy-directives
Draft

linsword13 wants to merge 4 commits into
Ramble-Project:developfrom
linsword13:lazy-directives

Conversation

@linsword13

Copy link
Copy Markdown
Collaborator

This is inspired by Spack's lazy directive (spack/spack#51881).

  • Add in the dict descriptor (as a non-data descriptor) to support the lazy initiailization
  • Remove the convert_class_attributes since now the lazy execution allows us to perform copy on first access
  • Update the various directives to support the new semantics. Specifically, it now requires listing out all mutated dicts of a directive
  • Also unify the various language meta directives, so now they are all essentially DirectiveMeta
  • Update the mirror test since before it implicitly relied on the mutation happening both at the class and the instance level

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors Ramble's directive system to support lazy evaluation of directives upon first access using a new DirectiveDictDescriptor descriptor. It removes the need for explicit class-to-instance attribute conversion and updates various base classes to use DirectiveMeta directly as their metaclass. Additionally, comprehensive tests are added to verify lazy evaluation, inheritance, and attribute isolation. The review feedback highlights a critical issue where instance-level modifications to these lazily evaluated directive dictionaries would be lost during cloning or copying. It is recommended to dynamically copy all evaluated directive dictionaries from self.__dict__ to the cloned or copied instances in both ApplicationBase.clone() and ObjectMixin.copy().

Comment thread var/ramble/repos/builtin/base_classes/application-base/base_class.py Outdated
Comment thread var/ramble/repos/builtin/base_classes/object-mixin/base_class.py
@ramble-pr-bot

ramble-pr-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

Ramble Performance Test Metrics

Results produced with commit: 85adeac

Test Name Outcome Duration (s) Most Recent Run (s) Last 5 Avg (s)
test_analyze_large_file passed 1.0998 1.0761 (893e6bf) 1.0855
test_large_template_expansion passed 1.1814 1.1868 (893e6bf) 1.1757
test_many_experiments passed 28.1515 23.4889 (893e6bf) 23.2700
test_many_objects_defaults passed 15.1407 13.3383 (893e6bf) 12.2945
test_matrix_filter_perf passed 1.0450 1.0997 (893e6bf) 1.0671

@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.78870% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.08%. Comparing base (6d999da) to head (e9a802e).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
lib/ramble/ramble/language/language_base.py 94.23% 9 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1756      +/-   ##
===========================================
+ Coverage    94.07%   94.08%   +0.01%     
===========================================
  Files          370      370              
  Lines        37399    37554     +155     
===========================================
+ Hits         35182    35333     +151     
- Misses        2217     2221       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@linsword13

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors Ramble's directive system to support lazy evaluation on first access, introducing the DirectiveDictDescriptor class and removing the eager conversion of class attributes to instance attributes across all base classes. A comprehensive test suite has been added to verify lazy evaluation, inheritance, and isolation. The review feedback highlights critical inheritance issues where using getattr on classes or objects can mistakenly retrieve parent class attributes (such as evaluated directive dictionaries or preferred versions) instead of checking the subclass's own __dict__ directly, which would bypass subclass directive execution or raise incorrect errors.

Comment thread lib/ramble/ramble/language/language_base.py Outdated
Comment thread lib/ramble/ramble/language/language_base.py Outdated
Comment thread lib/ramble/ramble/language/shared_language.py Outdated
@linsword13

Copy link
Copy Markdown
Collaborator Author

/gemini review

@linsword13
linsword13 force-pushed the lazy-directives branch 2 times, most recently from 7efbad1 to 2b398d1 Compare September 3, 2026 18:29
@linsword13

Copy link
Copy Markdown
Collaborator Author

/gemini review

@linsword13
linsword13 marked this pull request as ready for review September 4, 2026 00:35

@rfbgo rfbgo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two random questions:

  1. The bot reports a slow down from this. Is it it true? If so I think there are some places we might be able to improve perf slightly (eg optimized copies)
  2. I asked gemini to review this PR wrt "leaking" (it says it will leak). I kind of expect this is something you were already careful about, but I wanted to double check we believe this is indeed safe?

DirectiveMeta._directives_to_be_executed = [
(n, fn)
for n, fn in DirectiveMeta._directives_to_be_executed
if fn != arg

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can this be expressed safely as is not?

) -> Callable[..., Any]:
"""Decorator for Ramble directives."""
if dicts is None or dicts == ():
dicts_tuple: Tuple[str, ...] = ()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't really understand what this is doing, can you help me understand? Elsewhere this stuff is keyed on func_name, right? It feels like this could accidentally turn stuff into a noop?

def preferred_version(self, value: Optional[ObjectVersion]):
self._preferred_version = value

def _copy_evaluated_directives(self, target):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this able to retain preferred_version too?


def _pop_default_args() -> dict:
return DirectiveMeta._default_args.pop()
_UNSET = object()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Something odd and interesting is leaking in an info print, I (well.. gemini...) thinks it's this:

ramble info --type modifier ...
...
default_mode:
    <object object at 0x1052e1e50>

@linsword13

Copy link
Copy Markdown
Collaborator Author

Two random questions:

  1. The bot reports a slow down from this. Is it it true? If so I think there are some places we might be able to improve perf slightly (eg optimized copies)
  2. I asked gemini to review this PR wrt "leaking" (it says it will leak). I kind of expect this is something you were already careful about, but I wanted to double check we believe this is indeed safe?

Great questions:

For 1. it does seem to cause a slowdown on some perf tests. One reason is that previously the directive initialization cost are part of the startup, which wasn't counted by the benchmarking. And now with the lazy init, these init times are now part of the operations that are being benchmarked. So this is more or less an accounting thing. Other factors include the more works that are needed during copy (as we can no longer rely on the class-level attributes.) I should do a more thorough analysis though to see if any improvements are possible, and at least have a better breakdown of the timing.

For 2. I did try to look for leaking, but I will do another pass to see where I missed.

This is inspired by Spack's lazy directive (spack/spack#51881).

* Add in the dict descriptor (as a non-data descriptor) to support the lazy initiailization
* Remove the `convert_class_attributes` since now the lazy execution allows us to perform copy on first access
* Update the various directives to support the new semantics. Specifically, it now requires listing out all mutated dicts of a directive
* Also unify the various language meta directives, so now they are all essentially DirectiveMeta
* Update the mirror test since before it implicitly relied on the mutation happening both at the class and the instance level

Signed-off-by: Lin Guo <linsword13@gmail.com>
Signed-off-by: Lin Guo <linsword13@gmail.com>
Signed-off-by: Lin Guo <linsword13@gmail.com>
- Map default_mode to public descriptor default_usage_mode in info.py to prevent _UNSET leakage.
- Preserve class-level attribute values matching directive names in DirectiveMeta via _class_directive_values.
- Deduplicate directives by callable identity in DirectiveMeta and eagerly execute directives without target dictionaries (dicts=()).
- Add execution plan caching (_execution_plan_cache) in DirectiveMeta for O(1) membership checks.
- Scope directive descriptors to declared language types via _type_scoped_dicts to avoid leaking inapplicable descriptors.
- Fix identity comparison in remove_directives (fn is not arg).
- Clean up unused system attribute declarations in system-base/base_class.py.
- Retain selective deepcopy in ObjectMixin._copy_evaluated_directives to ensure cloned experiment isolation while fast-pathing empty and primitive values.
- Eliminate ad-hoc _preferred_version state, _preferred_version_class, and DirectiveMeta._current_directive; derive preferred_version as a computed property from known_versions and attach _defining_class directly to ObjectVersion.

Signed-off-by: Lin Guo <linsword13@gmail.com>
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.11940% with 26 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/ramble/ramble/language/language_base.py 91.47% 19 Missing ⚠️
...os/builtin/base_classes/object-mixin/base_class.py 85.00% 6 Missing ⚠️
...amble/ramble/test/language/test_lazy_directives.py 99.63% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@linsword13
linsword13 marked this pull request as draft September 14, 2026 23:41
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