8

As I understand it, writing your objects in an immutable style can help the performance of your program for compiled languages, as the compiler tends to rewrite your code in single static assignment form under the hood anyway.

Is there a performance benefit to using immutable objects in an interpreted language like Python or PHP?

I understand there may be readability or code quality benefits from immutability, but I am asking specifically about performance.

2
  • Any sources for the reasoning in your first paragraph? I don't doubt GHC and friends does explot immutability for all it's worth, but I'd be surprised if that blanket statement applies to all compilers.
    – user7043
    Mar 14, 2012 at 16:12
  • No source, just what I remember from compilers class.. Mar 14, 2012 at 16:15

4 Answers 4

7

In CPython, allocation of tuples (basically immutable lists) can be slightly faster than an allocation of the equivalent mutable type with the same items. I vaguely remember similar rumors about immuable sets, but timeit didn't confirm it. Tuples can also be smaller than lists, from a few bytes to 50% (if you hit a spot where the list had to resize, and doubled its capacity to speed up future growth). Moreover, tuples and sets are - in some contexts - subject to peephole optimizations which allow creating the object once and re-using it instead of re-building it every time.

But that's mostly peanuts. It's not why we use these types, at least usually. If ever, these differences are only invoked when seriously optimizing. It's not an aspect I usually consider when deciding for either, especially since there are other benefits, not to mention the enormous semantic impact.

3

The major benefit of immutable variables come the moment you go distributed and have your data spread over several computers. Mutable data needs elaborate mechanisms to ensure that when data is updated, all copies in various caches are updated too, which besides being elaborate also take time.

Immutable data can be copied and cached as much as is needed. You are guaranteed that the data will not change.

I do not know if Jython supports multi-threading, if so, it would be an advantage on heavily multithreaded, multi-core applications.

2
  • Would have downvoted if I had reputation enough, he was pretty clear about the question scope: "I understand there may be readability or code quality benefits from immutability, but I am asking specifically about performance." Dec 29, 2018 at 23:04
  • I think he is pretty clear. If there is multi-thread/core support immutable structures don't require the overhead of syncing. I will add that, depending on the context of business, you might need to get the latest object; but the one you have cached is still true, is still valid. Jun 18, 2020 at 10:02
0

In Python there is nothing in the language itself to mark an object as immutable so no hints can be given to the interpreter for optimization.

If the process is threaded and the object has "protected" properties then programmers don't need to worry about synchronization when using that object so there could be a performance gain.

3
  • I'm confused by your answer, because python does indeed have immutable objects. What does "mark[ing] an object" have to do with the use of existing immutable objects in python? Mar 15, 2012 at 12:18
  • There are immutable built in objects. I am talking about user defined objects. There is no flag or other method to indicate to the interpreter that the user defined object is immutable thus allowing optimizations to occur. Mar 15, 2012 at 12:44
  • 1
    Fast forward to 2020, there are dataclasses and frozen dataclasses. Frozen dataclasses are immutable classes. Jun 18, 2020 at 9:59
0

in Python and PHP there's very little benefit, most of it because immutable structures are usually simpler and smaller because they don't have to support any modifying feature.

OTOH, modern JITs (like LuaJIT or any modern JavaScript) could take advantage of any opportunity to constrain possibilities and produce tighter code (similar to what compilers do, but way more aggressively).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.