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

textToPoints perf improvement #7428

Merged
merged 2 commits into from
Dec 15, 2024
Merged

textToPoints perf improvement #7428

merged 2 commits into from
Dec 15, 2024

Conversation

davepagurek
Copy link
Contributor

textToPoints is known to be sort of slow because getting relatively even sampling in Cartesian space along a Bezier curve isn't computationally easy to do. The main change here is that I've replaced the implementation we currently have with one I've been working on on the side for a while.

I was using this test case to measure performance:

const points = font.textToPoints('p5*js', 0, 0, { sampleFactor: 2 });
p.beginShape(p.POINTS);
for (const { x, y } of points) p.vertex(x, y);
p.endShape();
Sampling densityCurrent implementationUpdated implementation
215 FPS 60+ FPS
700.4 FPS 15 FPS

On p5 1.x, this would give me 15fps on my 2021 M1 Macbook Pro. After these changes, this test case runs at 60fps.

To test the speedup, I increased the sampling density until it went back down to 15fps. I found I had to crank the density up to 70 to hit the same speed as before, so this seems to be a 350% speedup on this test case.

Under the hood, the main difference is that rather than using iteration to narrow down on the point at a given length for every sample, we can, upfront, calculate a much smaller number of coarse samples at evenly spaced intervals, and then essentially use these as a skip list to quickly narrow down on the target point.

The other update here is that I've reverted the default sampleFactor value to what it is in 1.x (I didn't realize before that this was an existing API -- the small adjustment I made to the defaults earlier isn't important enough to warrant a breaking change.)

@davepagurek davepagurek merged commit 685ca3c into dev-2.0 Dec 15, 2024
2 checks passed
@davepagurek davepagurek deleted the feat/texttopoints-perf branch December 15, 2024 19:25
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.

1 participant