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

Use iteration instead of recursion in Graphs.hasCycle. #7317

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

copybara-service[bot]
Copy link
Contributor

Use iteration instead of recursion in Graphs.hasCycle.

This can avoid stack overflows for graphs with long paths.

RELNOTES=graph: Improved Graphs.hasCycle to avoid causing StackOverflowError for long paths.

This can avoid stack overflows for graphs with long paths.

RELNOTES=`graph`: Improved `Graphs.hasCycle` to avoid causing `StackOverflowError` for long paths.
PiperOrigin-RevId: 653681462
@copybara-service copybara-service bot merged commit 63734b9 into master Jul 18, 2024
2 checks passed
@copybara-service copybara-service bot deleted the test_652886339 branch July 18, 2024 17:56
@perceptron8
Copy link
Contributor

I'm happy and slightly disappointed at the same time! ;) Have you considered #6059 instead? Main loop feels a little bit simpler plus it avoids successors copying (on the other hand, successors are visited in reverse order, which obviously doesn't change the result, but is worth noting). WDYT?

@cpovirk
Copy link
Member

cpovirk commented Jul 18, 2024

Ouch, sorry.

@jrtom, any thoughts? You may recall from 2 hours ago that I described my own approach as "clumsy," and I don't think you went out of your way to disagree... :)

@perceptron8
Copy link
Contributor

I've just realized that in #6059 successors are actually "copied" (by pushing them one by one to the stack - that's the source of mentioned reverse order), which makes those PRs more similar than I thought. As for simplicity, let's rely on @jrtom taste.

@jrtom
Copy link
Member

jrtom commented Jul 18, 2024

@perceptron8 I had completely forgotten about your previous offer; my apologies. 😅

I think I slightly prefer cpovirk@'s version mostly because it retains the abstraction of the subgraphHasCycle method (which makes it, to me, a little easier to read) but I don't think that the difference matters a great deal; they are pretty similar and I believe that they both do the job.

On that basis I'd suggest we go with the solution that we already have committed, but @perceptron8 I really appreciate your attention to this issue and your desire to make this code base as good as it can be. Thank you! 😃

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.

4 participants