Arch-ECS
💬 Join the discord!☕ Buy us a coffee!
  • 🌄Why Arch?
  • 📖Documentation
    • Concepts
    • World
    • Entity
    • Query
    • Archetypes & Chunks
    • Optimizations
      • Query-Techniques
      • Pass on data
      • Batch and Bulk
      • PURE_ECS
      • Multithreading
      • EntityData
    • Utilities
      • Component Registration
      • Non-generic API
      • CommandBuffer
      • Events
      • Dangerous Extensions
  • 🧩Extensions
    • Arch.Extended
      • Arch.System
      • Arch.System.SourceGenerator
      • Arch.EventBus
      • Arch.AOT.SourceGenerator
      • Arch.LowLevel
      • Arch.Persistence
      • Arch.Relationships
  • 💡Examples & Guidelines
    • Arch.Samples
    • Entities in Query
    • Structural changes
  • Unity
  • 🎮Projects using Arch
    • Skylandkingdoms
    • Cubetory
    • SS14
    • EquilibriumEngine-CSharp
    • Rougelite-Survivor
  • ✏️Misc
    • Roadmap
    • FAQ
Powered by GitBook
On this page
  • Setup
  • Example
  • Lowlevel

Was this helpful?

Edit on GitHub
  1. Documentation
  2. Optimizations

Multithreading

Multithreading, the simultaneous iteration and modification of entities for extra speed.

PreviousPURE_ECSNextEntityData

Last updated 3 months ago

Was this helpful?

Imagine that you have millions of entities and you want to move them all. It gets a bit difficult on a CPU core. But we have a solution for that too!

This is not a problem, because Arch comes with its own . This does not generate any garbage and is incredibly fast, even has dependencies and co. What more could you want? Let's be honest!

Setup

Not much is needed to set up multithreading. You set it up, you know it and that's it!

// Create Scheduler and assign it to world
var jobScheduler = new(
  new JobScheduler.Config
  {
    ThreadPrefixName = "Arch.Samples",
    ,                         
    MaxExpectedConcurrentJobs = 64,
    StrictAllocationMode = false,
  }
);


// To dispose the JobScheduler at the end of the lifecycle.
jobScheduler.Dispose();

So the world uses the JobScheduler and you don't need direct contact to it, but if you want to, !

Example

Wonderful, now we have set up the JobScheduler, all we need to do is let it work to utilise the full capacity of our power!

var queryDesc = new QueryDescription().WithAll<Dwarf, Position, Velocity>();
(in queryDesc, {
    Move(ref pos, ref vel);
});

And now we have EVEN more power to march our armies, marvellous, isn't it? Your computer will probably take off, but it's worth it.

public struct VelocityUpdate : IForEach<Position, Velocity> {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Update(ref Position pos, ref Velocity vel) { 
        Move(ref pos, ref vel);
    }
}

world.InlineParallelQuery<VelocityUpdate, Position, Velocity>(); 

Lowlevel

If this is all too high level for you, there is also the option of tackling the problem further down. Not below the belt, of course!

public struct VelocityUpdate : IChunkJob{

   public void Execute(ref Chunk chunk) {
      
      ref var positions = ref chunk.GetFirst<Position>();
      ref var velocities = ref velocity.GetFirst<Velocity>();

      foreach(var entity in chunk){

         ref var position = ref Unsafe.Add(ref positions, entity);
         ref var velocity = ref Unsafe.Add(ref velocities, entity);
         Move(ref position, velocity);
      }
   }
}

world.InlineParallelChunkQuery(in query, );

This is what it looks like under the bonnet. This is what happens when you call the other two queries, so you can take over the whole thing yourself and add your own logic!

And you'll have even more power for everything you set out to do... honestly, what do you want with so much power anyway?

This runs a query that processes the entities simultaneously on several cores of your CPU. Arch does everything for you by itself, all you have to do is set it up and change the name of the here and there... cool right? Just be careful, you have to synchronise access to other objects yourself!

Of course, every form of is supported again. Even .

Each of these calls, whether world.ParallelQuery or world.InlineParallelQuery, will block the main thread until the has been processed. The processing itself still takes place in parallel, but the main thread waits before continuing.

📖
JobScheduler
you can also access it directly
Query
Query
Query
inline queries