kotlinx.collections.immutable icon indicating copy to clipboard operation
kotlinx.collections.immutable copied to clipboard

concurrentList.toPersistentList or concurrentMap.toPersistentMap is incorrectly implemented

Open develar opened this issue 2 years ago • 1 comments
trafficstars

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 avatar Nov 02 '23 07:11 develar

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.

qurbonzoda avatar Nov 16 '23 19:11 qurbonzoda