Search Unity

RaycastCommand slower than normal Raycast

Discussion in 'Physics' started by Elervax, Jul 1, 2020.

  1. Elervax

    Elervax

    Joined:
    Sep 4, 2017
    Posts:
    5
    Hello I have run some tests on the performance of Physics.Raycast and RaycastCommand.
    RaycastCommand: 10000 rays per frame ~ 7.3ms frame time
    RaycastCommand with Burst Compiler: 10000 rays per frame ~ 7.6ms frame time
    Raycast: 10000 rays per frame ~ 3.8ms frame time

    I expected raycast command to be much faster as i think it is part of the c# job system.
    Am I wrong about that or is my code wrong?

    Normal Raycast:

    using UnityEngine;

    public class RaycastN : MonoBehaviour
    {
    public Transform origin;
    public int num = 0;
    public LayerMask mask;

    void Update()
    {
    if(Input.GetKeyDown(KeyCode.Alpha1))
    {
    num += 5;
    Debug.Log(num);
    }
    if (Input.GetKeyDown(KeyCode.Alpha5))
    {
    num += 50;
    Debug.Log(num);
    }
    if (Input.GetKeyDown(KeyCode.Alpha9))
    {
    num += 500;
    Debug.Log(num);
    }

    for(int i = 0; i < num; i++)
    {
    Vector3 randDir = new Vector3(Random.Range(-Mathf.Infinity, +Mathf.Infinity), Random.Range(-Mathf.Infinity, +Mathf.Infinity), Random.Range(-Mathf.Infinity, +Mathf.Infinity));
    if(Physics.Raycast(origin.position, randDir, out RaycastHit hit, 200f, mask, QueryTriggerInteraction.Collide))
    {
    Debug.Log(hit.transform.name);
    }
    }
    }
    }

    RaycastCommand:

    using Unity.Collections;
    using Unity.Jobs;
    using UnityEngine;

    public class RaycastJ : MonoBehaviour
    {
    public Transform origin;
    public int num = 0;
    public LayerMask mask;

    void Update()
    {
    if(Input.GetKeyDown(KeyCode.Alpha1))
    {
    num += 5;
    Debug.Log(num);
    }
    if (Input.GetKeyDown(KeyCode.Alpha5))
    {
    num += 50;
    Debug.Log(num);
    }
    if (Input.GetKeyDown(KeyCode.Alpha9))
    {
    num += 500;
    Debug.Log(num);
    }

    var commands = new NativeArray<RaycastCommand>(num, Allocator.TempJob);
    int count = 0;

    for (int i = 0; i < num; i++)
    {
    Vector3 randDir = new Vector3(Random.Range(-Mathf.Infinity, +Mathf.Infinity), Random.Range(-Mathf.Infinity, +Mathf.Infinity), Random.Range(-Mathf.Infinity, +Mathf.Infinity));
    commands[count] = new RaycastCommand(origin.position, randDir, 300f, mask);
    count++;
    }
    var results = new NativeArray<RaycastHit>(num, Allocator.TempJob);
    JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 1, default);
    handle.Complete();
    for (int i = 0; i < num; i++)
    {
    RaycastHit hit = results;
    if (hit.collider != null)
    {
    Debug.Log(hit.transform.name);
    }
    }
    results.Dispose();
    commands.Dispose();
    }
    }
     
  2. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Why don't you increase the size of each batch sent to each processor in Schedule?


    RaycastCommand.ScheduleBatch(commands, results, 64, default);

    Otherwise, you send each to each thread. That's slow.
     
    laurentlavigne and Dr-Nick like this.
  3. ownself

    ownself

    Joined:
    Mar 18, 2013
    Posts:
    16
    I'm having the similar performance here where I have 2352 ray-casting. For looping Physics.Raycast() it spends 10ms, and for batching RaycastCommand, it spends 11ms. I have tried different "miniCommandsPerJob" value, from 1 to 2352. None of them makes much differences. :-(
     
  4. nainjoo2001

    nainjoo2001

    Joined:
    Jul 28, 2022
    Posts:
    5
    It may depends on how much core your cpu have. If you are using quad-core cpu, using 4 threads could give maximum performance. However, it doesn't mean 4x faster but 3.?x faster as spawning new thread also costs.
     
    laurentlavigne likes this.
  5. dev11n

    dev11n

    Joined:
    Aug 3, 2022
    Posts:
    2
    Yeah Raycast Commands are indeed slower right now, no matter what you set minCommandsPerJob to.
     
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    in addition to what others said
    1. your raycastcommand fires a ray that's 50% longer at 300 vs 200
    2. complete is called right after schedule when it is meant to be completed later in the frame, like a job... shouldn't affect what you see in profile timeline but maybe
    3. i think that nativearray is still slower than array and i wonder if accessing data from the hits also gets counted by unity profiler as part of the raycast querry like main thread raycast does

    also 10K rays at only 3ms? never seen such high performance
     
    Last edited: Oct 9, 2022