Swift string via literal vs initializer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Swift string via literal vs initializer

Boon Chew

In language such as Java, under the hood there is actually a difference between string obtained via string literal vs initializer.
In Swift, are they equivalent under the hood?

var string:String = ""
var string:String = String()

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/b3dbdf21-cd82-4275-8d55-cfebe53d9856%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Jens Alfke

> On Jul 2, 2015, at 6:17 PM, Boon <[hidden email]> wrote:
>
> In language such as Java, under the hood there is actually a difference between string obtained via string literal vs initializer.

What kind of difference? I haven’t used Java much in a long time, but I used to work on the Java implementation at Apple and I don’t remember there being a difference.

> In Swift, are they equivalent under the hood?

In Cocoa an NSString created from a literal tends to be* of a different subclass than one created at runtime, but that doesn’t make any difference in its behavior; you have to inspect it at runtime (e.g. in the debugger) to detect the difference.

—Jens

* I’m being vague because the behavior varies between different OS versions, due to implementation changes in the runtime and frameworks.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/957B0B02-3BF7-4253-8F4B-93DB1FD41F95%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Boon Chew

This post explains the difference: http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext

On Friday, July 3, 2015 at 1:44:20 PM UTC-4, Jens Alfke wrote:

> On Jul 2, 2015, at 6:17 PM, Boon <<a href="javascript:" target="_blank" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">bo...@...> wrote:
>
> In language such as Java, under the hood there is actually a difference between string obtained via string literal vs initializer.

What kind of difference? I haven’t used Java much in a long time, but I used to work on the Java implementation at Apple and I don’t remember there being a difference.

> In Swift, are they equivalent under the hood?

In Cocoa an NSString created from a literal tends to be* of a different subclass than one created at runtime, but that doesn’t make any difference in its behavior; you have to inspect it at runtime (e.g. in the debugger) to detect the difference.

—Jens

* I’m being vague because the behavior varies between different OS versions, due to implementation changes in the runtime and frameworks.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/c81dba34-9146-4e52-b6f5-70ddad4d9e1b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Jens Alfke

On Jul 9, 2015, at 7:44 AM, Boon <[hidden email]> wrote:


I wouldn’t call that a difference in the objects, just in the details of how they’re instantiated. The expressions have indistinguishable results.

For what it’s worth, NSString is the same way (and I’d guess native Swift strings are too), although different binaries have their own set of static literals so a string literal from one binary may not be pointer-equal to one from another. Conversely, very short strings can be stored as tagged pointers (in 64-bit) so calling a String initializer twice may return the same pointer. It’s an implementation detail, basically, since you should never use pointer equality to compare strings.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/B182A3F4-4D38-401D-9F34-F19253DA39FC%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Boon Chew

Didn't say they are different in the object, but there is a difference under the hood in terms of where the object lies in memory, as indicated in the post.  In an extreme case, it can make a difference - for example, creating the string using literal vs initializer in a huge loop, the memory consumption will be quite different.

On Thursday, July 9, 2015 at 12:58:28 PM UTC-4, Jens Alfke wrote:

On Jul 9, 2015, at 7:44 AM, Boon <<a href="javascript:" target="_blank" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">bo...@...> wrote:

This post explains the difference: <a href="http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext" style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F3052442%2Fwhat-is-the-difference-between-text-and-new-stringtext\46sa\75D\46sntz\0751\46usg\75AFQjCNEDa3DUNRek2oqwEXXTIwX63u0SCA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F3052442%2Fwhat-is-the-difference-between-text-and-new-stringtext\46sa\75D\46sntz\0751\46usg\75AFQjCNEDa3DUNRek2oqwEXXTIwX63u0SCA&#39;;return true;">http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext

I wouldn’t call that a difference in the objects, just in the details of how they’re instantiated. The expressions have indistinguishable results.

For what it’s worth, NSString is the same way (and I’d guess native Swift strings are too), although different binaries have their own set of static literals so a string literal from one binary may not be pointer-equal to one from another. Conversely, very short strings can be stored as tagged pointers (in 64-bit) so calling a String initializer twice may return the same pointer. It’s an implementation detail, basically, since you should never use pointer equality to compare strings.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/51f8bff1-3461-4224-8e84-b7ab37d850e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Jens Alfke

On Jul 9, 2015, at 2:38 PM, Boon <[hidden email]> wrote:

Didn't say they are different in the object, but there is a difference under the hood in terms of where the object lies in memory, as indicated in the post.  

Either way the string is in the Java GC heap. There’s no difference. There is a “string constant pool” but it’s not a different place in memory; it’s conceptually just a global Map or Set object that keeps track of unique instances of strings.

In an extreme case, it can make a difference - for example, creating the string using literal vs initializer in a huge loop, the memory consumption will be quite different.

Sure, but that’s because one way explicitly creates a new object every time through the loop, while the other approach uses a single object. That’s a general optimization that has nothing to do specifically with strings.

The real question here is, “does a string literal return the same object every time it’s evaluated at runtime”, and that’s true in pretty much every language I know of.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/F34A1D6B-76ED-4DE3-88B1-9CFCD19A0638%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Boon Chew
They are not necessarily in the same memory, given that the source is different - it depends on the language you are using.  That's why I mention under the hood early on.  It would be misleading to state there is no difference, because technically if they are the same, the compiled code generated for either one should be identical.  It's definitely not the case for all languages.

On Thu, Jul 9, 2015 at 5:49 PM, Jens Alfke <[hidden email]> wrote:

On Jul 9, 2015, at 2:38 PM, Boon <[hidden email]> wrote:

Didn't say they are different in the object, but there is a difference under the hood in terms of where the object lies in memory, as indicated in the post.  

Either way the string is in the Java GC heap. There’s no difference. There is a “string constant pool” but it’s not a different place in memory; it’s conceptually just a global Map or Set object that keeps track of unique instances of strings.

In an extreme case, it can make a difference - for example, creating the string using literal vs initializer in a huge loop, the memory consumption will be quite different.

Sure, but that’s because one way explicitly creates a new object every time through the loop, while the other approach uses a single object. That’s a general optimization that has nothing to do specifically with strings.

The real question here is, “does a string literal return the same object every time it’s evaluated at runtime”, and that’s true in pretty much every language I know of.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAMOf6JtyUrVUiw1xr8J%3DgDtCt1ELtBZFOJSgftYD2GxEPp1J9g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Swift string via literal vs initializer

Jens Alfke

On Jul 9, 2015, at 2:55 PM, Boon Chew <[hidden email]> wrote:

They are not necessarily in the same memory, given that the source is different - it depends on the language you are using.

It does depend on the language, but I was referring specifically to Java.

It would be misleading to state there is no difference, because technically if they are the same, the compiled code generated for either one should be identical.  It's definitely not the case for all languages.

By "difference between string obtained via string literal vs initializer”, I assumed you meant a difference in the objects. I agree that there’s a difference in the code path. Sounds like we’re in violent agreement :)

Anyway, the answer to the original question is that in Swift, as in most languages, a string literal is simply a reference to a String object that’s created at initialization time (probably when the binary containing the code is loaded.) It does not allocate memory every time it’s referenced.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/DE013863-D403-4A95-B171-314816135966%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.