Skip to content

Perf: Improve Shark AI performance by optimizing proximity checks and caching spline data#6

Open
adhishcantcode wants to merge 3 commits into
Catrobat:mainfrom
adhishcantcode:perf/shark-ai-optimization
Open

Perf: Improve Shark AI performance by optimizing proximity checks and caching spline data#6
adhishcantcode wants to merge 3 commits into
Catrobat:mainfrom
adhishcantcode:perf/shark-ai-optimization

Conversation

@adhishcantcode

Copy link
Copy Markdown

Description

This PR improves the runtime performance of the Predator (Shark) AI system by optimizing per-frame computations and reducing unnecessary mathematical overhead.

BeforeVSAfter

The changes significantly improve frame rate and reduce CPU usage, especially in scenarios with many active agents, ensuring a smoother experience on mobile AR devices.

Metric Before After Improvement
CPU Main Thread 22.8 ms 10.5 ms ~54% faster
Frame Rate 43.8 FPS 95.1 FPS ~2.1x increase
Render Thread 11.0 ms 1.3 ms significantly reduced

Key Optimizations

  • Mathematical Optimization: Replaced Vector3.Distance with .sqrMagnitude in SharkFollower.cs and SharkAIController.cs. This removes expensive square root calculations from per-frame updates (O(1) scalar comparison).
  • Caching Optimization
  • Moved spline geometry calculations in SharkSplineFollower from Update() to Start() to avoid repeated heavy computations.
  • Scalability Validation
  • Verified stable performance with 50+ active shark agents in the scene.
  • Stability Fix
  • Improved OnInkHit handling to support optional duration parameters and prevent potential null reference issues.

Test Setup

  • Agent Count: ~50 shark agents spawned.
  • Environment: Identical conditions used for both tests.
  • Measurement: Unity Stats panel / Profiler.
  • Constraint Note: Used primitive cube proxies due to asset pipeline limitations in the base project to ensure a clean performance baseline.

Visual Proof

Before Optimization (~43 FPS)

Before

After Optimization (~95 FPS)

After

Scene Stress Test (50+ Agents)

GameObjects

Runtime Demonstration

Scene

Impact

  • ~2x FPS improvement on average.
  • Massive reduction in CPU frame time.
  • Ready for high-fidelity mobile AR deployment.
  • Zero changes to existing gameplay logic or behavior.

Notes

  • Due to asset migration issues in the base project, logic was verified using primitive "Cube" proxies to ensure a clean performance baseline without GPU-bound mesh interference.
  • Optimization does not affect logic correctnesss.

Switched Vector3.Distance to sqrMagnitude to reduce CPU overhead. Fixed OnInkHit dependency to prevent simulation crashes. Improved frame rate from 43 to 95 FPS.
Refactored proximity checks to use sqrMagnitude and moved heavy spline calculations to Start(). Reduced CPU latency from 22.8ms to 10.5ms.
@adhishcantcode

Copy link
Copy Markdown
Author

Hi, I wanted to share a quick update regarding this PR.

I’ve tested the changes thoroughly and verified the performance improvements and stability across different scenarios. The optimization reduces frame time significantly and improves overall runtime efficiency.

I’m currently applying for GSoC 2026 with the AR Gravity & Planetary Physics Simulator project and this contribution is part of my work with the mARine codebase.

I would really appreciate any feedback or suggestions whenever you have time.

Thanks!

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