Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

space-time, manchette performance #897

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rollup-base.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'process';
import path from 'path';
import process from 'process';

import eslint from '@rollup/plugin-eslint';
import nodeResolve from '@rollup/plugin-node-resolve';
Expand Down Expand Up @@ -30,7 +30,7 @@ const generateRollupBaseConfig = () => ({
sourceMap: true,
plugins: [],
}),
terser(),
// terser(),
process.env.ROLLUP_WATCH &&
livereload({
watch: 'dist',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState, useTransition } from 'react';

import type { ProjectPathTrainResult, Waypoint } from '@osrd-project/ui-manchette/dist/types';
import type {
SpaceScale,
SpaceTimeChartProps,
} from '@osrd-project/ui-spacetimechart/dist/lib/types';

import usePaths from './usePaths';
import computePaths from './usePaths';
import { MAX_ZOOM_Y, MIN_ZOOM_Y, ZOOM_Y_DELTA, DEFAULT_ZOOM_MS_PER_PX } from '../consts';
import {
computeWaypointsToDisplay,
Expand Down Expand Up @@ -39,6 +39,7 @@ const useManchettesWithSpaceTimeChart = (
height = 561,
spaceTimeChartRef?: React.RefObject<HTMLDivElement>
) => {
const [, startTransition] = useTransition();
const [isShiftPressed, setIsShiftPressed] = useState(false);
const [state, setState] = useState<State>({
xZoom: timeScaleToZoomValue(DEFAULT_ZOOM_MS_PER_PX),
Expand All @@ -54,7 +55,13 @@ const useManchettesWithSpaceTimeChart = (

const { xZoom, yZoom, xOffset, yOffset, scrollTo, panning, isProportional } = state;

const paths = usePaths(projectPathTrainResult, selectedTrain);
const [paths, setPaths] = useState(() => computePaths(projectPathTrainResult, selectedTrain));
useEffect(() => {
// console.log('transition');
startTransition(() => {
setPaths(computePaths(projectPathTrainResult, selectedTrain));
});
}, [projectPathTrainResult, selectedTrain]);

const waypointsToDisplay = useMemo(
() => computeWaypointsToDisplay(waypoints, { height, isProportional, yZoom }),
Expand Down Expand Up @@ -176,6 +183,47 @@ const useManchettesWithSpaceTimeChart = (
[spaceTimeChartRef]
);

const handleZoom = useCallback(
({ delta, position }: Parameters<NonNullable<SpaceTimeChartProps['onZoom']>>[0]) => {
if (isShiftPressed) {
handleXZoom(xZoom + delta, position.x);
}
},
[handleXZoom, isShiftPressed, xZoom]
);
const handlePan = useCallback(
(payload: {
initialPosition: { x: number; y: number };
position: { x: number; y: number };
isPanning: boolean;
}) => {
const diff = getDiff(payload.initialPosition, payload.position);
const newState = { ...state };

if (!payload.isPanning) {
newState.panning = null;
} else if (!panning) {
newState.panning = { initialOffset: { x: xOffset, y: yOffset } };
} else {
const { initialOffset } = panning;
newState.xOffset = initialOffset.x + diff.x;

const newYPos = initialOffset.y - diff.y;
if (
manchetteWithSpaceTimeChartContainer.current &&
newYPos >= 0 &&
newYPos + manchetteWithSpaceTimeChartContainer.current.offsetHeight <
manchetteWithSpaceTimeChartContainer.current.scrollHeight
) {
newState.yOffset = newYPos;
manchetteWithSpaceTimeChartContainer.current.scrollTop = newYPos;
}
}
setState(newState);
},
[manchetteWithSpaceTimeChartContainer, panning, state, xOffset, yOffset]
);

const spaceTimeChartProps = useMemo(
() => ({
operationalPoints: simplifiedWaypoints,
Expand All @@ -184,54 +232,10 @@ const useManchettesWithSpaceTimeChart = (
paths,
xOffset,
yOffset: -yOffset + 14,
onZoom: ({ delta, position }: Parameters<NonNullable<SpaceTimeChartProps['onZoom']>>[0]) => {
if (isShiftPressed) {
handleXZoom(xZoom + delta, position.x);
}
},
onPan: (payload: {
initialPosition: { x: number; y: number };
position: { x: number; y: number };
isPanning: boolean;
}) => {
const diff = getDiff(payload.initialPosition, payload.position);
const newState = { ...state };

if (!payload.isPanning) {
newState.panning = null;
} else if (!panning) {
newState.panning = { initialOffset: { x: xOffset, y: yOffset } };
} else {
const { initialOffset } = panning;
newState.xOffset = initialOffset.x + diff.x;

const newYPos = initialOffset.y - diff.y;
if (
manchetteWithSpaceTimeChartContainer.current &&
newYPos >= 0 &&
newYPos + manchetteWithSpaceTimeChartContainer.current.offsetHeight <
manchetteWithSpaceTimeChartContainer.current.scrollHeight
) {
newState.yOffset = newYPos;
manchetteWithSpaceTimeChartContainer.current.scrollTop = newYPos;
}
}
setState(newState);
},
onZoom: handleZoom,
onPan: handlePan,
}),
[
simplifiedWaypoints,
computedScales,
xZoom,
paths,
xOffset,
isShiftPressed,
state,
panning,
yOffset,
manchetteWithSpaceTimeChartContainer,
handleXZoom,
]
[simplifiedWaypoints, computedScales, xZoom, paths, xOffset, yOffset, handleZoom, handlePan]
);

return useMemo(
Expand Down
37 changes: 16 additions & 21 deletions ui-manchette-with-spacetimechart/src/hooks/usePaths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable import/no-unresolved */
import { useMemo } from 'react';

import { type ProjectPathTrainResult } from '@osrd-project/ui-manchette/dist/types';
import { type PathLevel } from '@osrd-project/ui-spacetimechart';
Expand All @@ -12,25 +11,21 @@ const transformCurve = (curve: ProjectPathTrainResult['spaceTimeCurves'][0], dep
position,
}));

const usePaths = (projectPathTrainResult: ProjectPathTrainResult[], selectedTrain?: number) =>
useMemo(
() =>
projectPathTrainResult.flatMap((path) =>
path.spaceTimeCurves.map<{
id: string;
label: string;
color: string;
level: PathLevel;
points: { time: number; position: number }[];
}>((spaceTimeCurve, ind) => ({
id: `${path.id}-${ind}`,
label: path.name,
color: PATH_COLOR_DEFAULT,
level: selectedTrain && selectedTrain === path.id ? 1 : 2,
points: transformCurve(spaceTimeCurve, path.departureTime),
}))
),
[projectPathTrainResult, selectedTrain]
const computePaths = (projectPathTrainResult: ProjectPathTrainResult[], selectedTrain?: number) =>
projectPathTrainResult.flatMap((path) =>
path.spaceTimeCurves.map<{
id: string;
label: string;
color: string;
level: PathLevel;
points: { time: number; position: number }[];
}>((spaceTimeCurve, ind) => ({
id: `${path.id}-${ind}`,
label: path.name,
color: PATH_COLOR_DEFAULT,
level: selectedTrain && selectedTrain === path.id ? 1 : 2,
points: transformCurve(spaceTimeCurve, path.departureTime),
}))
);

export default usePaths;
export default computePaths;
3 changes: 2 additions & 1 deletion ui-manchette/src/components/Manchette.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useLayoutEffect, useRef, useState } from 'react';
import React, { memo, useLayoutEffect, useRef, useState } from 'react';

Check warning on line 1 in ui-manchette/src/components/Manchette.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

Check warning on line 1 in ui-manchette/src/components/Manchette.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

import { ZoomIn, ZoomOut } from '@osrd-project/ui-icons';
import cx from 'classnames';
Expand Down Expand Up @@ -50,6 +50,7 @@

return (
<div className="manchette-container">
MANCHETTE NO MEMO
{waypointMenuData?.menu && waypointMenuData.activeWaypointId && (
<div className="menu-wrapper" style={{ top: menuPosition }}>
{waypointMenuData.menu}
Expand Down
2 changes: 1 addition & 1 deletion ui-spacetimechart/src/components/SpaceGraduations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, memo } from 'react';

Check warning on line 1 in ui-spacetimechart/src/components/SpaceGraduations.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

Check warning on line 1 in ui-spacetimechart/src/components/SpaceGraduations.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

import { useDraw } from '../hooks/useCanvas';
import { type DrawingFunction } from '../lib/types';
Expand Down
3 changes: 2 additions & 1 deletion ui-spacetimechart/src/components/SpaceTimeChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from 'react';
import React, { useEffect, useMemo, useState, memo } from 'react';

Check warning on line 1 in ui-spacetimechart/src/components/SpaceTimeChart.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

Check warning on line 1 in ui-spacetimechart/src/components/SpaceTimeChart.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

import cx from 'classnames';

Expand Down Expand Up @@ -198,6 +198,7 @@
className={cx('relative space-time-chart', attr.className)}
style={{ background: fullTheme.background }}
>
SPACE_TIME
<div ref={setCanvasesRoot} className="absolute inset-0" />
<SpaceTimeChartContext.Provider value={contextState}>
<CanvasContext.Provider value={canvasContext}>
Expand Down
2 changes: 1 addition & 1 deletion ui-spacetimechart/src/components/TimeGraduations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, memo } from 'react';

Check warning on line 1 in ui-spacetimechart/src/components/TimeGraduations.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

Check warning on line 1 in ui-spacetimechart/src/components/TimeGraduations.tsx

View workflow job for this annotation

GitHub Actions / build

'memo' is defined but never used

import { useDraw } from '../hooks/useCanvas';
import { MINUTE } from '../lib/consts';
Expand Down
5 changes: 4 additions & 1 deletion ui-spacetimechart/src/hooks/useCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/**
* This function renders all picking layers:
*/
const drawPicking = useCallback(

Check warning on line 54 in ui-spacetimechart/src/hooks/useCanvas.ts

View workflow job for this annotation

GitHub Actions / build

'drawPicking' is assigned a value but never used

Check warning on line 54 in ui-spacetimechart/src/hooks/useCanvas.ts

View workflow job for this annotation

GitHub Actions / build

'drawPicking' is assigned a value but never used
(stcContext: SpaceTimeChartContextType, layers?: Set<LayerType>) => {
stcContext.resetPickingElements();
PICKING_LAYERS.forEach((layer) => {
Expand Down Expand Up @@ -96,7 +96,10 @@
*/
const draw = useCallback(() => {
drawRendering(stcContextRef.current);
drawPicking(stcContextRef.current);
// TODO: expensive, 300ms for each frame
// can’t we detect the mouse is close to an element with just its euclidian distance from the point?
// do we really need super fine interaction (to know that the mouse is over an element with precision down to the pixel?
// drawPicking(stcContextRef.current);

if (scheduledRef.current) {
window.cancelAnimationFrame(scheduledRef.current.frameId);
Expand Down
1 change: 1 addition & 0 deletions ui-speedspacechart/src/components/SpeedSpaceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const SpeedSpaceChart = ({
}}
tabIndex={0}
>
SPEED_SPACE
<div
className="flex justify-end absolute base-margin-top"
style={{ width: adjustedWidthRightAxis }}
Expand Down
Loading