I have a list containing 4 million ordered element...
# codereview
z
I have a list containing 4 million ordered elements, but I'd like to extract the first from each group within that list. The list is kind of like a linear tree, the first element is the parent and all following elements (up to a point) are its children. I'm using a sequence, but it's kind of slow on KMP/JS Browser(~20 seconds to pull 200 elements). This is in a KMP react app, but this block is where the iteration happens.
Copy code
useEffect(setTopLevel, props.pluginElementMarkers) {
        val last = props.pluginElementMarkers.lastOrNull()
        if (last == null) {
            return@useEffect
        }
        val sequence = props.pluginElementMarkers.asSequence()
        val topLevelList = mutableListOf<PluginElementMarker>()
        var nextMarker = sequence.take(1).single()
        while (nextMarker != last) {
            val skip = nextMarker.skip
            val size = when (nextMarker) {
                is GroupElementMarker -> nextMarker.elementSize
                is RecordElementMarker -> nextMarker.elementSize
                is SubRecordElementMarker -> nextMarker.elementSize.toUInt()
            }.toLong()

            topLevelList += nextMarker
            nextMarker = sequence
                .dropWhile { marker -> marker.skip < skip + size }
                .take(1)
                .singleOrNull() ?: last
        }
        console.log("actually done")
        setTopLevel(topLevelList)
    }
Example output
Copy code
RecordElementMarker(skip=0, serialName=TypeTag(string=TES4), elementSize=78, isDataCompressed=false)

GroupElementMarker(skip=78, serialName=TypeTag(string=GRUP), elementSize=96862)

...

GroupElementMarker(skip=249738908, serialName=TypeTag(string=GRUP), elementSize=13763)

GroupElementMarker(skip=249752671, serialName=TypeTag(string=GRUP), elementSize=741)