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

Throttle events in polygon #1444

Merged
merged 6 commits into from
Jul 25, 2024
Merged

Throttle events in polygon #1444

merged 6 commits into from
Jul 25, 2024

Conversation

nicolecomputer
Copy link
Contributor

Summary:

Throttles the events in polygon to increase performance and decrease CPU load. This is especially important for Safari. This change lowers CPU usage from 87% to 30% on a M3 macbook. The increase in performance will be more on phones and iPads where this will also decrease energy usage.

40 FPS was chosen as a mark that feels response and good to use but doesn't put too much stress on the machine.

Issue: https://khanacademy.atlassian.net/browse/LEMS-2185

Test plan:

To test before:

  • Load the dev environment on Safari
  • Open the Timelines pane and start recording
  • The CPU usage should be high (it'll vary based on your machine)

To test after:

  • Repeat test above with this change
  • The CPU usage should be lower

@nicolecomputer nicolecomputer self-assigned this Jul 24, 2024
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Jul 24, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/pretty-planets-sleep.md, packages/perseus/src/widgets/interactive-graphs/graphs/polygon.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@khan-actions-bot khan-actions-bot requested a review from a team July 24, 2024 21:48
Copy link
Contributor

github-actions bot commented Jul 24, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (e41b7e0) and published it to npm. You
can install it using the tag PR1444.

Example:

yarn add @khanacademy/perseus@PR1444

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1444

Copy link
Contributor

github-actions bot commented Jul 24, 2024

Size Change: +860 B (+0.1%)

Total Size: 855 kB

Filename Size Change
packages/perseus-editor/dist/es/index.js 273 kB +569 B (+0.21%)
packages/perseus/dist/es/index.js 415 kB +291 B (+0.07%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.2 kB
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 80.6 kB
packages/math-input/dist/es/strings.js 1.73 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-linter/dist/es/index.js 21.6 kB
packages/perseus/dist/es/strings.js 3.21 kB
packages/pure-markdown/dist/es/index.js 3.67 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

Copy link

codecov bot commented Jul 24, 2024

Codecov Report

Attention: Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.

Project coverage is 70.26%. Comparing base (c6a5cbe) to head (e41b7e0).
Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1444      +/-   ##
==========================================
+ Coverage   69.86%   70.26%   +0.40%     
==========================================
  Files         505      508       +3     
  Lines      105665   105672       +7     
  Branches     7670    10801    +3131     
==========================================
+ Hits        73826    74254     +428     
+ Misses      31659    31418     -241     
+ Partials      180        0     -180     

Impacted file tree graph

Files Coverage Δ
.../src/widgets/interactive-graphs/graphs/polygon.tsx 89.65% <54.54%> (-8.09%) ⬇️

... and 166 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7bf32b7...e41b7e0. Read the comment docs.

@@ -42,6 +42,7 @@ export const PolygonGraph = (props: Props) => {
});

const active = hovered || focused || dragging;
const [lastMoveTime, setLastMoveTime] = React.useState<number>(0);
Copy link
Member

@benchristel benchristel Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for doing this optimization work!

Could we use a ref instead of state here? That would prevent unnecessary re-renders. I believe a ref is appropriate because we will never need to update DOM elements when lastMoveTime changes.

Copy link
Contributor Author

@nicolecomputer nicolecomputer Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yepppppp. You're absolutely right. Fixed up

}
onMove={(destination: vec.Vector2) => {
const now = Date.now();
const targetFPS = 40;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for capping FPS at 40? For displays that refresh at 60Hz (or browsers that repaint at 60Hz?), this will result in every third frame getting dropped. I guess my question is, why not 30 fps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loosely held opinion: 40 fps feels about right. I've noticed in games it's where things start to feel smooth (which is totally subjective). Some data, though: If I cap at 40fps I get around 26% CPU usage. If I cap at 60 I get around 40%.

@khan-actions-bot khan-actions-bot requested a review from a team July 25, 2024 16:51
Copy link
Member

@benchristel benchristel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 LGTM!

@nicolecomputer nicolecomputer merged commit 130ab94 into main Jul 25, 2024
10 of 11 checks passed
@nicolecomputer nicolecomputer deleted the new/throttle branch July 25, 2024 17:41
benchristel pushed a commit that referenced this pull request Jul 29, 2024
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.


# Releases
## @khanacademy/[email protected]

### Major Changes

-   [#1456](#1456) [`b868801fa`](b868801) Thanks [@benchristel](https://github.com/benchristel)! - Remove unused "Show ruler" option from the interactive graph editor. The
    new Mafs version of the interactive graph does not implement the ruler,
    and we have no plans to implement it, since it can't be made accessible
    and isn't used in Khan Academy's existing content.


-   [#1450](#1450) [`2216ad012`](2216ad0) Thanks [@handeyeco](https://github.com/handeyeco)! - Remove Unit aka UnitInput widget

### Minor Changes

-   [#1451](#1451) [`9bc4812fc`](9bc4812) Thanks [@mark-fitzgerald](https://github.com/mark-fitzgerald)! - Upgrade Mafs Dependency to v0.19.0 for Domain Restricted Functions


-   [#1441](#1441) [`9bc264ce1`](9bc264c) Thanks [@Myranae](https://github.com/Myranae)! - Turn off interactivity when Interactive Graph in hint mode


-   [#1437](#1437) [`7a448e77c`](7a448e7) Thanks [@Myranae](https://github.com/Myranae)! - Update Polygon to be filled on hover


-   [#1422](#1422) [`c386515ad`](c386515) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Segment and Linear System graph start coords UI

### Patch Changes

-   [#1448](#1448) [`84675574c`](8467557) Thanks [@nishasy](https://github.com/nishasy)! - Cleaned up the `startCoords` code in the stateful mafs graph useEffect


-   [#1444](#1444) [`130ab9446`](130ab94) Thanks [@nicolecomputer](https://github.com/nicolecomputer)! - Throttles point-moving performance in polygon


-   [#1445](#1445) [`bb1ac584b`](bb1ac58) Thanks [@jeremywiebe](https://github.com/jeremywiebe)! - useDraggable - fix to ignore keyup events (we don't want to treat keyup events as an intent to move - we have keydown for that)

-   Updated dependencies \[[`7e71f8e8a`](7e71f8e)]:
    -   @khanacademy/[email protected]

## @khanacademy/[email protected]

### Major Changes

-   [#1456](#1456) [`b868801fa`](b868801) Thanks [@benchristel](https://github.com/benchristel)! - Remove unused "Show ruler" option from the interactive graph editor. The
    new Mafs version of the interactive graph does not implement the ruler,
    and we have no plans to implement it, since it can't be made accessible
    and isn't used in Khan Academy's existing content.


-   [#1450](#1450) [`2216ad012`](2216ad0) Thanks [@handeyeco](https://github.com/handeyeco)! - Remove Unit aka UnitInput widget

### Minor Changes

-   [#1422](#1422) [`c386515ad`](c386515) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Segment and Linear System graph start coords UI

### Patch Changes

-   [#1446](#1446) [`4985d2d4c`](4985d2d) Thanks [@nishasy](https://github.com/nishasy)! - Rename StartCoordSettings to StartCoordsSettings


-   [#1448](#1448) [`84675574c`](8467557) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Refactor and clean up start coords UI implementation

-   Updated dependencies \[[`b868801fa`](b868801), [`84675574c`](8467557), [`7e71f8e8a`](7e71f8e), [`9bc4812fc`](9bc4812), [`130ab9446`](130ab94), [`9bc264ce1`](9bc264c), [`bb1ac584b`](bb1ac58), [`2216ad012`](2216ad0), [`7a448e77c`](7a448e7), [`c386515ad`](c386515)]:
    -   @khanacademy/[email protected]
    -   @khanacademy/[email protected]

## @khanacademy/[email protected]

### Patch Changes

-   [#1454](#1454) [`7e71f8e8a`](7e71f8e) Thanks [@Myranae](https://github.com/Myranae)! - Update mathjax-renderer version

## @khanacademy/[email protected]

### Patch Changes

-   Updated dependencies \[[`7e71f8e8a`](7e71f8e)]:
    -   @khanacademy/[email protected]

Author: khan-actions-bot

Reviewers: benchristel

Required Reviewers:

Approved By: benchristel

Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ⏭️  Publish npm snapshot, ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants