Why ActionQueue / InsertInfo decide to clear the visited list on each entity?

Why ActionQueue.buildTransitiveDependencies(Set<InsertInfo> visited) is clearing the visited on each iteration ?

My initial concern was about the huge time used to execute a findByxxx query.
This request happens in a spring batch that is looping on days (~400) and create a result that is a chain of entities.
On each day, we are executing the findByxxx query that start a flush, and this flush is extremely costly because we are computing an huge amount of Object.hashcode().

I will fix the issue with a query hints to change the flush mode for this query.

But when looking the code (6.6.36, but the code seems to be the same in v7), I see we are looping on all the insertInfos in InsertInfoSorted.sort() (up to 4000 in my case when close to the end of my iterations) and passing and HashSet that contains the visited InsertAction to the method InsertInfo.buildTransitiveDependencies(visited)

Then the method InsertInfo.buildTransitiveDependencies(visited) iterate on the incoming transitiveIncomingDependencies of the current InsertInfo.
My entities are chained and the transitiveIncomingDependencies contains a significant part of my 4000 entities.

Why the code clears the visited hashset at the end the method InsertInfo.buildTransitiveDependencies(visited) ?

With this, we are relooping on all the entities on each iteration of buildTransitiveDependencies method.

In term of design it’s suprising as the Hashset visited is only created for this method to decide to clear it. But mot important, I can’t understand why we clear this visited list.

I don’t know off the top of my head why I chose this design back when I did this, but if you have an idea to improve the design or performance, please provide a PR.
ActionQueue is quite complicated and we are planning to do a redesign which you can track for progress by following HHH-17922 if you want.

The improvement I see is to remove the visited.clear() because I don’t see why we need it.
When removed, all the hibernate tests are still green.

But as you said the part of the code is quite complex and sensitive. I probably miss something and I want to check here my comprehension before create a PR.

And in my case it seems working well with a batch duration of 1min instead of 16 min.