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

concurrentList.toPersistentList or concurrentMap.toPersistentMap is incorrectly implemented #155

Open
develar opened this issue Nov 2, 2023 · 1 comment

Comments

@develar
Copy link

develar commented Nov 2, 2023

The following code is used by SmallPersistentVector to create persistent list by some collection.

      val newBuffer = buffer.copyOf(size + elements.size)
            // TODO: investigate performance of elements.toArray + copyInto
            var index = size
            for (element in elements) {
                newBuffer[index++] = element
            }
            return SmallPersistentVector(newBuffer)

The issue is that concurrent collections in Java are weakly consistent.
So, you create a buffer with a size that is immediately incorrect.

So, you have weird runtime bugs like NPE or array index issues.

As the project uses Gradle, I am not yet able to manage to open it in IDEA and write a test.

JDK (List.copyOf) uses toArray ((List<E>)List.of(coll.toArray())), which is a safe and efficient solution.

@develar develar changed the title concurrentList.toPersistentList or toConcurrentMap.toPersistentList is incorrectly implemented concurrentList.toPersistentList or concurrentMap.toPersistentMap is incorrectly implemented Nov 2, 2023
@qurbonzoda
Copy link
Contributor

Hi, Thanks for reporting the issue.
The library does not prioritise correct handling of concurrent collections.
So it becomes the user responsibility to make sure the elements collection consistency.
It might make sense to pass a snapshot of your concurrent collection to the function instead.

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

No branches or pull requests

2 participants