Select the types of activity you want to include in your feed.
Optimize LIS reconstruction in child reordering
Use pre-sized LIS buffers and Int32Array predecessor tracking to reduce allocations and avoid unshift-heavy reconstruction in the reordering hot path while preserving behavior.
···946946 const n = sequence.length
947947 if (n === 0) return []
948948949949- // smallestEnding[i] = smallest ending value of any increasing subsequence of length i+1
950950- const smallestEnding: Array<number> = []
951951- // indices[i] = index in sequence where smallestEnding[i] occurs
952952- const indices: Array<number> = []
953953- // prev[i] = previous index in the LIS ending at sequence[i]
954954- const prev: Array<number> = new Array(n)
949949+ const smallestEnding = new Array<number>(n)
950950+ const indices = new Array<number>(n)
951951+ const prev = new Int32Array(n)
952952+ prev.fill(-1)
955953956956- // Build the LIS by processing each value
954954+ let lisLength = 0
955955+957956 for (let i = 0; i < n; i++) {
958957 const val = sequence[i]
959959- if (val === undefined) continue // Skip new nodes (not in original sequence)
958958+ if (val === undefined) continue
960959961961- // Binary search: find where this value fits in smallestEnding
962960 let left = 0
963963- let right = smallestEnding.length
961961+ let right = lisLength
964962965963 while (left < right) {
966964 const mid = Math.floor((left + right) / 2)
···968966 else right = mid
969967 }
970968971971- // Link this element to the previous one in the subsequence
972969 prev[i] = left > 0 ? indices[left - 1]! : -1
973970974974- // Either extend the sequence or update an existing position
975975- if (left === smallestEnding.length) {
976976- // Extend: this value is larger than all previous endings
977977- smallestEnding.push(val)
978978- indices.push(i)
979979- } else {
980980- // Update: found a better (smaller) ending for this length
981981- smallestEnding[left] = val
982982- indices[left] = i
983983- }
971971+ smallestEnding[left] = val
972972+ indices[left] = i
973973+ if (left === lisLength) lisLength++
984974 }
985975986986- // Reconstruct the actual indices that form the LIS
987987- const result: Array<number> = []
988988- if (indices.length === 0) return result
976976+ if (lisLength === 0) return []
977977+978978+ const result = new Array<number>(lisLength)
979979+ let curr = indices[lisLength - 1]!
989980990990- // Walk backwards through prev links to build the LIS
991991- let curr: number | undefined = indices[indices.length - 1]
992992- while (curr !== undefined && curr !== -1) {
993993- result.unshift(curr)
994994- curr = prev[curr]
981981+ for (let i = lisLength - 1; i >= 0; i--) {
982982+ result[i] = curr
983983+ curr = prev[curr]!
995984 }
996985997986 return result