Skip to content

Adds Math::sin_cos() - This computes sine and cosine of the same angle together, which reduces cycles and should improve performance slightly.#1298

Merged
Arctis-Fireblight merged 2 commits into
Redot-Engine:masterfrom
mcdubhghlas:rex-boosts
Jul 11, 2026
Merged

Conversation

@mcdubhghlas

@mcdubhghlas mcdubhghlas commented Jul 9, 2026

Copy link
Copy Markdown
Member

I also replaced certain individual calls of sin and cos with sin_cos. I did not do every single instance of sine and cosine because it is mostly not necessary in every case and I did not want to add more code to review. Plus I wanted to not touch tons of files. We can, if someone wanted to, but the pay off is a one-off and I don't think it will be impactful outside of hot paths.

Summary by CodeRabbit

  • Performance Improvements
    • Optimized trigonometric usage by introducing Math::sin_cos (leveraging compiler builtins when available).
    • Updated rotation, Euler/axis-angle conversions, circle sampling, particle initialization/orientation, and various rendering/audio/physics computations to use paired sine/cosine calculations.
    • Improves efficiency in drawing arcs/circles, generating particle motion, computing collision contact points, and running audio effects, with no expected visible behavior changes.

…e together, which reduces cycles and should improve performance slightly.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0d0396fa-c250-4027-bfe8-ab30d3694ad8

📥 Commits

Reviewing files that changed from the base of the PR and between 68a0610 and 4f5a0f3.

📒 Files selected for processing (3)
  • scene/2d/cpu_particles_2d.cpp
  • servers/audio/effects/audio_effect_pitch_shift.cpp
  • servers/audio/effects/audio_effect_spectrum_analyzer.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • servers/audio/effects/audio_effect_spectrum_analyzer.cpp
  • servers/audio/effects/audio_effect_pitch_shift.cpp
  • scene/2d/cpu_particles_2d.cpp

Walkthrough

This PR adds Math::sin_cos and switches multiple math, physics, rendering, particle, and audio code paths to compute sine and cosine together instead of separately.

Changes

Math::sin_cos rollout

Layer / File(s) Summary
sin_cos helper definition
core/math/math_funcs.h
Adds Math::sin_cos template overloads for float and double, with builtin support when available.
Core math types
core/math/basis.cpp, core/math/quaternion.cpp, core/math/vector2.cpp, core/math/transform_2d.cpp, core/math/transform_2d.h
Basis, Quaternion, Vector2, and Transform2D rotation and skew code now uses paired sine/cosine values.
Physics collision solvers
modules/godot_physics_3d/godot_collision_solver_3d.cpp, modules/godot_physics_3d/godot_collision_solver_3d_sat.cpp
Circle support and contact point generation now uses sin_cos.
CPU particle math
scene/2d/cpu_particles_2d.cpp, scene/3d/cpu_particles_3d.cpp
Particle rotation, emission placement, hue rotation, and basis construction now use shared sine/cosine results.
Canvas and rendering paths
scene/main/canvas_item.cpp, scene/resources/style_box_flat.cpp, servers/rendering/renderer_canvas_cull.cpp, servers/rendering/renderer_rd/renderer_compositor_rd.cpp, servers/rendering/renderer_scene_cull.cpp
Arc/circle drawing, rounded rectangles, screen rotation constants, circle polygons, and spotlight geometry now use sin_cos.
Audio FFT computations
servers/audio/effects/audio_effect_pitch_shift.cpp, servers/audio/effects/audio_effect_spectrum_analyzer.cpp
Pitch-shift synthesis and FFT twiddle factors now use Math::sin_cos.

Estimated code review effort: 2 (Simple) | ~15 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding Math::sin_cos() and using it for performance-sensitive trig computations.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
servers/audio/effects/audio_effect_spectrum_analyzer.cpp (1)

84-87: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider using float instead of real_t for sc_sin/sc_cos.

Same observation as the smbFft in audio_effect_pitch_shift.cpp: all surrounding variables are float, so declaring sc_sin/sc_cos as real_t is inconsistent and may cause an unnecessary promote-narrow if real_t is double.

♻️ Proposed refactor
-	real_t sc_sin, sc_cos;
+	float sc_sin, sc_cos;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@servers/audio/effects/audio_effect_spectrum_analyzer.cpp` around lines 84 -
87, The `AudioEffectSpectrumAnalyzer` FFT twiddle setup uses `real_t` for
`sc_sin` and `sc_cos`, which is inconsistent with the surrounding `float`
variables and may introduce unnecessary conversions. Update the `Math::sin_cos`
locals in the spectrum analyzer code path to use `float`, matching the `smbFft`
style in `AudioEffectPitchShift`, and keep the subsequent assignments to `wr`
and `wi` in the same `float` type flow.
servers/audio/effects/audio_effect_pitch_shift.cpp (1)

262-265: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider using float instead of real_t for sc_sin/sc_cos.

All surrounding variables in smbFft (wr, wi, arg, ur, ui) are float, but sc_sin/sc_cos are declared as real_t. If real_t is configured as double, this triggers the sin_cos(float, double&, double&) overload which computes in float via sincosf then promotes to double, only to narrow back to float on assignment to wr/wi. Using float directly is consistent with the rest of the function and avoids the unnecessary promotion.

♻️ Proposed refactor
-		real_t sc_sin, sc_cos;
+		float sc_sin, sc_cos;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@servers/audio/effects/audio_effect_pitch_shift.cpp` around lines 262 - 265,
The smbFft implementation in audio_effect_pitch_shift.cpp uses real_t for
sc_sin/sc_cos even though the surrounding values are float. Update the sc_sin
and sc_cos declarations in smbFft to float so Math::sin_cos uses the float
overload consistently, matching wr, wi, arg, ur, and ui and avoiding the extra
promote/narrow path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@servers/audio/effects/audio_effect_pitch_shift.cpp`:
- Around line 262-265: The smbFft implementation in audio_effect_pitch_shift.cpp
uses real_t for sc_sin/sc_cos even though the surrounding values are float.
Update the sc_sin and sc_cos declarations in smbFft to float so Math::sin_cos
uses the float overload consistently, matching wr, wi, arg, ur, and ui and
avoiding the extra promote/narrow path.

In `@servers/audio/effects/audio_effect_spectrum_analyzer.cpp`:
- Around line 84-87: The `AudioEffectSpectrumAnalyzer` FFT twiddle setup uses
`real_t` for `sc_sin` and `sc_cos`, which is inconsistent with the surrounding
`float` variables and may introduce unnecessary conversions. Update the
`Math::sin_cos` locals in the spectrum analyzer code path to use `float`,
matching the `smbFft` style in `AudioEffectPitchShift`, and keep the subsequent
assignments to `wr` and `wi` in the same `float` type flow.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1553b08b-27c8-4218-ad72-fa90c4572700

📥 Commits

Reviewing files that changed from the base of the PR and between d93bf62 and 68a0610.

📒 Files selected for processing (17)
  • core/math/basis.cpp
  • core/math/math_funcs.h
  • core/math/quaternion.cpp
  • core/math/transform_2d.cpp
  • core/math/transform_2d.h
  • core/math/vector2.cpp
  • modules/godot_physics_3d/godot_collision_solver_3d.cpp
  • modules/godot_physics_3d/godot_collision_solver_3d_sat.cpp
  • scene/2d/cpu_particles_2d.cpp
  • scene/3d/cpu_particles_3d.cpp
  • scene/main/canvas_item.cpp
  • scene/resources/style_box_flat.cpp
  • servers/audio/effects/audio_effect_pitch_shift.cpp
  • servers/audio/effects/audio_effect_spectrum_analyzer.cpp
  • servers/rendering/renderer_canvas_cull.cpp
  • servers/rendering/renderer_rd/renderer_compositor_rd.cpp
  • servers/rendering/renderer_scene_cull.cpp

@Arctis-Fireblight

Arctis-Fireblight commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Fantastic work McDubh!

I am seeing a pretty good speed up here compared to #1273 .
We were sitting at 253FPS on the last benchmark, and this PR raises it to 264!
image

@Arctis-Fireblight Arctis-Fireblight merged commit 0d066cb into Redot-Engine:master Jul 11, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants