Run RaycastCommand on multipleThreads?

I think it’s caused by the name string compare, when you nuke that and replace by a collidID int compare it goes away.
After a couple fixes I get a nice boost on an i7-1280p, thanks everyone for the legwork.
8500676--1131941--upload_2022-10-9_11-0-21.png

using System;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;

public class RaycastCommand_test : MonoBehaviour
{
    List<Vector3> dirs = new();
    List<Vector3> outPoints = new(40000);
    JobHandle _dependency;
    NativeArray<RaycastHit> _results;
    public bool completeOnLateUpdate;
    public bool vanillaRaycast;

    //Fill my directions
    void Awake()
    {
        for (var i = 0; i < 40000; i++) dirs.Add(new Vector3(Random.Range(-100f, 100f), Random.Range(-100f, 100f), Random.Range(-100f, 100f)));
    }

    public GameObject prefab;
    public int instanceCount = 1000;
    public float radius = 100;
    NativeArray<RaycastCommand> _commands;
    NativeArray<Vector3> _directions;

    void Start()
    {
        for (var i = 0; i < instanceCount; i++) Instantiate(prefab, Random.insideUnitSphere * radius, quaternion.identity);
    }

    //Call it everyframe
    public void Update()
    {
        if (vanillaRaycast) RaycastVanilla();
        else
            RaycastJobbified();
    }

    void RaycastVanilla()
    {
        outPoints.Clear();
        for (var i = 0; i < 40000; i++)
            if (Physics.Raycast(Random.insideUnitSphere * 100, dirs[i], out var hit))
                outPoints.Add(hit.point);
    }

    [BurstCompile]
    struct SetupCommandJob : IJobParallelFor
    {
        public NativeArray<RaycastCommand> commands;
        [ReadOnly] public NativeArray<Vector3> detectorPoints, origins;
        public void Execute(int index) { commands[index] = new RaycastCommand(origins[index], detectorPoints[index]); }
    }

    void RaycastJobbified()
    {
        _results = new NativeArray<RaycastHit>(40000, Allocator.TempJob);
        _commands = new NativeArray<RaycastCommand>(40000, Allocator.TempJob);
        _directions = new NativeArray<Vector3>(_commands.Length, Allocator.TempJob);
        for (var i = 0; i < 40000; i++) _directions[i] = dirs[i];
        //origins[i] = Vector3.zero; // Not Used?
        //set up a new job for the raycast commands to be filled
        var setupCommandsJob = new SetupCommandJob() {commands = _commands, origins = _directions, detectorPoints = _directions};
        _dependency = setupCommandsJob.Schedule(_commands.Length, 1, default);
        _dependency = RaycastCommand.ScheduleBatch(_commands, _results, 1, _dependency);
        if (!completeOnLateUpdate) CompleteThenDoTheRestOfTheWork();
    }

    void LateUpdate()
    {
        if (completeOnLateUpdate)
            CompleteThenDoTheRestOfTheWork();
    }

    void CompleteThenDoTheRestOfTheWork()
    {
        _dependency.Complete();
        outPoints.Clear();

        //evaluating, saving the results
        for (var i = 0; i < 40000; i++)
        {
            // collider ID = 0 means no hit
            if (_results[i].colliderInstanceID == 0)
                continue;
            outPoints.Add(_results[i].point);
        }
        _commands.Dispose();
        _results.Dispose();
        _directions.Dispose();
    }
}

8500676--1131938--upload_2022-10-9_10-59-54.png

1 Like