[swift-evolution] Strings in Swift 4

Dave Abrahams dabrahams at apple.com
Tue Feb 7 12:44:22 CST 2017


on Tue Feb 07 2017, "Ted F.A. van Gaalen" <tedvgiosdev-AT-gmail.com> wrote:

>> On 7 Feb 2017, at 05:42, Karl Wagner <razielim at gmail.com> wrote:
>> 
>>> 
>>> On 6 Feb 2017, at 19:29, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org
> <mailto:swift-evolution at swift.org>> wrote:
>>> 
>> When it comes to fast access what’s most important is cache
>> locality. DRAM is like 200x slower than L2 cache. Looping through
>> some contiguous 16-bit integers is always going to beat the pants
>> out of derefencing pointers.
>
>> 
> Hi Karl
> That is of course hardware/processor dependent…and Swift runs on different target systems… isn’t? 

Actually the basic calculus holds for any modern processor.

>> It’s quite rare that you need to grab arbitrary parts of a String
>> without knowing what is inside it. If you’re saying str[12..<34] -
>> why 12, and why 34? Is 12 the length of some substring you know from
>> earlier? In that case, you could find out how many CodeUnits it had,
>> and use that information instead.
>> For this example, I have used constants here, but normally these would be variables..
>> 
>
> I’d say it is not so rare, these things are often used for all kinds of string parsing, there are
> many
> examples to be found on the Internet.
> TedvG

That proves nothing, though.  The fact that people are using integers to
do this doesn't mean you need to use them, nor does it mean that you'll
get the right results from doing so.  Typically examples that use
integer constants with strings are wrong for some large proportion of
unicode text.

-- 
-Dave


More information about the swift-evolution mailing list