Replace O(N) blockList based lookups with O(1) blockIndex property#5798
Replace O(N) blockList based lookups with O(1) blockIndex property#5798Ashutoshx7 wants to merge 1 commit intosugarlabs:masterfrom
Conversation
|
✅ All Jest tests passed! This PR is ready to merge. |
kartikktripathi
left a comment
There was a problem hiding this comment.
While the idea of caching blockIndex to avoid repeated blockList.indexOf() scans is good, testing revealed a correctness issue.
Running structural operations (example: deleting random blocks via sendStackToTrash) caused a runtime exception because the cached blockIndex was used in places that previously relied on a fresh lookup. The original indexOf guaranteed a current valid position in blockList, but the cached value can refer to a block in a context where that assumption no longer holds.
This means the change alters behavioural guarantees rather than only improving performance, and certain operations can crash even though the index invariant itself appears intact.
The optimisation approach is valid, but the implementation needs additional safeguards or revalidation of indices before being safely merged.
Thanks!
633617c to
2fdedc8
Compare
|
✅ All Jest tests passed! This PR is ready to merge. |
thanx for the review |
|
The caching idea is good for performance, but replacing indexOf with property access removes a crucial safety check. If a block is removed or the project clears, structural operations (like sendStackToTrash) crash because they use stale indices. Please add a getter to revalidate the cached index before it's used (blockList[cachedIndex] === block). |
blockIndex is assigned at creation time and blocks are appended to blockList. There is no reordering of blockList, and deletion paths do not shift indices in a way that leaves live blocks in inconsistent positions. refreence: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice |
|
I think the confusion might be around this part that even if we don’t explicitly reorder the array, Array.splice() itself shifts the indices of elements after the removed item. So if blockList is modified via splice during deletion operations, cached indices after that point can become stale unless they’re updated or revalidated. That’s the case I was worried about. |
Description
This PR optimizes the performance of block lookups by replacing O(N)
blockList.indexOf(this)calls with O(1)this.blockIndexproperty access.Key Changes:
blockIndexproperty to the Block class (initialized to -1). This property is updated whenever a block is added toblockListvia makeNewBlock.blockList.indexOf(...)across js/block.js, js/blocks.js, js/piemenus.js, and js/activity.js with directblockIndexaccess.blockIndexdefaults to -1, preserving the behavior ofindexOf(returning -1) for blocks not in the list.Performance Impact
blockList(O(N)). For projects with thousands of blocks, this caused measurable overhead.Verification
npm testpassed (110 suites, 3108 tests).blockIndexorundefinedwere observed during interaction.