From f731557b5ab67986168ad5546490b0635493b71e Mon Sep 17 00:00:00 2001 From: krauders <129333359+krauders@users.noreply.github.com> Date: Thu, 3 Apr 2025 17:22:18 -0400 Subject: [PATCH] Update handlecache.ts (#24233) ## Description While testing the performance of SharedMatrix when loading large CSV datasets of 100k-500k rows and 10-12 columns, it was identified that cacheMiss() contributes significantly to the overall performance. This change removes the spread operator and reduces the number of copies and edits made to the handles array. Performance testing shows a 3x improvement in time taken to load large dataset, and a 50-95% reduction in memory usage. --- packages/dds/matrix/src/handlecache.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/dds/matrix/src/handlecache.ts b/packages/dds/matrix/src/handlecache.ts index 315985f32bca..66080232c99f 100644 --- a/packages/dds/matrix/src/handlecache.ts +++ b/packages/dds/matrix/src/handlecache.ts @@ -72,11 +72,10 @@ export class HandleCache implements IVectorConsumer { /** * Used by {@link HandleCache.cacheMiss} to retrieve handles for a range of positions. */ - private getHandles(start: number, end: number): Handle[] { + private getHandles(start: number, end: number, handles: Handle[]): Handle[] { // TODO: This can be accelerated substantially using 'walkSegments()'. The only catch // is that - const handles: Handle[] = []; const { vector } = this; for (let pos = start; pos < end; pos++) { @@ -102,16 +101,15 @@ export class HandleCache implements IVectorConsumer { // the handle cache). if (_position < this.start) { - this.handles = [...this.getHandles(_position, this.start), ...this.handles]; + const handles: Handle[] = []; + this.getHandles(_position, this.start, handles); + handles.push(...this.handles); + this.handles = handles; this.start = _position; return this.handles[0]; } else { ensureRange(_position, this.vector.getLength()); - - this.handles = [ - ...this.handles, - ...this.getHandles(this.start + this.handles.length, _position + 1), - ]; + this.getHandles(this.start + this.handles.length, _position + 1, this.handles); return this.handles[this.handles.length - 1]; } }