String constructor returning interned string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Skeet

    #1

    String constructor returning interned string?

    I've just noticed something rather odd and disturbing. The following
    code displays "True":

    using System;

    class Test
    {
    public static void Main(string[] args)
    {
    string x = new string ("".ToCharArray ());
    string y = new string ("".ToCharArray ());
    Console.WriteLi ne (object.Referen ceEquals (x, y));
    }
    }

    In other words, new string(...) is *not* returning a new string
    reference.

    This worries me - not so much for the specific example, but for the
    precedent set. What other new ... expressions might return non-new
    references? This could have significant implications in multi-
    threading, where you may rely on two references being different for
    locking purposes.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too
  • Daniel O'Connell

    #2
    Re: String constructor returning interned string?


    "Jon Skeet" <skeet@pobox.co m> wrote in message
    news:MPG.19db5e 4bdf12820998973 d@news.microsof t.com...[color=blue]
    > I've just noticed something rather odd and disturbing. The following
    > code displays "True":
    >
    > using System;
    >
    > class Test
    > {
    > public static void Main(string[] args)
    > {
    > string x = new string ("".ToCharArray ());
    > string y = new string ("".ToCharArray ());
    > Console.WriteLi ne (object.Referen ceEquals (x, y));
    > }
    > }
    >
    > In other words, new string(...) is *not* returning a new string
    > reference.
    >
    > This worries me - not so much for the specific example, but for the
    > precedent set. What other new ... expressions might return non-new
    > references? This could have significant implications in multi-
    > threading, where you may rely on two references being different for
    > locking purposes.
    >[/color]

    This is odd...quite honestly. However, have you checked to make sure the JIT
    isn't making some kind of very clever optimzation here? Perhaps realizing
    your creating two strings from the same source(the char array of an already
    interned string), and not creating the new object but instead setting both x
    & y to the same reference?
    Otherwise this is a quite disturbing find, indeed.

    [color=blue]
    > --
    > Jon Skeet - <skeet@pobox.co m>
    > http://www.pobox.com/~skeet
    > If replying to the group, please do not mail me too[/color]


    Comment

    • Jon Skeet

      #3
      Re: String constructor returning interned string?

      Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:[color=blue][color=green]
      > > This worries me - not so much for the specific example, but for the
      > > precedent set. What other new ... expressions might return non-new
      > > references? This could have significant implications in multi-
      > > threading, where you may rely on two references being different for
      > > locking purposes.[/color]
      >
      > This is odd...quite honestly. However, have you checked to make sure the JIT
      > isn't making some kind of very clever optimzation here? Perhaps realizing
      > your creating two strings from the same source(the char array of an already
      > interned string), and not creating the new object but instead setting both x
      > & y to the same reference?[/color]

      It only happens with the empty string, as far as I can see.
      [color=blue]
      > Otherwise this is a quite disturbing find, indeed.[/color]

      I think it's disturbing either way - basically, if you rely on the new
      operator always returning a previously unknown reference, you've got
      problems.

      However, I've looked at the String(char[]) docs, and the remarks say:

      <quote>
      If value is a null reference (Nothing in Visual Basic) or contains no
      element, an Empty instance is initialized.
      </quote>

      I suspect if I hadn't known what that meant beforehand (due to seeing
      this) I wouldn't have understood it.

      I'd have thought this would actually take more work, and that there
      wouldn't really be that much benefit in it. I just wonder where else
      this might be lurking...

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Kelly Norton

        #4
        Re: String constructor returning interned string?

        There's more on .NET's string interning over on Chris Brumme's blog:



        He even has some example code snippets to illustrate how it can bite you
        when calling into unmanaged code.

        /kel

        Jon Skeet wrote:[color=blue]
        > Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
        >[color=green][color=darkred]
        >>>This worries me - not so much for the specific example, but for the
        >>>precedent set. What other new ... expressions might return non-new
        >>>references ? This could have significant implications in multi-
        >>>threading, where you may rely on two references being different for
        >>>locking purposes.[/color]
        >>
        >>This is odd...quite honestly. However, have you checked to make sure the JIT
        >>isn't making some kind of very clever optimzation here? Perhaps realizing
        >>your creating two strings from the same source(the char array of an already
        >>interned string), and not creating the new object but instead setting both x
        >>& y to the same reference?[/color]
        >
        >
        > It only happens with the empty string, as far as I can see.
        >
        >[color=green]
        >>Otherwise this is a quite disturbing find, indeed.[/color]
        >
        >
        > I think it's disturbing either way - basically, if you rely on the new
        > operator always returning a previously unknown reference, you've got
        > problems.
        >
        > However, I've looked at the String(char[]) docs, and the remarks say:
        >
        > <quote>
        > If value is a null reference (Nothing in Visual Basic) or contains no
        > element, an Empty instance is initialized.
        > </quote>
        >
        > I suspect if I hadn't known what that meant beforehand (due to seeing
        > this) I wouldn't have understood it.
        >
        > I'd have thought this would actually take more work, and that there
        > wouldn't really be that much benefit in it. I just wonder where else
        > this might be lurking...
        >[/color]

        Comment

        • Gang Peng

          #5
          Re: String constructor returning interned string?

          Hi, Jon,
          Null and Empty string are the only cases.
          This is to avoid too many instances of empty strings in CLR.
          But I agree we probably should always create new strings in new String(...)

          Gang Peng
          [MS]

          "Jon Skeet" <skeet@pobox.co m> wrote in message
          news:MPG.19db88 982cbcc91898974 5@news.microsof t.com...[color=blue]
          > Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:[color=green][color=darkred]
          > > > This worries me - not so much for the specific example, but for the
          > > > precedent set. What other new ... expressions might return non-new
          > > > references? This could have significant implications in multi-
          > > > threading, where you may rely on two references being different for
          > > > locking purposes.[/color]
          > >
          > > This is odd...quite honestly. However, have you checked to make sure the[/color][/color]
          JIT[color=blue][color=green]
          > > isn't making some kind of very clever optimzation here? Perhaps[/color][/color]
          realizing[color=blue][color=green]
          > > your creating two strings from the same source(the char array of an[/color][/color]
          already[color=blue][color=green]
          > > interned string), and not creating the new object but instead setting[/color][/color]
          both x[color=blue][color=green]
          > > & y to the same reference?[/color]
          >
          > It only happens with the empty string, as far as I can see.
          >[color=green]
          > > Otherwise this is a quite disturbing find, indeed.[/color]
          >
          > I think it's disturbing either way - basically, if you rely on the new
          > operator always returning a previously unknown reference, you've got
          > problems.
          >
          > However, I've looked at the String(char[]) docs, and the remarks say:
          >
          > <quote>
          > If value is a null reference (Nothing in Visual Basic) or contains no
          > element, an Empty instance is initialized.
          > </quote>
          >
          > I suspect if I hadn't known what that meant beforehand (due to seeing
          > this) I wouldn't have understood it.
          >
          > I'd have thought this would actually take more work, and that there
          > wouldn't really be that much benefit in it. I just wonder where else
          > this might be lurking...
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          Working...