Reduce filesize of spring and inertia generators - #3799
Open
mattgperry wants to merge 1 commit into
Open
Conversation
- Dedupe the underdamped spring position/velocity math into a single memoized sampler shared by resolveSpring, resolveVelocity and the next() hot path - Hoist repeated dampingRatio * undampedAngularFreq product and replace Math.pow(x, 2) with multiplication in findSpring - Use the analytical derivative of the inertia friction curve for the boundary spring velocity instead of finite-difference sampling - Calculate delta once per frame in applyFriction - Unify calcGeneratorDuration and pregenerateKeyframes into one parametrized sampling loop Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Filesize pass over the spring and inertia generators: -374 bytes minified / -107 bytes gzipped (4,773 → 4,399 min, 2,185 → 2,078 gzip, measured by bundling
spring+inertia+pregenerateKeyframeswith esbuild).resolveSpring,resolveVelocity, and an inlined copy in thenext()hot path). It now exists once, in asample(t)closure memoized bytthat computes both values while sharing theexp/sin/costerms — same one-trig-evaluation-per-frame behavior, single-pathnext(). Also hoists the repeateddampingRatio * undampedAngularFreqproduct and replacesMath.pow(x, 2)with multiplication infindSpring. Output is bit-identical (spring tests pass unchanged).applyFrictioncalculatescalcDeltaonce per frame instead of twice. The boundary spring now receives the exact analytical derivative of the friction curve (-calcDelta(t) / timeConstantper ms) instead of a 5ms finite-difference sample, removingcalcLatestand thegetGeneratorVelocityimport and resolving the stale units TODO on that line.isOutOfBoundsrelies on comparisons againstundefinedalways being false.calcGeneratorDurationnow accepts optionaltimeStep,maxDurationandkeyframesparameters sopregenerateKeyframesreuses the same sampling loop instead of duplicating it.Behaviour notes
inertia.test.tswere regenerated and verified programmatically (values before the boundary are bit-identical, min/max cases deviate symmetrically).Test plan
Made with Cursor