News for hackers. Stuff that matters.
Re:Guido on Python by bitL on Friday July 31, 2015 @07:43AM (#9981155)
In other words you'd make it slow without the ability to allow it to be fast.
Re:Guido on Python by gphil on Friday July 31, 2015 @08:34AM (#9981307)
> without the ability to allow it to be fast

Practically speaking, Python is still slower than a lot of languages that use predominantly immutable data structures. I think you'd want to go to a systems programming language for raw speed anyway.

Re:Guido on Python by bitL on Friday July 31, 2015 @08:53AM (#9981384)
Imagine immutable dict. How many programs in Python you know would still work?
Re:Guido on Python by mynegation on Friday July 31, 2015 @09:09AM (#9981464)
"Immutable" data structures is rather confusing name. Here it is a synonym for persistent data structure [1], which means that you operate on data structures by creating new "versions" of them, which share a lot of memory with the old versions. Parts of the program that refer to the old version see it as immutable.

[1] https://en.wikipedia.org/wiki/Persistent_data_structure

Re:Guido on Python by tikhonj on Friday July 31, 2015 @02:05PM (#9983282)
Modern radix trees[1] can be as fast as the hash maps you'd find in a language's standard library while providing additional capabilities (ie fast ordered traversal). Making them immutable and persistent[2] is easy and only adds a bit of performance overhead.

Obviously programs that rely on communicating by mutating parts of a dictionary would have to be rewritten to use an immutable, persistent map—but they could still maintain similar performance.

[1]: Namely "adaptive radix trees" as described in “The adaptive radix tree: Artful indexing for main-memory databases” by V. Leis, A. Kemper, and T. Neumann <http://www3.informatik.tu-muenchen.de/~leis/papers/ART.pdf>

[2]: A persistent adaptive radix tree in Java: https://github.com/ankurdave/part

Re:Guido on Python by bitL on Friday July 31, 2015 @02:37PM (#9983473)
Thanks for the radix tree reference!

On the other hand, stating "they could still maintain similar performance" for immutable dictionaries is misleading at best (you are right for dictionary sizes approaching 0). Anyone can look up benchmarks on the Internet showing real-world performance of immutable data structures and make their mind up if they actually can deliver in their particular use cases.

Mutability could be always used to boost performance on Von Neumann architecture if you know what you are doing. It's just becoming impractical due to increased complexity, pervasive security paranoia and the fact that there are very few people that can get things done right. If you want to contribute to advance your case, please create a competing computer architecture as fast as Von Neumann's, physically possible, but based on immutability.

Re:Guido on Python by semi-extrinsic on Friday July 31, 2015 @04:02PM (#9984051)
> Mutability could be always used to boost performance (...)

For the extreme case, see the EQUIVALENCE statement in old Fortran.

Re:Guido on Python by wyldfire on Friday July 31, 2015 @12:29PM (#9982675)
Well, opt-in immutable dict would be awesome. I've found that many times my usage of set and list is more appropriate as their immutable counterparts (frozenset, tuple). Lots of times I create sets or lists with the intention that they be immutable and it would be clearer if I could specify to future me/maintainers/extenders that this should remain as initialized.
Re:Guido on Python by bitL on Friday July 31, 2015 @01:46PM (#9983134)
Re:Guido on Python by bpicolo on Friday July 31, 2015 @11:23PM (#9985781)
Switch to namedtuples for lightweight data structures. No regrets.
Re:Guido on Python by porker on Friday July 31, 2015 @08:01AM (#9981203)
Is that true? It doesn't match many of the comments on HN around React, Om, Clojure etc...
Re:Guido on Python by bitL on Friday July 31, 2015 @08:15AM (#9981243)
Yes, it is slow. Immutability is allowing you to write elegant concurrent algorithms at the expense of speed and memory. Imagine you are forced to never change an array of numbers for an algorithm computing matrices, rather creating new matrices for each step and specifying numbers only during initialization. But hey, you can cache them and keep track of every single copy without any confusion now! Now imagine a non-concurrent language like JavaScript - what exactly are you going to get by using immutable structures except for the feeling you are going with the times?

Reference counting and caching are probably the only areas where immutability can help for most programs.

Immutability is in fashion nowadays due to functional programming being in fashion despite the inability to ever achieve "pure" functional programming in reality - I/O is by default "dirty" (I really hate these charged words in computing), no amount of flatmapping (i.e. monads) is going to help with that.

Re:Guido on Python by mtanski on Friday July 31, 2015 @03:05PM (#9983662)
Everything immutable (every data structure) is a really convenient way of thinking about processing data in the abstract sense. You can think about processing your (semi) structure data (records) in terms of high level pipelines and processing steps. And, for a whole class of problems this works well.

Certainly this abstraction can stretch pretty well. Esp. when your language / framework can optimize immutable data structures under the covers (mutable and reuse.) But the abstraction is just that an abstraction for a mutable platform.

The platforms of today (VMs, operating systems, hardware, storage) are not immutable and notoriously full of side effects and undefined behavior. So these immutable language / frameworks end up being good tools at one extreme (high level data processing), bad at the other (system level languages) and vary on a case by case basis.

In today's todays platforms if you want to allocate an object in your language think of the layers and layers of side effects that will happen. 1. The allocator/GC system allocates it from some kind of pool possibly. Lots of side effects there (like operating on a pool, triggering some kind of collection / cleanup, requesting a large mapping from the OS. 2. Then at the OS level accessing mapped memory has all sorts of side effect (paging, lazy initialization, etc). 3. At the hardware levels there's lots of side effects: instruction cache, multiple layers of data caches, TLB, DMA.

In conclusion, for some kinds of problems you don't need to worry about these details and for others you can't afford to ignore them.

Re:Guido on Python by porker on Friday July 31, 2015 @08:29AM (#9981285)
Thanks, that's a great answer and helps me understand.

I guess there are subsets of problems where immutability is a good idea (anything needing state, 'Undo' capabilities), neutral, and a bad idea (number crunching, data processing, 'traditional'(??) programming).

I have been wowed by the beauty of the immutable approach in web apps, but definitely subscribe to the right technique for a task. I can't imagine doing the data processing in physics using an immutable language...

Re:Guido on Python by bitL on Friday July 31, 2015 @08:44AM (#9981342)
Indeed it boils down to what you need to achieve. You might want to use both approaches and combine them for best performance/readability.

There are specialized high-performance structures that are mutable (e.g. disruptor in HFT trading) optimizing all the way down to CPU cache lines to achieve unbelievable speeds, yet they work nicely if the objects they refer to are immutable.

So just take all advices to switch to immutability with a grain of salt and learn to figure out which approach is suitable for what part of your system to have it both performing well and being able to conceptualize and maintain the code nicely ;-)

Re:Guido on Python by rakoo on Friday July 31, 2015 @11:38AM (#9982394)
> Reference counting and caching are probably the only areas where immutability can help for most programs.

There is another use spurred by React: when you have a new state to represent, you have to diff it against current state to know exactly what changed and work only on this diff. "Immutable" structures make this trivial.