Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cycle checking when calculating min delay from nearest physical action #2459

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lsk567
Copy link
Collaborator

@lsk567 lsk567 commented Jan 21, 2025

As the title suggests, currently, when actions for a deep loop, the recursion never terminates and leads to stack overflow. This PR addresses the issue by introducing a visited set.

@lsk567 lsk567 added the bugfix label Jan 21, 2025
Copy link
Collaborator

@edwardalee edwardalee left a comment

Choose a reason for hiding this comment

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

Thanks for catching and fixing this! It would be nice to add a test case.

TimeValue minDelay = TimeValue.MAX_VALUE;
for (TriggerInstance<? extends Variable> trigger : reaction.triggers) {
if (trigger.getDefinition() instanceof Action action) {
ActionInstance actionInstance = (ActionInstance) trigger;
if (action.getOrigin() == ActionOrigin.PHYSICAL) {
System.out.println("Physical action: " + action);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest deleting this debug output.

Comment on lines +714 to +717
}
else if (action.getOrigin() == ActionOrigin.LOGICAL) {
System.out.println("Logical action: " + action);
System.out.println("Depends on: " + actionInstance.getDependsOnReactions());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
else if (action.getOrigin() == ActionOrigin.LOGICAL) {
System.out.println("Logical action: " + action);
System.out.println("Depends on: " + actionInstance.getDependsOnReactions());
} else if (action.getOrigin() == ActionOrigin.LOGICAL) {

You could also turn these into debug outputs.

// Avoid a potentially deep loop by checking the visited set.
if (!visited.contains(uReaction)) {
visited.add(uReaction); // Mark the upstream reaction as visited.
System.out.println("Upstream reaction: " + uReaction);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Delete or convert to debug output.

@@ -713,7 +735,8 @@ public TimeValue findNearestPhysicalActionTrigger(ReactionInstance reaction) {
// Outputs of contained reactions
PortInstance outputInstance = (PortInstance) trigger;
for (ReactionInstance uReaction : outputInstance.getDependsOnReactions()) {
TimeValue uMinDelay = findNearestPhysicalActionTrigger(uReaction);
visited.add(uReaction); // Mark the upstream reaction as visited.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this add premature? Won't the recursive call on the next line add it if it isn't there already? And if added here, won't the recursive call actually not do anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants