[vector_math] Write out parameters once through setValues - #38
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates Vector2, Vector3, and Vector4 (both 32-bit and 64-bit implementations) so that normalizeInto, copyInto, min, max, and mix write to their output parameters exactly once using setValues. It also adds tests to verify this behavior for subclass observation. The review feedback highlights multiple instances where newly introduced lines exceed the 80-character limit specified in the style guide, suggesting wrapping arguments to improve readability.
normalizeInto wrote its target with setFrom followed by a normalize pass, min, max and mix assigned each component through a separate setter, and copyInto wrote the target's storage field by field. The Vector2, Vector3 and Vector4 members that take an out or result parameter now compute their result first and write it with a single setValues call, which measures 6 to 17 percent faster in AOT for normalizeInto, min, max and mix while producing identical results. Claude-Session: https://claude.ai/code/session_01PixXaRTPs8v1P2mQF2QHGG
69d5fdb to
f0f0e94
Compare
victorsanni
left a comment
There was a problem hiding this comment.
LGTM with a comment about missing tests.
| // found in the LICENSE file. | ||
|
|
||
| import 'package:test/test.dart'; | ||
| import 'package:vector_math/vector_math.dart'; |
There was a problem hiding this comment.
What about tests for vector_math_64?
There was a problem hiding this comment.
The convention has always been to test the Float32 library and trust the generator for the 64-bit variant?
But I can add it for this one if you want.
|
Hi @spydon, the PR description checklist row
is unchecked, even though the commit is from a Claude session. Have you read our AI contribution guidelines? |
|
@victorsanni ah yes, both that one and the CLA one is checked now. |
Makes the
Vector2,Vector3andVector4members that take anout/resultparameter compute their result first and write it with a singlesetValuescall:normalizeInto(out)didout..setFrom(this)..normalize(): one pass to copy the components and a second pass to read them back, compute the length and scale them. It now computes the normalized components fromthisand writes them once; the zero vector is still copied unchanged.min,maxandmixassigned each component through a separate setter call (result..x = …..y = …). They now make onesetValuescall.copyInto(arg)wrote the target's storage field by field; it is aligned with the same singlesetValuespattern.Numeric results are identical (verified by the benchmark checksums and the test suite).
vector_math_64was regenerated withtool/generate_vector_math_64.dart; only the vector files are included, because the generator also produces unrelated diffs inintersection_result.dartandquaternion.dart(those 64-bit files were hand-edited after generation), so they are left untouched here.A new test,
test/out_parameter_test.dart, covers the rewritten members for all three classes: results fornormalizeInto(including the zero-length vector andv.normalizeInto(v)aliasing),copyInto,min,maxandmix.Benchmark
Standalone benchmark (not committed) compiled with
dart compile exeagainstmainand against this branch, on macOS arm64; 4096 vectors per case, each binary run five times alternately, values are min/median/max of the runs in nanoseconds per call.mainVector2normalizeIntoVector2copyIntoVector2minVector2maxVector2mixVector3normalizeIntoVector3copyIntoVector3minVector3maxVector3mixVector4normalizeIntoVector4copyIntoVector4minVector4maxVector4mixThe gains are AOT-specific. The same benchmark compiled with
dart compile js -O4and run in node measures neutral: every case is within about 2% with overlapping ranges. Both builds produce identical checksums on both platforms.Fixes flutter/flutter#192041
Pre-Review Checklist
[vector_math]///).https://claude.ai/code/session_01PixXaRTPs8v1P2mQF2QHGG
Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2