Why don't purely functional languages use reference counting?

前端 未结 6 1300
忘了有多久
忘了有多久 2021-02-02 06:27

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional la

6条回答
  •  暖寄归人
    2021-02-02 06:51

    Relative to other managed languages like Java and C#, purely functional languages allocate like crazy. They also allocate objects of different sizes. The fastest known allocation strategy is to allocate from contiguous free space (sometimes called a "nursery") and to reserve a hardware register to point to the next available free space. Allocation from the heap becomes as fast as allocation from a stack.

    Reference counting is fundamentally incompatible with this allocation strategy. Ref counting puts objects on free lists and takes them off again. Ref counting also has substantial overheads required for updating ref counts as new objects are created (which, as noted above, pure functional languages do like crazy).

    Reference counting tends to do really well in situations like these:

    • Almost all heap memory is used to hold live objects.
    • Allocation and pointer assignment are infrequent relative to other operations.
    • References can be managed on another processor or computer.

    To understand how the best high-performance ref-counting systems work today, look up the work of David Bacon and Erez Petrank.

提交回复
热议问题