Skip to content

Improve clipping polygon performance #12650

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

javagl
Copy link
Contributor

@javagl javagl commented Jun 3, 2025

Description

Cache the "rectangle" that is computed in the ClippingPolygon class, so that it does not have to be re-computed each frame.

As mentioned in this issue, the clipping polygons had a distressingly low performance for a higher number of points. As analyzed in that issue, this was largely caused by the computeRectangle function in ClippingPolygon, which did re-compute the rectangle each frame.

This PR caches the rectangle, and only re-computes it when the _positions of the polygon change. For that, it was necessary to actually also cache the positions, and compare the cached positions with the actual positions (there is no other way to detect changes in the positions).

Issue number and link

Addresses #12258

Testing plan

  1. Run this sandcastle on the deployed sandcastle: https://sandcastle.cesium.com/index.html#c=hVZbUxs3FP4rGr9kXYxsEmgSY5hOSJOmkxQG0rxgHmTtsa1BK+1IWpNNhv/eI2mvZmk8Hls6OtdP5yKulXVkJ+ABDDkjCh7IBVhRZPRboCXLEQ/7C60cEwrMcjQhP5eKECcykEiZkzWTFiaexpTImBNadYmWg4IvOoUrwe/BdI9WzMJnVoJ5erSRetXR/Tg+Xaqlip7SoJKmsCo2N1v98MGwDOwVmBvgWqUYiDMFBAEeAnRCggWHB+yBCVfHyA0wBx+13ki42mqncS+FdYK/ev81iiTebs9qbkQmnNiBpSxNk0p1dG9dKO7DJ1HzhTAcNWuhnE1Q2IH5rNWEVEvmJsSwVBR2QlSRRb5xRDf6nQcSun17d+qpa21IIjEQgbTZKf4tWkncHhxU4rUChnyCTDtM3VOJnp6RxjFyQL4wt6VWqISR3+Lm6hOuXo7xJ7raV8BcqwDXlQKu7S8VxNBoXthtcis9KqjsbhxOH/2PAVcYVfEh+XEA4K9g3XvmWJI/xc5VZ+hgBYkrc8yo5egDihYGLrSUELRhTkcOhXnkObxsQ+TGzmsVrRLP2rBgOEbnY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g8/bSpiQQImzeU5eBAsvqmCG7Tr47rwBhNFb+4WBnqpefLip8cBlgxSuKwgj1v8B
  2. Increase the "Number of points" to 500, and observe how the FPS drop to ~10
  3. Run the same thing on this branch: https://ci-builds.cesium.com/cesium/improve-clipping-polygon-performance/Apps/Sandcastle/index.html#c=hVZbUxs3FP4rGr9kXYxsEmgSY5hOSJOmkxQG0rxgHmTtsa1BK+1IWpNNhv/eI2mvZmk8Hls6OtdP5yKulXVkJ+ABDDkjCh7IBVhRZPRboCXLEQ/7C60cEwrMcjQhP5eKECcykEiZkzWTFiaexpTImBNadYmWg4IvOoUrwe/BdI9WzMJnVoJ5erSRetXR/Tg+Xaqlip7SoJKmsCo2N1v98MGwDOwVmBvgWqUYiDMFBAEeAnRCggWHB+yBCVfHyA0wBx+13ki42mqncS+FdYK/ev81iiTebs9qbkQmnNiBpSxNk0p1dG9dKO7DJ1HzhTAcNWuhnE1Q2IH5rNWEVEvmJsSwVBR2QlSRRb5xRDf6nQcSun17d+qpa21IIjEQgbTZKf4tWkncHhxU4rUChnyCTDtM3VOJnp6RxjFyQL4wt6VWqISR3+Lm6hOuXo7xJ7raV8BcqwDXlQKu7S8VxNBoXthtcis9KqjsbhxOH/2PAVcYVfEh+XEA4K9g3XvmWJI/xc5VZ+hgBYkrc8yo5egDihYGLrSUELRhTkcOhXnkObxsQ+TGzmsVrRLP2rBgOEbnYJwAz9vowQDmesPnKaznXs3lx4v5EX01v7i+eXO8HJHHSr7+X0fPUMdtrbgx/MT/jnUsF9AZOFPOuwKtyJdCOnGlZbnpRBs/XGuTCoVoerO3Ecm7uw7PY7OuV/H08bRzTzXc9U35LOWFMZgbnnyjC8N9SQ7Sb7Ysj/XKbKk4aa65yFNfR1LkuVCbKgCbDFWLlkCl3mDH+jcIkQfhtm3inyHcB50yC9YIEWuSVN0ALwnbWZo88W7cFFXVCNLmyFIDmd7BgFBdKPsHmI+Fqmy12b5X7wP94/D1CT06eXk8ezMhr97St8e/v319MiEzejTphzVss9f4PoL+22rVMiByLE3qfB8qLX8WtQ+A4BvhIAKD3oTb9kHuH1DcCV9DdMdkgX8IUpokgVqOydl5dHDvvuIxzWNydBytMbXC5xLC+syNBH9qeboVYJjh25JuwH3zfiRj2ijpKOZ7Wdmfn3s52zab5GfdAKNYp9afF086Vd34Mm+XbYWO+yUakaiGFB1weZ9U12/Ml2fq73jcGa4cm51hl+t1nLCdIP4C7PgoJhzfXjO1gZBh1WEYDk5fIwtDlTM6i64PHx8en9QMJzP80NlSdUbzD62zrzoZvtpJz8Wu617av0xkPSSaQpqTYzrz747nu1IQ/GB05p9KSW/2NGpQ7z9FtsKHVGOK7hXr/wLd4w33UsFzrzS/14WjzjB+32r3fNX801qumH/VpZoXGcLiE/pPCX75rvyE5T6qeJYjL7avmeW5LN9hBaJDtrUwqTV7mfAgqWJGjIlQLaYVIr7DtsFvmb18UFdxWpaJF2rb654HdU57vy9XFsyOrSR0PQniDZstVpYbsYJk4IKalwV+R5PRwrpSQmgof4gs1wavwMiE0qmDLJd+Gk5XBT5KsWhsQH8xrUUWqdgRkZ4NvI2xnJj1s2ZdSHkjfuCQPl9Mkb8n5pstgnq5AyNZ6Vm2R+efI5FSupji9qlUc1nB64XzYFQdceFWOi3PayQWzpy3/WLh0vOYhESvqxGzmCKxz9J9EyyEygsXXg9o2PjSxdgyoXDn3y0Z+44rLERcWwc5bo5w6UfC4QozBvehg8/bSpiQQImzeU5eBAsvqmCG7Tr47rwBhNFb+4WBnqpefLip8cBlgxSuKwgj1v8B
  4. Increase the "Number of points" to 500, and observe no change in the FPS

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • (n.a.) I have updated CHANGES.md with a short summary of my change
  • (n.a.) I have added or updated unit tests to ensure consistent code coverage
  • (n.a.) I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

Copy link

github-actions bot commented Jun 3, 2025

Thank you for the pull request, @javagl!

✅ We can confirm we have a CLA on file for you.

@afzaale01
Copy link

afzaale01 commented Jun 11, 2025

@ggetz can this be reviewed to be merged ?

@theogerritsen
Copy link

@javagl Thanks a lot for this fix on the performance of the Clipping Polygon tool ! We tested it and it's way more efficient than the previous version !

Comment on lines +88 to +98
function copyArrayCartesian3(input) {
if (!defined(input)) {
return undefined;
}
const n = input.length;
const output = Array(n);
for (let i = 0; i < n; i++) {
output[i] = Cartesian3.clone(input[i]);
}
return output;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like something that might be useful in other contexts. Does it belong in the Cartesian3 class itself?

Copy link
Contributor

Choose a reason for hiding this comment

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

Same below with the equalsArrayCartesian3 function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure that similar things are done at many places, and this and similar trivial things are "inlined" everywhere (The size of the codebase could be reduced significantly by avoiding this sort of duplication). There's a tension between 1. trying to keep the API surface area small and 2. identifying the parts that should be accessible and actually use them (and that "using them" part is the difficulty when such fundamental building blocks are not identified and offered right from the beginning...)

I'll schedule another review over the code, and try to identify places where CartesianX arrays are copied or compared.

(BTW: If we offer this for Cartesian3, we also have to offer it for all other CartesianX classes, and maybe even for the MatrixX classes - otherwise, these classes diverge and become (too) inconsistent...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, there certainly are many places where such functions could have been useful, and could have been used, to avoid certain redundancies, but ... now the redundancies are there. And while I strongly agree that such "bulk" operations should at least be considered, I don't think that this PR is the right place and time to publicly introduce the two specific ones that happen to be required here.

I opened #12780 with further thoughts (we don't want that to stay "1.4k" for too long, eh...? 😁 )

@mzschwartz5
Copy link
Contributor

PR looks good to me- just have a comment about where to put a couple functions

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.

4 participants