mattk1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi. In Java and Python are some data types immutable. For instance a string. In Perl a string is mutable. Why do the designers of a language decide that some are immutable and some not? Is it something about threads and the balanced use of n cpu's or??? Regards Matt
  • Comment on programming languages and immutable data types

Replies are listed 'Best First'.
Re: programming languages and immutable data types
by BrowserUk (Patriarch) on May 03, 2010 at 21:49 UTC

    The problem with mandating that strings be immutable, is that whilst it works okay for occasional modifications to run-of-the-mill short strings, it imposes a high cost on frequent changes to large ones.

    And as "strings" are frequently the only mechanism for allocating (often) large chunks of memory for use (for example) as buffers, it can extract a high cost. Effectively discarding one of the primary benefits of threads over separate processes.

    Imagine the cost of the use of ramfiles if every write had to duplicate the buffer.

    Allowing, and even defaulting strings to immutable makes some sense, provided that you retain the ability to also allocate mutable chunks of ram. Immutability is an artificial constraint born out of a particular viewpoint of programming. Imposing it upon programmers is like imposing vegetarianism upon the world.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: programming languages and immutable data types
by moritz (Cardinal) on May 03, 2010 at 21:13 UTC
    In Perl a string is mutable.

    Actually in Perl 5 that's an implementation detail. You can't modify string literals, you can only mutate variables which hold strings. A (non-XS) Perl programmer wouldn't see the difference to an immutable string.

    But yet, many modern languages tend towards immutable basic data types because it makes multi threading easier - an immutable value can be shared between threads without the need for locking or copying.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: programming languages and immutable data types
by ikegami (Patriarch) on May 03, 2010 at 21:18 UTC

    If a type is immutable, functions and container objects can share the instance to save memory. In essence, it's a high level copy-on-write scheme.

    If a type is immutable, you don't risk breaking code by accidentally sharing between functions and contained objects. Since strings are objects in Java, assigning a string to a variable shares the instance rather than making a copy.

    If a type is immutable, you can share it across threads without having to use locks to read from it. This is an important factor in Java since it promotes the use of threads.

Re: programming languages and immutable data types
by Marshall (Canon) on May 03, 2010 at 21:53 UTC
Re: programming languages and immutable data types
by LanX (Saint) on May 03, 2010 at 22:24 UTC
    Hmm can you plz show me the practical difference to perl?

    lanx@nc10-ubuntu:~$ python Python 2.5.2 (r252:60911, Jan 20 2010, 23:16:55) ... >>> str="123" >>> str+="4" >>> str '1234'

    The only thing I can think of are runtime issues :

    I've read that in JS  str+="a";str+="b" takes 40% longer than str=str+"a"+"b", because strings have to be copied...

    But in perl the second version needs much longer...

    DB<1> $str="";$t=time;for (1..100000) {$str=$str."a"."b"};print time +-$t 13 DB<2> $str="";$t=time;for (1..1000000) {$str.="a";$str.="b"};print t +ime-$t 1

    Actually the same code in python has similar benchmarks

    >>> for i in range(1, 1000000): ... str+="a"+"b" ... >>> str="" >>> for i in range(1, 100000): ... str=str+"a"+"b" ...

    So where is the practical difference?

    Cheers Rolf

    UPDATE: Maybe not a good benchmark, since the growing of $str plays a too important role ...

    DB<1> $t=time;for (1..10000000) {$str="";$str.="a"."b";};print time- +$t 10 DB<2> $t=time;for (1..10000000) {$str="";$str=$str."a";$str.="b";};p +rint time-$t 17 DB<3> $t=time;for (1..10000000) {$str="";$str=$str."a"."b";};print t +ime-$t 15

      There are other ways of mutating a string than just appending to the end. For example:

      $n=1e3; cmpthese -1,{ a=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ substr $s, $i, 1, chr(1); } ], b=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ $s = substr($s,0,$i-1) . chr(1) . substr $s,$i+1; } ] };; Rate b a b 811/s -- -85% a 5525/s 581% -- $n=1e4; cmpthese -1,{ a=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ substr $s, $i, 1, chr(1); } ], b=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ $s = substr($s,0,$i-1) . chr(1) . substr $s,$i+1; } ] };; Rate b a b 13.0/s -- -98% a 556/s 4173% -- $n=1e5; cmpthese -1,{ a=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ substr $s, $i, 1, chr(1); } ], b=>q[ my$s = chr(0)x$n; for my $i ( 0..length($s)-1){ $s = substr($s,0,$i-1) . chr(1) . substr $s,$i+1; } ] };; (warning: too few iterations for a reliable count) s/iter b a b 12.5 -- -100% a 1.77e-002 70277% --

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.