I am trying to make a simple time rewind mechanic. It's working but It becomes slower as number of game objects increases. I decided to give Job system a shot. My idea is to interpolate b/w positions and rotations for N game objects in parallel. It certainly running faster than single thread execution. but when I see the profiler, worker threads are sitting idle,

Here's the screenshot,

enter image description here

here's the IJob code,

using System; using System.Collections.Generic;

struct RewindJob : IJob
    public int RewindableIndex;
    public NativeArray<Vector3> positions;

    public NativeArray<Vector3> finalPositions;
    public float currentTime;

    public void Execute()
       //rewind code for interpolating b/w positions


and this is how I schedule RewindJob for each entity like this in main thread,

NativeArray<Vector3> finalPositions = new NativeArray<Vector3>(m_Objects.Count, Allocator.TempJob);

for (int i = 0; i < m_Objects.Count; i++)
    NativeArray<Vector3> positions = new NativeArray<Vector3>(count, Allocator.TempJob); ;

    TimeRewindable rewindable = m_Objects[i];

    for(int yo = 0; yo < count; yo++)
        positions[yo] = rewindable.m_Transforms[yo].position;

    RewindJob rewindJob = new RewindJob();
    rewindJob.finalPositions = finalPositions;
    rewindJob.positions = positions;
    rewindJob.RewindableIndex = i;

    JobHandle handle = rewindJob.Schedule() ;


for(int i = 0; i < m_Objects.Count; i++)
    m_Objects[i].transform.position = finalPositions[i];
    m_Objects[i].transform.rotation = finalRotations[i];


What I can understand it's not real multi-threading but context switching. Can anyone just explain a lil bit what's happening? Thanks.

  • \$\begingroup\$ Can you show us the code you're using to package up these jobs for the threads to consume? \$\endgroup\$ – DMGregory Apr 11 at 16:43
  • \$\begingroup\$ @DMGregory Thanks for responding. I added some dummy code. Cannot add the full code. But you will get the idea what I am doing. Hopefully. \$\endgroup\$ – Ankit singh kushwah Apr 11 at 16:58
  • 1
    \$\begingroup\$ It looks like you're invoking a job for each object, rather than gathering the objects that need rewinding into one big collection and letting the job system blast through them in one big chunk, distributing the work between threads as it needs. Have you tried handling this in a more batch-oriented way? \$\endgroup\$ – DMGregory Apr 11 at 17:00
  • 1
    \$\begingroup\$ Effectively. Also, since you're demanding the results back immediately and this thread's just going to be waiting till it gets them, it might as well be the one to do the work. Think of it like an outsourcing firm. You're calling each worker, one at a time, to give them one work item, and you wait on the line until they finish it. Instead, you could send the outsourcing firm a big work order, give them a deadline of next frame, then go on with the rest of what you need to get done while they do the work. At the deadline, you pick up the finished work without ever stalling to wait for it. \$\endgroup\$ – DMGregory Apr 11 at 17:08
  • 1
    \$\begingroup\$ Sounds good! If you find an alternative way to structure the work that improves your performance, please post it as an Answer below \$\endgroup\$ – DMGregory Apr 11 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.