mirror of
https://github.com/stablyai/orca.git
synced 2026-09-22 00:02:31 +00:00
perf: sum omitted workspace sizes without intermediate objects (#19491)
Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { expect, it, vi } from 'vitest'
|
||||
import { compactWorkspaceSpaceItems } from './workspace-space-compaction'
|
||||
import type { WorkspaceSpaceItem } from './workspace-space-types'
|
||||
|
||||
it('sums omitted sizes without constructing a replacement object per omitted item', () => {
|
||||
const items: WorkspaceSpaceItem[] = Array.from({ length: 10000 }, (_, index) => ({
|
||||
name: String(index),
|
||||
path: String(index),
|
||||
kind: 'file',
|
||||
sizeBytes: index
|
||||
}))
|
||||
const original = Array.prototype.reduce
|
||||
let objectAccumulators = 0
|
||||
const spy = vi.spyOn(Array.prototype, 'reduce').mockImplementation(function (
|
||||
this: unknown[],
|
||||
callback,
|
||||
initial: unknown
|
||||
) {
|
||||
if (initial && typeof initial === 'object' && 'name' in initial && initial.name === 'Other') {
|
||||
objectAccumulators += this.length
|
||||
}
|
||||
return Reflect.apply(original, this, [callback, initial])
|
||||
})
|
||||
let result: ReturnType<typeof compactWorkspaceSpaceItems>
|
||||
try {
|
||||
result = compactWorkspaceSpaceItems(items)
|
||||
} finally {
|
||||
spy.mockRestore()
|
||||
}
|
||||
expect(objectAccumulators).toBe(0)
|
||||
expect(result!.topLevelItems).toHaveLength(48)
|
||||
expect(result!.omittedTopLevelItemCount).toBe(9953)
|
||||
expect(result!.omittedTopLevelSizeBytes).toBe((9952 * 9953) / 2)
|
||||
expect(result!.topLevelItems[0]).toBe(items[9999])
|
||||
expect(items[0].sizeBytes).toBe(0)
|
||||
})
|
||||
|
||||
it('preserves small-list size ties and empty results', () => {
|
||||
expect(compactWorkspaceSpaceItems([]).topLevelItems).toEqual([])
|
||||
const items: WorkspaceSpaceItem[] = ['b', 'a'].map((name) => ({
|
||||
name,
|
||||
path: name,
|
||||
kind: 'file',
|
||||
sizeBytes: 1
|
||||
}))
|
||||
expect(compactWorkspaceSpaceItems(items).topLevelItems.map((item) => item.name)).toEqual([
|
||||
'a',
|
||||
'b'
|
||||
])
|
||||
})
|
||||
@@ -19,23 +19,20 @@ export function compactWorkspaceSpaceItems(items: WorkspaceSpaceItem[]): {
|
||||
}
|
||||
|
||||
const visible = sorted.slice(0, WORKSPACE_SPACE_MAX_TOP_LEVEL_ITEMS - 1)
|
||||
const omitted = sorted.slice(WORKSPACE_SPACE_MAX_TOP_LEVEL_ITEMS - 1)
|
||||
const other = omitted.reduce<WorkspaceSpaceItem>(
|
||||
(acc, item) => ({
|
||||
...acc,
|
||||
sizeBytes: acc.sizeBytes + item.sizeBytes
|
||||
}),
|
||||
{
|
||||
name: 'Other',
|
||||
path: '',
|
||||
kind: 'other',
|
||||
sizeBytes: 0
|
||||
}
|
||||
)
|
||||
let omittedSizeBytes = 0
|
||||
for (let index = visible.length; index < sorted.length; index += 1) {
|
||||
omittedSizeBytes += sorted[index].sizeBytes
|
||||
}
|
||||
const other: WorkspaceSpaceItem = {
|
||||
name: 'Other',
|
||||
path: '',
|
||||
kind: 'other',
|
||||
sizeBytes: omittedSizeBytes
|
||||
}
|
||||
|
||||
return {
|
||||
topLevelItems: [...visible, other],
|
||||
omittedTopLevelItemCount: omitted.length,
|
||||
omittedTopLevelItemCount: sorted.length - visible.length,
|
||||
omittedTopLevelSizeBytes: other.sizeBytes
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user