How to find out if I need .ValueRO vs .ValueRW

    private void StartServer()
    {
        var serverWorld = ClientServerBootstrap.CreateServerWorld("Server World");
        
        var endPoint = NetworkEndpoint.AnyIpv4.WithPort(_port);
        using var networkDriverQuery = serverWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
     
        networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(endPoint);
    }

When using entities, i need to use .ValueRO(Read Only) or valueRW (Read and Write), that is easy to get, but how should do i figure out if the Listen is writing anything?

Access it with ValueRW. You can typically just look at the source code for the method in question and double check if it modifies any fields or does anything that would mutate them.

In general though, any operations that can change the state of the instance (like in this case, NetworkStreamDriver.Listen) should be accessed with a non-readonly ref. Only pure (no state changes, like LocalTransform.ToMatrix) methods should be accessed with readonly refs.

You could use readonly refs to call methods that change the state of the application if they don’t change that specific instance, where they instead change global state or data of the arguments passed to the method, but it’s a safe practice to use a mutable reference if you’re not sure about what the method is doing and you want changes to persist, as calling non-readonly instance methods on readonly instances of struct types would cause a defensive copy to be created for that call.

Thank you very much for the explanation. Can i assume this is for performance reasons?

What in particular are you referring to?

Allowing designated retrieval of read-only references is part of the framework design that allows for multiple readers to execute in parallel, so using read-only references where appropriate is indeed a performance benefit. It’s not something intrinsic to using read-only or read-write references though, it’s the fact that the framework has things like codegen for IJobEntity / SystemAPI.Query and underlying management of readers/writers for specific component types that enforces safe parallel execution. Attempting to obtain write access with something like ValueRW when readers are in flight should cause a safety exception when running in the editor (builds strip safety management to some extent), so your choices there would be to either force the dependencies to complete before proceeding or, if appropriate for your circumstances, access the data as read-only and be able to read the data while other reader jobs are executing.

So the performance improvement is from parallel execution. Thanks!