performance penalty of message passing as opposed to shared data

后端 未结 7 2347
梦谈多话
梦谈多话 2021-02-15 12:38

There is a lot of buzz these days about not using locks and using Message passing approaches like Erlang. Or about using immutable datastructures like in Functional programming

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-15 13:19

    For e.g. in a DB, you have to access and modify the same record

    But that is handled by the DB. As a user of the database, you simply execute your query, and the database ensures it is executed in isolation.

    As for performance, one of the most important things about eliminating shared state is that it enables new optimizations. Shared state is not particularly efficient. You get cores fighting over the same cache lines, and data has to be written through to memory where it could otherwise stay in a register or in CPU cache.

    Many compiler optimizations rely on absence of side effects and shared state as well.

    You could say that a stricter language guaranteeing these things requires more optimizations to be performant than something like C, but it also makes these optimizations much much easier for the compiler to implement.

    Many concerns similar to concurrency issues arise in singlethreaded code. Modern CPUs are pipelined, execute instructions out of order, and can run 3-4 of them per cycle. So even in a single-threaded program, it is vital that the compiler and CPU is able to determine which instructions can be interleaved and executed in parallel.

提交回复
热议问题