Skip to content

Commit

Permalink
[REF] zones: improve performances on 'positions'
Browse files Browse the repository at this point in the history
Avoid using the `sort` function on a lists of two arguments,
divide the computation time on the line of code by 100

closes #4162

Task: 3901818
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
laa-odoo committed May 10, 2024
1 parent 9a49647 commit 5ee2cad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions src/helpers/zones.ts
Expand Up @@ -477,8 +477,8 @@ export function isOneDimensional(zone: Zone): boolean {
*/
export function positions(zone: Zone): Position[] {
const positions: Position[] = [];
const [left, right] = [zone.right, zone.left].sort((a, b) => a - b);
const [top, bottom] = [zone.top, zone.bottom].sort((a, b) => a - b);

const { left, right, top, bottom } = reorderZone(zone);
for (const col of range(left, right + 1)) {
for (const row of range(top, bottom + 1)) {
positions.push({ col, row });
Expand All @@ -487,6 +487,16 @@ export function positions(zone: Zone): Position[] {
return positions;
}

export function reorderZone(zone: Zone): Zone {
if (zone.left > zone.right) {
zone = { left: zone.right, right: zone.left, top: zone.top, bottom: zone.bottom };
}
if (zone.top > zone.bottom) {
zone = { left: zone.left, right: zone.right, top: zone.bottom, bottom: zone.top };
}
return zone;
}

/**
* This function returns a zone with coordinates modified according to the change
* applied to the zone. It may be possible to change the zone by resizing or moving
Expand Down Expand Up @@ -607,15 +617,6 @@ export function findCellInNewZone(oldZone: Zone, currentZone: Zone): Position {
return { col, row };
}

export function organizeZone(zone: Zone): Zone {
return {
top: Math.min(zone.top, zone.bottom),
bottom: Math.max(zone.top, zone.bottom),
left: Math.min(zone.left, zone.right),
right: Math.max(zone.left, zone.right),
};
}

export function positionToZone(position: Position): Zone {
return { left: position.col, right: position.col, top: position.row, bottom: position.row };
}
Expand Down
8 changes: 4 additions & 4 deletions src/selection_stream/selection_stream_processor.ts
Expand Up @@ -3,8 +3,8 @@ import {
deepEquals,
isEqual,
isInside,
organizeZone,
positionToZone,
reorderZone,
union,
} from "../helpers";
import { _t } from "../translation";
Expand Down Expand Up @@ -226,7 +226,7 @@ export class SelectionStreamProcessorImpl implements SelectionStreamProcessor {
}
let result: Zone | null = anchor.zone;
const expand = (z: Zone) => {
z = organizeZone(z);
z = reorderZone(z);
const { left, right, top, bottom } = this.getters.expandZone(sheetId, z);
return {
left: Math.max(0, left),
Expand Down Expand Up @@ -257,7 +257,7 @@ export class SelectionStreamProcessorImpl implements SelectionStreamProcessor {
const newTop = this.getNextAvailableRow(deltaRow, refCol, top + (n - 1));
result = top + n <= refRow ? expand({ top: newTop, left, bottom, right }) : null;
}
result = result ? organizeZone(result) : result;
result = result ? reorderZone(result) : result;
if (result && !isEqual(result, anchor.zone)) {
return this.processEvent({
options: { scrollIntoView: true },
Expand All @@ -272,7 +272,7 @@ export class SelectionStreamProcessorImpl implements SelectionStreamProcessor {
left: anchorCol,
right: anchorCol,
};
const zoneWithDelta = organizeZone({
const zoneWithDelta = reorderZone({
top: this.getNextAvailableRow(deltaRow, refCol!, top),
left: this.getNextAvailableCol(deltaCol, left, refRow!),
bottom: this.getNextAvailableRow(deltaRow, refCol!, bottom),
Expand Down

0 comments on commit 5ee2cad

Please sign in to comment.