Schedule vs Run?

What exactly is the difference between

Entities.ForEach(...).Schedule();

and

Entities.ForEach(...).Run();

?

Is it correct that Schedule uses the job system and parallelization, whereas Run runs the code on the main thread? Are there other differences?

1 Like

It’s as you said and right now, overhead for acquiring data is so slow that Run is in most cases, depending on entity count, faster than Schedule. Scheduling has quite the cost and if your code isn’t heavy in computation or entity amount, the actual code will be only a fraction of overall cost.

So beware and profile.

3 Likes

.Run will also complete all system dependencies, which mean sync point.

1 Like

Schedule - runs on one free worker thread.
ScheduleParallel - runs on all free worker threads.
Run - runs code immediately on main thread.

It completes only dependency chain which used as input for this exact job (if you have multiple chains for system or manually track handles - they wouldn’t be touched). And this is not synch point. Synch point is a specific and clearly defined thing in DOTS -
Many EntityManager operations result in structural changes that change the layout of entities in memory.
Before it can perform such operations, the EntityManager must wait for all running Jobs to complete, an event
called a sync point. A sync point both blocks the main thread and prevents the application from taking
advantage of all available cores as the running Jobs wind down.

Synch point it’s the place where all jobs in an application (no matter they have shared dependencies or not) will be completed right now and the main thread will be blocked until that.

5 Likes

The main thread will be blocked anyway, while dependency chain is completing. So if you have hard mix of .Run() and .Schedule() calls in your SystemGroup without carefully inspecting dependencies, you may have problems with perfomance.