Skip to content

Commit

Permalink
fix #117: resizing buffer before rearranging elements into "safe buff…
Browse files Browse the repository at this point in the history
…er space"
  • Loading branch information
bbohlender committed Sep 30, 2024
1 parent 9afbc00 commit 0f465cf
Show file tree
Hide file tree
Showing 4 changed files with 15,975 additions and 5,413 deletions.
2 changes: 1 addition & 1 deletion packages/uikit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"./internals": "./dist/internals.js"
},
"scripts": {
"test": "mocha ./tests/flex.spec.ts",
"test": "mocha ./tests/*.spec.ts",
"build": "node --loader ts-node/esm scripts/extract-core-component-properties.ts && tsc -p ./tsconfig.build.json",
"generate": "node --loader ts-node/esm scripts/flex-generate-setter.ts",
"check:prettier": "prettier --check src scripts tests",
Expand Down
8 changes: 7 additions & 1 deletion packages/uikit/src/panel/instanced-panel-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,17 @@ export class InstancedPanelGroup {
if (this.elementCount > this.bufferElementSize) {
//buffer is to small to host the current elements
this.resize()
//we need to execute updateSortedBucketsAllocation after resize so that updateSortedBucketsAllocation has enough space to arrange all the elements
updateSortedBucketsAllocation(this.buckets, this.activateElement, this.bufferCopyWithin)
} else if (this.elementCount <= this.bufferElementSize / 3) {
//we need to execute updateSortedBucketsAllocation first, so we still have access to the elements in the space that will be removed by the resize
//TODO: this could be improved since now we are re-arraging in place and then copying. we could rearrange while copying. Not sure if faster though?
updateSortedBucketsAllocation(this.buckets, this.activateElement, this.bufferCopyWithin)
//buffer is at least 300% bigger than the needed space
this.resize()
} else {
updateSortedBucketsAllocation(this.buckets, this.activateElement, this.bufferCopyWithin)
}
updateSortedBucketsAllocation(this.buckets, this.activateElement, this.bufferCopyWithin)
this.mesh!.count = this.elementCount
this.mesh!.visible = true
}
Expand Down
2 changes: 0 additions & 2 deletions packages/uikit/tests/allocation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ function createSortedBuckets(data: Float32Array, buckets: Array<Bucket<Element>>
const activateElement = (element: Element, bucket: Bucket<Element>, index: number) => {
data[bucket.offset + index] = element.value
element.index = index
console.log('activate', element.value, 'at', bucket.offset, '/', index, ':', ...data)
}
const setElementIndex = (element: Element, index: number) => {
element.index = index
}
const copyBufferWithin = (targetIndex: number, startIndex: number, endIndex: number) => {
data.copyWithin(targetIndex, startIndex, endIndex)
console.log('copy ', startIndex, '-', endIndex, 'to', targetIndex, ':', ...data)
}
return {
add(value: number, bucketIndex: number): Element {
Expand Down
Loading

0 comments on commit 0f465cf

Please sign in to comment.