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

[MNG-8575] Replace a list with O(N²) performance by O(N) at least during iteration. #2092

Merged
merged 4 commits into from
Feb 11, 2025

Conversation

desruisseaux
Copy link
Contributor

@desruisseaux desruisseaux commented Feb 7, 2025

JIRA issue: MNG-8575

The use of AbstractList with an implementation that creates the whole list every time that get(int) is invoked implies that iterations over that list would have a performance cost of O(N²) where N is the number of elements. Extending AbstractSequentialList instead brings back the performance cost to O(N) at least during iterations.

@desruisseaux desruisseaux requested a review from gnodet February 7, 2025 09:04
@desruisseaux
Copy link
Contributor Author

It brings a related question. The following code also has a O(N²) performance cost if a large number N of sources are added through this method:

    private List<SourceRoot> sources = new CopyOnWriteArrayList<>();

    public void addSourceRoot(SourceRoot source) {
        if (!sources.contains(source)) {
            sources.add(source);
        }
    }

We could reduce the cost to O(N) by using LinkedHashSet instead of a list. However, CopyOnWriteArrayList is thread-safe while LinkedHashSet is not. Do we really need thread-safety for that particular field while everything else in MavenProject is not thread-safe? I agree that we may want thread-safety later, but maybe it would need to a more general strategy (e.g. with immutable records)?

@cstamas
Copy link
Member

cstamas commented Feb 8, 2025

Maybe we want two methods to add sources? And one could be a "batch" (several sources at once) to be used by Project Builder and other adding one by one like for example some plugin may be? Am unsure would the rate of second call justify complicating things....

@desruisseaux
Copy link
Contributor Author

A batch method would not fundamentally change the O(N²) nature of the operation, unless it temporarily copy the list in an hash set, in which case we lost the concurrency characteristics of CopyOnWriteArrayList.

I think that the first question still: do we need to care about concurrency or thread safety now, given that the rest of the class is not thread-safe? If no, we can just replace CopyOnWriteArrayList by LinkedHashSet and we are done.

@gnodet
Copy link
Contributor

gnodet commented Feb 10, 2025

A batch method would not fundamentally change the O(N²) nature of the operation, unless it temporarily copy the list in an hash set, in which case we lost the concurrency characteristics of CopyOnWriteArrayList.

I think that the first question still: do we need to care about concurrency or thread safety now, given that the rest of the class is not thread-safe? If no, we can just replace CopyOnWriteArrayList by LinkedHashSet and we are done.

I don't think we need thread safety here. Thread safe

A batch method would not fundamentally change the O(N²) nature of the operation, unless it temporarily copy the list in an hash set, in which case we lost the concurrency characteristics of CopyOnWriteArrayList.

I think that the first question still: do we need to care about concurrency or thread safety now, given that the rest of the class is not thread-safe? If no, we can just replace CopyOnWriteArrayList by LinkedHashSet and we are done.

I don't think we do. We can defer that at the time we reverse the MavenProject implementation to be implemented on top of Project.

…rate over all previous values every time that a new value is added.
@gnodet gnodet added this to the 4.0.0-rc-3 milestone Feb 11, 2025
@gnodet gnodet changed the title Replace a list with O(N²) performance by O(N) at least during iteration. [MNG-8575] Replace a list with O(N²) performance by O(N) at least during iteration. Feb 11, 2025
@gnodet gnodet merged commit f2bc813 into apache:master Feb 11, 2025
13 checks passed
@desruisseaux desruisseaux deleted the sequential_list branch February 11, 2025 14:56
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