Re: [swift-users] Why are Swift Loops slow?

2016-10-12 Thread Gerriet M. Denkmann via swift-users

> On 12 Oct 2016, at 23:05, Joe Groff via swift-users  
> wrote:
> 
>> 
>> On Oct 12, 2016, at 2:25 AM, Gerriet M. Denkmann via swift-users 
>>  wrote:
>> 
>> uint64_t nbrBytes = 4e8;
>> uint64_t count = 0;
>> for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
>> {
>>  count += byteIndex;
>>  if ( ( byteIndex & 0x ) == 0 ) { count += 1.3; }  (AAA) 
>> };
>> 
>> Takes 260 msec.
>> 
>> Btw.: Without the (AAA) line the whole loop is done in 10 μsec. A really 
>> clever compiler!
>> And with “count += 1” instead of “count += 1.3” it takes 410 msec. Very 
>> strange. 
>> But this is beside the point here.
>> 
>> 
>> Now Swift:
>> let nbrBytes = 400_000_000
>> var count = 0
>> for byteIndex in 0 ..< nbrBytes
>> {
>>  count += byteIndex
>>  if ( ( byteIndex & 0x ) == 0 ) {count += Int(1.3);}
>> }
>> 
>> takes 390 msec - about 50 % more.
>> 
>> Release build with default options.
> 
> This is a useless benchmark because the loop does no observable work in 
> either language.

I quite agree that this is useless as a benchmark.
It is an abstraction of some real code, which has the common feature that 
inside the loop is not done anything lengthy.

Following some very helpful remarks from Greg Parker I was able to improve the 
Swift code by a factor of 3, making it on par with ObjC:
1. using a signed loop counter (factor of 2)
2. using “count = count &+ something” instead of “count += something” (factor 
of 1.5).

So this exercise gave me some very valuable insights into using Swift (or into 
some shortcomings of the current Swift compiler).

Thanks again to Greg Parker for his comments and suggestions!

Kind regards,

Gerriet.

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Hooman Mehr via swift-users

> On Oct 12, 2016, at 3:21 AM, Jean-Denis Muys via swift-users 
>  wrote:
> 
> 
> But this is not very DRY.
> 
> What would be a more idiomatic way?
> 

The more idiomatic way is to look at API design in a new way. Note these points:

1. `Countable` variant is preferred when you want to deal with integer ranges 
as it more closely matches the element type.
2. Both countable range variants share a common protocol conformance already: 
`RandomAccessCollection`
3. Swift API design prefers member functions to free functions.

Hence a more idiomatic (Swifty) API would probably be something like this:

extension RandomAccessCollection {

func random() -> Iterator.Element? {

guard count > 0 else { return nil }

let offset = arc4random_uniform(numericCast(count))

let i = index(startIndex, offsetBy: numericCast(offset))

return self[i]
}
}

Using the above, both cases work and there is no repetition:

(4..<10).random()
(4...9).random()

It also makes a lot more possible: 

let people = ["David", "Chris", "Joe", "Jordan", "Tony"]
let winner = people.random()___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Installation permissions issue with 3.0 release in Ubuntu 14.04?

2016-10-12 Thread Owen Smith via swift-users
Greetings,

I'm not sure where to take this issue, so I'm starting here. It's an
installation issue, not a language issue. Any tips on where to send this
would be greatly appreciated! (And of course please let me know if it's a
known issue.)

I'm one of the brave that is installing Swift 3.0 on Ubuntu 14.04 as a
system installation. I'd love to be able to take the release tarball and
simply expand it as-is to the final location in /usr.

However, there's one significant issue: many CoreFoundation files in the
archive appear to be marked with a permission of 640, which means they're
unusable by the general population on the server when installed straight
up. It's not hard to fix, but it's annoying.

Specifically, it's these files:

640 /usr/lib/swift/CoreFoundation/CFArray.h

640 /usr/lib/swift/CoreFoundation/CFAsmMacros.h

640 /usr/lib/swift/CoreFoundation/CFAttributedString.h

640 /usr/lib/swift/CoreFoundation/CFAvailability.h

640 /usr/lib/swift/CoreFoundation/CFBag.h

640 /usr/lib/swift/CoreFoundation/CFBase.h

640 /usr/lib/swift/CoreFoundation/CFBasicHash.h

640 /usr/lib/swift/CoreFoundation/CFBigNumber.h

640 /usr/lib/swift/CoreFoundation/CFBinaryHeap.h

640 /usr/lib/swift/CoreFoundation/CFBitVector.h

640 /usr/lib/swift/CoreFoundation/CFBundle_BinaryTypes.h

640 /usr/lib/swift/CoreFoundation/CFBundle.h

640 /usr/lib/swift/CoreFoundation/CFBundle_Internal.h

640 /usr/lib/swift/CoreFoundation/CFBundlePriv.h

640 /usr/lib/swift/CoreFoundation/CFBurstTrie.h

640 /usr/lib/swift/CoreFoundation/CFByteOrder.h

640 /usr/lib/swift/CoreFoundation/CFCalendar.h

640 /usr/lib/swift/CoreFoundation/CFCharacterSet.h

640 /usr/lib/swift/CoreFoundation/CFCharacterSetPriv.h

640 /usr/lib/swift/CoreFoundation/CFData.h

640 /usr/lib/swift/CoreFoundation/CFDateFormatter.h

640 /usr/lib/swift/CoreFoundation/CFDate.h

640 /usr/lib/swift/CoreFoundation/CFDictionary.h

640 /usr/lib/swift/CoreFoundation/CFError.h

640 /usr/lib/swift/CoreFoundation/CFError_Private.h

640 /usr/lib/swift/CoreFoundation/CFICUConverters.h

640 /usr/lib/swift/CoreFoundation/CFICULogging.h

640 /usr/lib/swift/CoreFoundation/CFInternal.h

640 /usr/lib/swift/CoreFoundation/CFLocale.h

640 /usr/lib/swift/CoreFoundation/CFLocaleInternal.h

640 /usr/lib/swift/CoreFoundation/CFLogUtilities.h

640 /usr/lib/swift/CoreFoundation/CFMachPort.h

640 /usr/lib/swift/CoreFoundation/CFMessagePort.h

640 /usr/lib/swift/CoreFoundation/CFNumberFormatter.h

640 /usr/lib/swift/CoreFoundation/CFNumber.h

640 /usr/lib/swift/CoreFoundation/CFPlugInCOM.h

640 /usr/lib/swift/CoreFoundation/CFPlugIn_Factory.h

640 /usr/lib/swift/CoreFoundation/CFPlugIn.h

640 /usr/lib/swift/CoreFoundation/CFPreferences.h

640 /usr/lib/swift/CoreFoundation/CFPriv.h

640 /usr/lib/swift/CoreFoundation/CFPropertyList.h

640 /usr/lib/swift/CoreFoundation/CFRegularExpression.h

640 /usr/lib/swift/CoreFoundation/CFRunArray.h

640 /usr/lib/swift/CoreFoundation/CFRunLoop.h

640 /usr/lib/swift/CoreFoundation/CFRuntime.h

640 /usr/lib/swift/CoreFoundation/CFSet.h

640 /usr/lib/swift/CoreFoundation/CFSocket.h

640 /usr/lib/swift/CoreFoundation/CFStorage.h

640 /usr/lib/swift/CoreFoundation/CFStreamAbstract.h

640 /usr/lib/swift/CoreFoundation/CFStream.h

640 /usr/lib/swift/CoreFoundation/CFStreamInternal.h

640 /usr/lib/swift/CoreFoundation/CFStreamPriv.h

640 /usr/lib/swift/CoreFoundation/CFStringDefaultEncoding.h

640 /usr/lib/swift/CoreFoundation/CFStringEncodingConverterExt.h

640 /usr/lib/swift/CoreFoundation/CFStringEncodingConverter.h

640 /usr/lib/swift/CoreFoundation/CFStringEncodingConverterPriv.h

640 /usr/lib/swift/CoreFoundation/CFStringEncodingDatabase.h

640 /usr/lib/swift/CoreFoundation/CFStringEncodingExt.h

640 /usr/lib/swift/CoreFoundation/CFString.h

640 /usr/lib/swift/CoreFoundation/CFStringLocalizedFormattingInternal.h

640 /usr/lib/swift/CoreFoundation/CFTimeZone.h

640 /usr/lib/swift/CoreFoundation/CFTree.h

640 /usr/lib/swift/CoreFoundation/CFUniChar.h

640 /usr/lib/swift/CoreFoundation/CFUniCharPriv.h

640 /usr/lib/swift/CoreFoundation/CFUnicodeDecomposition.h

640 /usr/lib/swift/CoreFoundation/CFUnicodePrecomposition.h

640 /usr/lib/swift/CoreFoundation/CFURLAccess.h

640 /usr/lib/swift/CoreFoundation/CFURLComponents.h

640 /usr/lib/swift/CoreFoundation/CFURL.h

640 /usr/lib/swift/CoreFoundation/CFURL.inc.h

640 /usr/lib/swift/CoreFoundation/CFURLPriv.h

640 /usr/lib/swift/CoreFoundation/CFURLSessionInterface.h

640 /usr/lib/swift/CoreFoundation/CFUserNotification.h

640 /usr/lib/swift/CoreFoundation/CFUtilities.h

640 /usr/lib/swift/CoreFoundation/CFUUID.h

640 /usr/lib/swift/CoreFoundation/CFXMLInputStream.h

640 /usr/lib/swift/CoreFoundation/CFXMLInterface.h

640 /usr/lib/swift/CoreFoundation/CFXMLNode.h

640 /usr/lib/swift/CoreFoundation/CFXMLParser.h

640 /usr/lib/swift/CoreFoundation/CoreFoundation.h

640 /usr/lib/swift/CoreFoundation/CoreFoundation_Prefix.h

640 /usr/lib/swift/CoreFoundation/ForFoundationOnly.h

640 /usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h

6

Re: [swift-users] Counting in Threads

2016-10-12 Thread Daniel Dunbar via swift-users
Not in this case, today:
--
$ cat x.swift 
import Darwin.C

public class AtomicInt32 {
   public fileprivate (set) var value : Int32 = 0

   /// Create a new atomic integer with the specified initial value.
   public init(_ value: Int32 = 0) {
   self.value = value
   }

   /// Add one to the value.
   public func increment () {
   OSAtomicIncrement32(&value)
   }
}

$ swiftc -S -o - x.swift -O | grep '.globl  
__TFC4main11AtomicInt329incrementfT_T_' -A16
.globl  __TFC4main11AtomicInt329incrementfT_T_
.align  4, 0x90
__TFC4main11AtomicInt329incrementfT_T_:
.cfi_startproc
pushq   %rbp
Ltmp4:
.cfi_def_cfa_offset 16
Ltmp5:
.cfi_offset %rbp, -16
movq%rsp, %rbp
Ltmp6:
.cfi_def_cfa_register %rbp
leaq16(%rdi), %rsi
movl$1, %edi
popq%rbp
jmp _OSAtomicAdd32
.cfi_endproc
--

 - Daniel

> On Oct 12, 2016, at 8:31 AM, Philippe Hausler  wrote:
> 
> I was under the impression that taking the address was more than a single 
> load instruction and would emit a placeholder invalid value: which would make 
> that technically unsafe in a threaded context.
> 
> Sent from my iPhone
> 
> On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users 
> mailto:swift-users@swift.org>> wrote:
> 
>> I suspect one of the actual compiler people might tell me I shouldn't trust 
>> this, but in practice it works:
>> --
>> import Darwin.C
>> 
>> public class AtomicInt32 {
>>public fileprivate (set) var value : Int32 = 0
>> 
>>/// Create a new atomic integer with the specified initial value.
>>public init(_ value: Int32 = 0) {
>>self.value = value
>>}
>> 
>>/// Add one to the value.
>>public func increment () {
>>OSAtomicIncrement32(&value)
>>}
>> }
>> 
>> public func +=(int: AtomicInt32, value: Int32) {
>>OSAtomicAdd32(value, &int.value)
>> }
>> --
>> 
>> Would also love to know if compiler guarantees I *can* trust this.
>> 
>> Note that this has to be a class for this to be in any way safe, which means 
>> it is also rather inefficient if the use case was having a lot of them.
>> 
>> - Daniel
>> 
>>> On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users 
>>> mailto:swift-users@swift.org>> wrote:
>>> 
>>> 
>>> How to translate this to Swift:
>>> 
>>> __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
>>> dispatch_apply( nbrInterations, queue, ^void(size_t idx)
>>>{
>>>uint64_t tCount = 0;
>>>... do some counting ...
>>>atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );
>>>}
>>> )
>>> 
>>> Currently I am using:
>>> 
>>> var counter: UInt64 = 0
>>> let dsema = DispatchSemaphore(value: 1)  
>>> DispatchQueue.concurrentPerform( iterations: nbrInterations )
>>> { ( idx: size_t) -> Void in
>>>
>>>var tCount: UInt64 = 0
>>>... do some counting ...
>>>_ = dsema.wait(timeout: .distantFuture) 
>>>counter += tCount;
>>>dsema.signal()  
>>> }
>>> 
>>> Is there a better way?
>>> 
>>> Gerriet.
>>> 
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-users 
>>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users 
>> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Jenus Dong via swift-users
Yes,  it is so inconvenient to use that CloseRange isn’t the special of Range.  
But according to your case,  CountableRange can be used in the condition. And 
ClosedCountableRange and CountableRange is  available to converting. Refer: 
https://oleb.net/blog/2016/09/swift-3-ranges/
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Counting in Threads

2016-10-12 Thread Philippe Hausler via swift-users
Or allocate a pointer for it. The other alternative would be NSLock but that 
might be swatting flies with sledgehammers. We had a similar issue implementing 
NSLock in swift-corelibs-foundation where taking the address of a pthread mutex 
corrupted the structure for when another thread would attempt to grab it.

Sent from my iPhone

> On Oct 12, 2016, at 4:45 PM, Shawn Erickson  wrote:
> 
> So we would have to drop down to C code, etc. to safely leverage OSAtomic?
> 
>> On Wed, Oct 12, 2016 at 8:32 AM Philippe Hausler via swift-users 
>>  wrote:
>> I was under the impression that taking the address was more than a single 
>> load instruction and would emit a placeholder invalid value: which would 
>> make that technically unsafe in a threaded context.
>> 
>> Sent from my iPhone
>> 
>>> On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users 
>>>  wrote:
>>> 
>>> I suspect one of the actual compiler people might tell me I shouldn't trust 
>>> this, but in practice it works:
>>> --
>>> import Darwin.C
>>> 
>>> public class AtomicInt32 {
>>>public fileprivate (set) var value : Int32 = 0
>>> 
>>>/// Create a new atomic integer with the specified initial value.
>>>public init(_ value: Int32 = 0) {
>>>self.value = value
>>>}
>>> 
>>>/// Add one to the value.
>>>public func increment () {
>>>OSAtomicIncrement32(&value)
>>>}
>>> }
>>> 
>>> public func +=(int: AtomicInt32, value: Int32) {
>>>OSAtomicAdd32(value, &int.value)
>>> }
>>> --
>>> 
>>> Would also love to know if compiler guarantees I *can* trust this.
>>> 
>>> Note that this has to be a class for this to be in any way safe, which 
>>> means it is also rather inefficient if the use case was having a lot of 
>>> them.
>>> 
>>> - Daniel
>>> 
 On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users 
  wrote:
 
 
 How to translate this to Swift:
 
 __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
 dispatch_apply( nbrInterations, queue, ^void(size_t idx)
{
uint64_t tCount = 0;
... do some counting ...
atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );
}
 )
 
 Currently I am using:
 
 var counter: UInt64 = 0
 let dsema = DispatchSemaphore(value: 1)  
 DispatchQueue.concurrentPerform( iterations: nbrInterations )
 { ( idx: size_t) -> Void in

var tCount: UInt64 = 0
... do some counting ...
_ = dsema.wait(timeout: .distantFuture) 
counter += tCount;
dsema.signal()  
 }
 
 Is there a better way?
 
 Gerriet.
 
 ___
 swift-users mailing list
 swift-users@swift.org
 https://lists.swift.org/mailman/listinfo/swift-users
>>> 
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Build Android GUI apps with Swift 3.0 via a framework/library

2016-10-12 Thread Tony Constantinides via swift-users
In Swift 3.0 you can build Android apps in Linux but only console apps as
there is no framework to build GUI apps using JNI.
What I propose is to build an initial limited framework coded in C that
calls enough of the Java Android API via JNI to bootstrap the android app
and to create widgets and layouts.A default Androidmanifest.xml file and
some files needed to be generated to make a valid android app.
  The Android API java surface is vast, so this framework needs to be build
over many releases to be useful. Developing a graphical Android app
requires interaction with "Activities" and the package manager and some
widgets like Button and some layouts like "RelativeLayout" and
"LinearLayout".
 The result will be the ability to develop GUI Android apps on Linux using
Swift 3.0
Further support for additional APIs will be provided once the basics are
solid..
Who am I: Senior Android mobile developer with more than six years
experience on Android.
Am I able to build Swift 3.0 on Linux: Yes
Any help, suggestions, or ideas are most welcome
Warms regards,
Tony Constantinides
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Nevin Brackett-Rozinsky via swift-users
You could also create a “Range” protocol with “lowerBound” and “upperBound”
properties. Conform all the range types to it, and make your function take
generic over the protocol.

Nevin


On Wed, Oct 12, 2016 at 7:21 PM, Hooman Mehr via swift-users <
swift-users@swift.org> wrote:

> I recommend having explicit precondition and reducing repetition like this:
>
> import Foundation
>
> func random(from range: CountableRange) -> Int {
>
> precondition(range.count > 0,
>  "The range can't be empty.")
>
> return random(from: CountableClosedRange(range))
> }
>
> func random(from range: CountableClosedRange) -> Int {
>
> let lowerBound = range.lowerBound
> let upperBound = range.upperBound
>
> precondition(upperBound - lowerBound < Int(UInt32.max),
>  "The range \(range) is too wide. It shouldn't be wider
> than \(UInt32.max).")
>
> return lowerBound + Int(arc4random_uniform(UInt32(upperBound -
> lowerBound + 1)))
> }
>
> let r1 = random(from: 4 ..< 8)
> let r2 = random(from: 6 ... 8)
>
> Once we have the new improved Integer protocols
> 
>  in
> place, you will be able to make it generic to support all integer types.
> (It is possible now, but too messy to be worth doing.)
>
>
> On Oct 12, 2016, at 1:23 PM, Adriano Ferreira via swift-users <
> swift-users@swift.org> wrote:
>
> Hi there!
>
> Ole Begeman offers here  (take
> a look at the bottom of the page) an interesting consideration about
> converting between half-open and closed ranges.
>
> As of now, it seems the way to go is by overloading…
>
>
> import Foundation
>
> func random(from range: Range) -> Int {
> let lowerBound = range.lowerBound
> let upperBound = range.upperBound
>
> return lowerBound + Int(arc4random_uniform(UInt32(upperBound -
> lowerBound)))
> }
>
> func random(from range: ClosedRange) -> Int {
> let lowerBound = range.lowerBound
> let upperBound = range.upperBound
>
> return lowerBound + Int(arc4random_uniform(UInt32(upperBound -
> lowerBound + 1)))
> }
>
> let r1 = random(from: 4 ..< 8)
> let r2 = random(from: 6 ... 8)
>
>
> Cheers,
>
> — A
>
> On Oct 12, 2016, at 6:21 AM, Jean-Denis Muys via swift-users <
> swift-users@swift.org> wrote:
>
> Hi,
>
> I defined this:
>
> func random(from r: Range) -> Int {
> let from = r.lowerBound
> let to =  r.upperBound
>
> let rnd = arc4random_uniform(UInt32(to-from))
> return from + Int(rnd)
> }
>
> so that I can do:
>
> let testRandomValue = random(from: 4..<8)
>
> But this will not let me do:
>
> let otherTestRandomValue = random(from: 4...10)
>
> The error message is a bit cryptic:
>
> “No ‘…’ candidate produce the expected contextual result type ‘Range’”
>
> What is happening is that 4…10 is not a Range, but a ClosedRange.
>
> Of course I can overload my function above to add a version that takes a
> ClosedRange.
>
> But this is not very DRY.
>
> What would be a more idiomatic way?
>
> Thanks,
>
> Jean-Denis
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Counting in Threads

2016-10-12 Thread Shawn Erickson via swift-users
So we would have to drop down to C code, etc. to safely leverage OSAtomic?

On Wed, Oct 12, 2016 at 8:32 AM Philippe Hausler via swift-users <
swift-users@swift.org> wrote:

> I was under the impression that taking the address was more than a single
> load instruction and would emit a placeholder invalid value: which would
> make that technically unsafe in a threaded context.
>
> Sent from my iPhone
>
> On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users <
> swift-users@swift.org> wrote:
>
> I suspect one of the actual compiler people might tell me I shouldn't
> trust this, but in practice it works:
> --
> import Darwin.C
>
> public class AtomicInt32 {
>public fileprivate (set) var value : Int32 = 0
>
>/// Create a new atomic integer with the specified initial value.
>public init(_ value: Int32 = 0) {
>self.value = value
>}
>
>/// Add one to the value.
>public func increment () {
>OSAtomicIncrement32(&value)
>}
> }
>
> public func +=(int: AtomicInt32, value: Int32) {
>OSAtomicAdd32(value, &int.value)
> }
> --
>
> Would also love to know if compiler guarantees I *can* trust this.
>
> Note that this has to be a class for this to be in any way safe, which
> means it is also rather inefficient if the use case was having a lot of
> them.
>
> - Daniel
>
> On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users <
> swift-users@swift.org> wrote:
>
>
>
> How to translate this to Swift:
>
>
> __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
>
> dispatch_apply( nbrInterations, queue, ^void(size_t idx)
>
>{
>
>uint64_t tCount = 0;
>
>... do some counting ...
>
>atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );
>
>}
>
> )
>
>
> Currently I am using:
>
>
> var counter: UInt64 = 0
>
> let dsema = DispatchSemaphore(value: 1)
>
> DispatchQueue.concurrentPerform( iterations: nbrInterations )
>
> { ( idx: size_t) -> Void in
>
>
>
>var tCount: UInt64 = 0
>
>... do some counting ...
>
>_ = dsema.wait(timeout: .distantFuture)
>
>counter += tCount;
>
>dsema.signal()
>
> }
>
>
> Is there a better way?
>
>
> Gerriet.
>
>
> ___
>
> swift-users mailing list
>
> swift-users@swift.org
>
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Hooman Mehr via swift-users
I recommend having explicit precondition and reducing repetition like this:

import Foundation

func random(from range: CountableRange) -> Int {

precondition(range.count > 0,
 "The range can't be empty.")

return random(from: CountableClosedRange(range))
}

func random(from range: CountableClosedRange) -> Int {

let lowerBound = range.lowerBound
let upperBound = range.upperBound

precondition(upperBound - lowerBound < Int(UInt32.max),
 "The range \(range) is too wide. It shouldn't be wider than 
\(UInt32.max).")

return lowerBound + Int(arc4random_uniform(UInt32(upperBound - lowerBound + 
1)))
}

let r1 = random(from: 4 ..< 8)
let r2 = random(from: 6 ... 8)

Once we have the new improved Integer protocols 

 in place, you will be able to make it generic to support all integer types. 
(It is possible now, but too messy to be worth doing.)


> On Oct 12, 2016, at 1:23 PM, Adriano Ferreira via swift-users 
> mailto:swift-users@swift.org>> wrote:
> 
> Hi there!
> 
> Ole Begeman offers here  (take 
> a look at the bottom of the page) an interesting consideration about 
> converting between half-open and closed ranges.
> 
> As of now, it seems the way to go is by overloading…
> 
> 
> import Foundation
> 
> func random(from range: Range) -> Int {
> let lowerBound = range.lowerBound
> let upperBound = range.upperBound
> 
> return lowerBound + Int(arc4random_uniform(UInt32(upperBound - 
> lowerBound)))
> }
> 
> func random(from range: ClosedRange) -> Int {
> let lowerBound = range.lowerBound
> let upperBound = range.upperBound
> 
> return lowerBound + Int(arc4random_uniform(UInt32(upperBound - lowerBound 
> + 1)))
> }
> 
> let r1 = random(from: 4 ..< 8)
> let r2 = random(from: 6 ... 8)
> 
> 
> Cheers,
> 
> — A
> 
>> On Oct 12, 2016, at 6:21 AM, Jean-Denis Muys via swift-users 
>> mailto:swift-users@swift.org>> wrote:
>> 
>> Hi,
>> 
>> I defined this:
>> 
>> func random(from r: Range) -> Int {
>> let from = r.lowerBound
>> let to =  r.upperBound
>> 
>> let rnd = arc4random_uniform(UInt32(to-from))
>> return from + Int(rnd)
>> }
>> 
>> so that I can do:
>> 
>> let testRandomValue = random(from: 4..<8)
>> 
>> But this will not let me do:
>> 
>> let otherTestRandomValue = random(from: 4...10)
>> 
>> The error message is a bit cryptic:
>> 
>> “No ‘…’ candidate produce the expected contextual result type ‘Range’”
>> 
>> What is happening is that 4…10 is not a Range, but a ClosedRange.
>> 
>> Of course I can overload my function above to add a version that takes a 
>> ClosedRange.
>> 
>> But this is not very DRY.
>> 
>> What would be a more idiomatic way?
>> 
>> Thanks,
>> 
>> Jean-Denis
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users 
>> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Why are Swift Loops slow?

2016-10-12 Thread Andrew Trick via swift-users

> On Oct 12, 2016, at 9:05 AM, Joe Groff via swift-users 
>  wrote:
> 
> It looks like LLVM does not recognize the overflow traps Swift emits on 
> arithmetic operations as dead code, so that prevents it from completely 
> eliminating the Swift loop.  That's a bug worth fixing in Swift, but unlikely 
> to make a major difference in real, non-dead code. 

The traps aren’t dead if they are reachable. Eventually we will teach LLVM 
about less-strict trap semantics so they can be speculated and reordered. But 
for now we can do that sort of thing in SIL in the most important cases.

-Andy___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Adriano Ferreira via swift-users
Hi there!

Ole Begeman offers here  (take a 
look at the bottom of the page) an interesting consideration about converting 
between half-open and closed ranges.

As of now, it seems the way to go is by overloading…


import Foundation

func random(from range: Range) -> Int {
let lowerBound = range.lowerBound
let upperBound = range.upperBound

return lowerBound + Int(arc4random_uniform(UInt32(upperBound - lowerBound)))
}

func random(from range: ClosedRange) -> Int {
let lowerBound = range.lowerBound
let upperBound = range.upperBound

return lowerBound + Int(arc4random_uniform(UInt32(upperBound - lowerBound + 
1)))
}

let r1 = random(from: 4 ..< 8)
let r2 = random(from: 6 ... 8)


Cheers,

— A

> On Oct 12, 2016, at 6:21 AM, Jean-Denis Muys via swift-users 
>  wrote:
> 
> Hi,
> 
> I defined this:
> 
> func random(from r: Range) -> Int {
> let from = r.lowerBound
> let to =  r.upperBound
> 
> let rnd = arc4random_uniform(UInt32(to-from))
> return from + Int(rnd)
> }
> 
> so that I can do:
> 
> let testRandomValue = random(from: 4..<8)
> 
> But this will not let me do:
> 
> let otherTestRandomValue = random(from: 4...10)
> 
> The error message is a bit cryptic:
> 
> “No ‘…’ candidate produce the expected contextual result type ‘Range’”
> 
> What is happening is that 4…10 is not a Range, but a ClosedRange.
> 
> Of course I can overload my function above to add a version that takes a 
> ClosedRange.
> 
> But this is not very DRY.
> 
> What would be a more idiomatic way?
> 
> Thanks,
> 
> Jean-Denis
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Segfault only when running swift in system-wide directory

2016-10-12 Thread mishal_shah via swift-users
Thanks Lane! 

> On Oct 12, 2016, at 11:11 AM, Lane Schwartz  wrote:
> 
> Done: https://bugs.swift.org/browse/SR-2929 
> 
> 
> On Mon, Oct 10, 2016 at 1:28 PM, mishal_shah  > wrote:
> Hi Lane,
> 
> Can you please file a bug on bugs.swift.org ?
> 
> Thanks,
> Mishal Shah
> > On Oct 9, 2016, at 11:56 AM, Lane Schwartz via swift-users 
> > mailto:swift-users@swift.org>> wrote:
> >
> > Hi,
> >
> > I'm on Ubuntu 16, and just downloaded the latest Swift package. When I run 
> > swift from /opt (installed using sudo) as a normal user, I get a segfault. 
> > When I run it from /opt as root, no problems. When I run from my home dir 
> > as a normal user, things are fine.
> >
> >
> > Run swift (installed in /opt using sudo) as local user - I get a segfault.
> >
> > lanes:~$ /opt/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for 
> > assistance.
> >   1> print("Hello")
> > Segmentation fault (core dumped)
> >
> >
> > Run swift (installed in home dir) as local user - everything is fine.
> >
> > lanes:~$ ~/Downloads/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for 
> > assistance.
> >   1> print("Hello")
> > Hello
> >
> >
> > Run swift (installed in /opt using sudo) as root - everything is fine.
> >
> > root@kulusiq:~# /opt/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for 
> > assistance.
> >   1> print("Hello")
> > Hello
> >
> >
> > Any help would be appreciated.
> >
> > $ uname -a
> > Linux 4.4.0-38-generic #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64 
> > x86_64 x86_64 GNU/Linux
> >
> > Thanks,
> > Lane
> >
> >
> >
> > ___
> > swift-users mailing list
> > swift-users@swift.org 
> > https://lists.swift.org/mailman/listinfo/swift-users 
> > 
> 
> 
> 
> 
> -- 
> When a place gets crowded enough to require ID's, social collapse is not
> far away.  It is time to go elsewhere.  The best thing about space travel
> is that it made it possible to go elsewhere.
> -- R.A. Heinlein, "Time Enough For Love"

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Segfault only when running swift in system-wide directory

2016-10-12 Thread Lane Schwartz via swift-users
Done: https://bugs.swift.org/browse/SR-2929

On Mon, Oct 10, 2016 at 1:28 PM, mishal_shah  wrote:

> Hi Lane,
>
> Can you please file a bug on bugs.swift.org?
>
> Thanks,
> Mishal Shah
> > On Oct 9, 2016, at 11:56 AM, Lane Schwartz via swift-users <
> swift-users@swift.org> wrote:
> >
> > Hi,
> >
> > I'm on Ubuntu 16, and just downloaded the latest Swift package. When I
> run swift from /opt (installed using sudo) as a normal user, I get a
> segfault. When I run it from /opt as root, no problems. When I run from my
> home dir as a normal user, things are fine.
> >
> >
> > Run swift (installed in /opt using sudo) as local user - I get a
> segfault.
> >
> > lanes:~$ /opt/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for
> assistance.
> >   1> print("Hello")
> > Segmentation fault (core dumped)
> >
> >
> > Run swift (installed in home dir) as local user - everything is fine.
> >
> > lanes:~$ ~/Downloads/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for
> assistance.
> >   1> print("Hello")
> > Hello
> >
> >
> > Run swift (installed in /opt using sudo) as root - everything is fine.
> >
> > root@kulusiq:~# /opt/swift-3.0.1-PREVIEW-2-ubuntu16.04/usr/bin/swift
> > Welcome to Swift version 3.0.1 (swift-3.0.1-PREVIEW-2). Type :help for
> assistance.
> >   1> print("Hello")
> > Hello
> >
> >
> > Any help would be appreciated.
> >
> > $ uname -a
> > Linux 4.4.0-38-generic #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64
> x86_64 x86_64 GNU/Linux
> >
> > Thanks,
> > Lane
> >
> >
> >
> > ___
> > swift-users mailing list
> > swift-users@swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
>


-- 
When a place gets crowded enough to require ID's, social collapse is not
far away.  It is time to go elsewhere.  The best thing about space travel
is that it made it possible to go elsewhere.
-- R.A. Heinlein, "Time Enough For Love"
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Why are Swift Loops slow?

2016-10-12 Thread Joe Groff via swift-users

> On Oct 12, 2016, at 2:25 AM, Gerriet M. Denkmann via swift-users 
>  wrote:
> 
> uint64_t nbrBytes = 4e8;
> uint64_t count = 0;
> for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
> {
>   count += byteIndex;
>   if ( ( byteIndex & 0x ) == 0 ) { count += 1.3; }  (AAA) 
> };
> 
> Takes 260 msec.
> 
> Btw.: Without the (AAA) line the whole loop is done in 10 μsec. A really 
> clever compiler!
> And with “count += 1” instead of “count += 1.3” it takes 410 msec. Very 
> strange. 
> But this is beside the point here.
> 
> 
> Now Swift:
> let nbrBytes = 400_000_000
> var count = 0
> for byteIndex in 0 ..< nbrBytes
> {
>   count += byteIndex
>   if ( ( byteIndex & 0x ) == 0 ) {count += Int(1.3);}
> }
> 
> takes 390 msec - about 50 % more.
> 
> Release build with default options.

This is a useless benchmark because the loop does no observable work in either 
language. The C version, if you look at the generated assembly, in fact 
optimizes away to nothing:

~/src/s/swift$ clang ~/butt.c -O3  -S -o -
.section__TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.globl  _main
.p2align4, 0x90
_main:  ## @main
.cfi_startproc
## BB#0:## %entry
pushq   %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq%rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
xorl%eax, %eax
popq%rbp
retq
.cfi_endproc

It looks like LLVM does not recognize the overflow traps Swift emits on 
arithmetic operations as dead code, so that prevents it from completely 
eliminating the Swift loop.  That's a bug worth fixing in Swift, but unlikely 
to make a major difference in real, non-dead code. However, if we make the work 
useful by factoring the loop into a function in both languages, the perf 
difference is unmeasurable. Try comparing:

#include 

__attribute__((noinline))
uint64_t getCount(uint64_t nbrBytes) {
  uint64_t count = 0;
  for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
  {
  count += byteIndex;
  if ( ( byteIndex & 0x ) == 0 ) { count += 1.3; } 
  };
  return count;
}

int main() {
  uint64_t nbrBytes = 4e8;
  return getCount(nbrBytes);
}


with:

import Darwin

@inline(never)
func getCount(nbrBytes: Int) -> Int {
  var count = 0
  for byteIndex in 0 ..< nbrBytes
  {
  count += byteIndex
  if ( ( byteIndex & 0x ) == 0 ) {count += Int(1.3);}
  }
  return count
}

exit(Int32(truncatingBitPattern: getCount(nbrBytes: 400_000_000)))

-Joe___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread David Hart via swift-users
Dash + Alfred :)

> On 12 Oct 2016, at 17:13, Lars-Jørgen Kristiansen via swift-users 
>  wrote:
> 
> I recommend Dash 😏
> 
>> 12. okt. 2016 kl. 17.03 skrev Adrian Zubarev via swift-users 
>> mailto:swift-users@swift.org>>:
>> 
>> You could also fallback to http://swiftdoc.org . ;)
>> 
>> 
>> 
>> 
>> -- 
>> Adrian Zubarev
>> Sent with Airmail
>> 
>> Am 12. Oktober 2016 um 17:01:48, Jon Shier via swift-users 
>> (swift-users@swift.org ) schrieb:
>> 
>>> I’m guessing you only came to Apple’s developer environment with Swift? (If 
>>> not I apologize.) The searchability and discoverability of anything on 
>>> Apple’s dev site is rather low. You learn to live with it and use Google 
>>> whenever you need to find something. (Which turns up the standard library 
>>> docs as the first result if you search for “swift standard library 
>>> documentation”). 
>>> 
>>> 
>>> 
>>> Jon
>>> 
 On Oct 12, 2016, at 7:58 AM, J.E. Schotsman via swift-users 
 mailto:swift-users@swift.org>> wrote:
 
 
> On 12 Oct 2016, at 13:31, Adrian Zubarev  > wrote:
> 
> It’s not hidden.
> 
> https://developer.apple.com/ 
> Develop
> API Reference
> Swift Standard Library
> Profit https://developer.apple.com/reference/swift 
> I see. I still find it 
> strange that it cannot be found on developer.apple.com 
>  with the search function.
 The swift.org  version used to have a nice display of 
 the hierarchical arrangement of protocols. Is that still available 
 anywhere?
 
 Jan E.
 ___
 swift-users mailing list
 swift-users@swift.org 
 https://lists.swift.org/mailman/listinfo/swift-users 
 
>>> 
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-users
>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users 
>> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Counting in Threads

2016-10-12 Thread Philippe Hausler via swift-users
I was under the impression that taking the address was more than a single load 
instruction and would emit a placeholder invalid value: which would make that 
technically unsafe in a threaded context.

Sent from my iPhone

> On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users 
>  wrote:
> 
> I suspect one of the actual compiler people might tell me I shouldn't trust 
> this, but in practice it works:
> --
> import Darwin.C
> 
> public class AtomicInt32 {
>public fileprivate (set) var value : Int32 = 0
> 
>/// Create a new atomic integer with the specified initial value.
>public init(_ value: Int32 = 0) {
>self.value = value
>}
> 
>/// Add one to the value.
>public func increment () {
>OSAtomicIncrement32(&value)
>}
> }
> 
> public func +=(int: AtomicInt32, value: Int32) {
>OSAtomicAdd32(value, &int.value)
> }
> --
> 
> Would also love to know if compiler guarantees I *can* trust this.
> 
> Note that this has to be a class for this to be in any way safe, which means 
> it is also rather inefficient if the use case was having a lot of them.
> 
> - Daniel
> 
>> On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users 
>>  wrote:
>> 
>> 
>> How to translate this to Swift:
>> 
>> __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
>> dispatch_apply( nbrInterations, queue, ^void(size_t idx)
>>{
>>uint64_t tCount = 0;
>>... do some counting ...
>>atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );
>>}
>> )
>> 
>> Currently I am using:
>> 
>> var counter: UInt64 = 0
>> let dsema = DispatchSemaphore(value: 1)  
>> DispatchQueue.concurrentPerform( iterations: nbrInterations )
>> { ( idx: size_t) -> Void in
>>
>>var tCount: UInt64 = 0
>>... do some counting ...
>>_ = dsema.wait(timeout: .distantFuture) 
>>counter += tCount;
>>dsema.signal()  
>> }
>> 
>> Is there a better way?
>> 
>> Gerriet.
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Counting in Threads

2016-10-12 Thread Daniel Dunbar via swift-users
I suspect one of the actual compiler people might tell me I shouldn't trust 
this, but in practice it works:
--
import Darwin.C

public class AtomicInt32 {
public fileprivate (set) var value : Int32 = 0

/// Create a new atomic integer with the specified initial value.
public init(_ value: Int32 = 0) {
self.value = value
}

/// Add one to the value.
public func increment () {
OSAtomicIncrement32(&value)
}
}

public func +=(int: AtomicInt32, value: Int32) {
OSAtomicAdd32(value, &int.value)
}
--

Would also love to know if compiler guarantees I *can* trust this.

Note that this has to be a class for this to be in any way safe, which means it 
is also rather inefficient if the use case was having a lot of them.

 - Daniel

> On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users 
>  wrote:
> 
> 
> How to translate this to Swift:
> 
> __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
> dispatch_apply( nbrInterations, queue, ^void(size_t idx)  
>   {
>   uint64_t tCount = 0;
>   ... do some counting ...
>   atomic_fetch_add_explicit( &counter, tCount, 
> memory_order_relaxed );
>   }
> )
> 
> Currently I am using:
> 
> var counter: UInt64 = 0
> let dsema = DispatchSemaphore(value: 1)  
> DispatchQueue.concurrentPerform( iterations: nbrInterations )
> { ( idx: size_t) -> Void in
>   
>   var tCount: UInt64 = 0
>   ... do some counting ...
>   _ = dsema.wait(timeout: .distantFuture) 
>   counter += tCount;
>   dsema.signal()  
> }
> 
> Is there a better way?
> 
> Gerriet.
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread Lars-Jørgen Kristiansen via swift-users
I recommend Dash 😏

> 12. okt. 2016 kl. 17.03 skrev Adrian Zubarev via swift-users 
> :
> 
> You could also fallback to http://swiftdoc.org . ;)
> 
> 
> 
> 
> -- 
> Adrian Zubarev
> Sent with Airmail
> 
> Am 12. Oktober 2016 um 17:01:48, Jon Shier via swift-users 
> (swift-users@swift.org ) schrieb:
> 
>> I’m guessing you only came to Apple’s developer environment with Swift? (If 
>> not I apologize.) The searchability and discoverability of anything on 
>> Apple’s dev site is rather low. You learn to live with it and use Google 
>> whenever you need to find something. (Which turns up the standard library 
>> docs as the first result if you search for “swift standard library 
>> documentation”). 
>> 
>> 
>> 
>> Jon
>> 
>>> On Oct 12, 2016, at 7:58 AM, J.E. Schotsman via swift-users 
>>> mailto:swift-users@swift.org>> wrote:
>>> 
>>> 
 On 12 Oct 2016, at 13:31, Adrian Zubarev >>> > wrote:
 
 It’s not hidden.
 
 https://developer.apple.com/ 
 Develop
 API Reference
 Swift Standard Library
 Profit https://developer.apple.com/reference/swift 
 I see. I still find it 
 strange that it cannot be found on developer.apple.com 
  with the search function.
>>> The swift.org  version used to have a nice display of 
>>> the hierarchical arrangement of protocols. Is that still available anywhere?
>>> 
>>> Jan E.
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-users
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread Adrian Zubarev via swift-users
You could also fallback to http://swiftdoc.org. ;)



-- 
Adrian Zubarev
Sent with Airmail

Am 12. Oktober 2016 um 17:01:48, Jon Shier via swift-users 
(swift-users@swift.org) schrieb:

I’m guessing you only came to Apple’s developer environment with Swift? (If not 
I apologize.) The searchability and discoverability of anything on Apple’s dev 
site is rather low. You learn to live with it and use Google whenever you need 
to find something. (Which turns up the standard library docs as the first 
result if you search for “swift standard library documentation”). 



Jon

On Oct 12, 2016, at 7:58 AM, J.E. Schotsman via swift-users 
 wrote:


On 12 Oct 2016, at 13:31, Adrian Zubarev  
wrote:

It’s not hidden.

https://developer.apple.com/

Develop
API Reference
Swift Standard Library
Profit https://developer.apple.com/reference/swift
I see. I still find it strange that it cannot be found on developer.apple.com 
with the search function.
The swift.org version used to have a nice display of the hierarchical 
arrangement of protocols. Is that still available anywhere?

Jan E.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread Jon Shier via swift-users
I’m guessing you only came to Apple’s developer environment with Swift? 
(If not I apologize.) The searchability and discoverability of anything on 
Apple’s dev site is rather low. You learn to live with it and use Google 
whenever you need to find something. (Which turns up the standard library docs 
as the first result if you search for “swift standard library documentation”). 



Jon

> On Oct 12, 2016, at 7:58 AM, J.E. Schotsman via swift-users 
>  wrote:
> 
> 
>> On 12 Oct 2016, at 13:31, Adrian Zubarev > > wrote:
>> 
>> It’s not hidden.
>> 
>> https://developer.apple.com/ 
>> Develop
>> API Reference
>> Swift Standard Library
>> Profit https://developer.apple.com/reference/swift 
>> I see. I still find it strange 
>> that it cannot be found on developer.apple.com 
>>  with the search function.
> The swift.org  version used to have a nice display of the 
> hierarchical arrangement of protocols. Is that still available anywhere?
> 
> Jan E.
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread David Hart via swift-users
I’ve been bitten by that quite a few times. I’m not a fan of the new 
distinction between Range and ClosedRange. I understand the reasoning behind 
them, but the new model is creating more problems for me than the it solves.

David.

> On 12 Oct 2016, at 12:21, Jean-Denis Muys via swift-users 
>  wrote:
> 
> Hi,
> 
> I defined this:
> 
> func random(from r: Range) -> Int {
> let from = r.lowerBound
> let to =  r.upperBound
> 
> let rnd = arc4random_uniform(UInt32(to-from))
> return from + Int(rnd)
> }
> 
> so that I can do:
> 
> let testRandomValue = random(from: 4..<8)
> 
> But this will not let me do:
> 
> let otherTestRandomValue = random(from: 4...10)
> 
> The error message is a bit cryptic:
> 
> “No ‘…’ candidate produce the expected contextual result type ‘Range’”
> 
> What is happening is that 4…10 is not a Range, but a ClosedRange.
> 
> Of course I can overload my function above to add a version that takes a 
> ClosedRange.
> 
> But this is not very DRY.
> 
> What would be a more idiomatic way?
> 
> Thanks,
> 
> Jean-Denis
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread J.E. Schotsman via swift-users

> On 12 Oct 2016, at 13:31, Adrian Zubarev  
> wrote:
> 
> It’s not hidden.
> 
> https://developer.apple.com/ 
> Develop
> API Reference
> Swift Standard Library
> Profit https://developer.apple.com/reference/swift 
> I see. I still find it strange 
> that it cannot be found on developer.apple.com  
> with the search function.
The swift.org version used to have a nice display of the hierarchical 
arrangement of protocols. Is that still available anywhere?

Jan E.___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] find std lib doc

2016-10-12 Thread Adrian Zubarev via swift-users
It’s not hidden.

https://developer.apple.com/

Develop
API Reference
Swift Standard Library
Profit https://developer.apple.com/reference/swift


-- 
Adrian Zubarev
Sent with Airmail

Am 12. Oktober 2016 um 11:55:17, J.E. Schotsman via swift-users 
(swift-users@swift.org) schrieb:

Is it just me, or is the Standard library document not very discoverable?

Try searching developer.apple.com for "(Swift) standard library". No hit!
Can’t find it on swift.org either (which isn’t even searchable).

In case anybody else needs it, Mateusz Malczak gave me the link:
https://developer.apple.com/reference/swift

Jan E.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] How to be DRY on ranges and closed ranges?

2016-10-12 Thread Jean-Denis Muys via swift-users
Hi,

I defined this:

func random(from r: Range) -> Int {
let from = r.lowerBound
let to =  r.upperBound

let rnd = arc4random_uniform(UInt32(to-from))
return from + Int(rnd)
}

so that I can do:

let testRandomValue = random(from: 4..<8)

But this will not let me do:

let otherTestRandomValue = random(from: 4...10)

The error message is a bit cryptic:

“No ‘…’ candidate produce the expected contextual result type ‘Range’”

What is happening is that 4…10 is not a Range, but a ClosedRange.

Of course I can overload my function above to add a version that takes a 
ClosedRange.

But this is not very DRY.

What would be a more idiomatic way?

Thanks,

Jean-Denis

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Why are Swift Loops slow?

2016-10-12 Thread Greg Parker via swift-users

> On Oct 12, 2016, at 2:25 AM, Gerriet M. Denkmann via swift-users 
>  wrote:
> 
> uint64_t nbrBytes = 4e8;
> uint64_t count = 0;
> for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
> {
>   count += byteIndex;
>   if ( ( byteIndex & 0x ) == 0 ) { count += 1.3; }  (AAA) 
> };
> 
> Takes 260 msec.
> 
> Btw.: Without the (AAA) line the whole loop is done in 10 μsec. A really 
> clever compiler!
> And with “count += 1” instead of “count += 1.3” it takes 410 msec. Very 
> strange. 
> But this is beside the point here.
> 
> 
> Now Swift:
> let nbrBytes = 400_000_000
> var count = 0
> for byteIndex in 0 ..< nbrBytes
> {
>   count += byteIndex
>   if ( ( byteIndex & 0x ) == 0 ) {count += Int(1.3);}
> }
> 
> takes 390 msec - about 50 % more.
> 
> Release build with default options.

You'll need to read the generated assembly code if you want to analyze 
performance of this sort of small arithmetic loop. Performance of this kind of 
code can be greatly affected by small optimization changes.

clang's `count +=1` code is vectorized, and the `count += 1.3` code is not 
vectorized. For whatever reason that vectorization is unsuccessful and the 
vectorized loop runs slower (at least on your machine and on my machine). 
Optimization is hard.

The Swift loop runs slower because Swift performs arithmetic overflow checks 
that C does not, and in this case swiftc was unable to optimize them all away.

If you use &+ instead of +, or compile with -Ounchecked, then Swift won't 
perform the overflow checks. Unfortunately in this case you then get the same 
slower vectorized code from swiftc as you did from clang's `count += 1` case; 
presumably both clang and swiftc get this pessimization from LLVM. I couldn't 
find a way to disable LLVM vectorization from swiftc.


-- 
Greg Parker gpar...@apple.com  Runtime 
Wrangler


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] find std lib doc

2016-10-12 Thread J.E. Schotsman via swift-users
Is it just me, or is the Standard library document not very discoverable?

Try searching developer.apple.com for "(Swift) standard library". No hit!
Can’t find it on swift.org either (which isn’t even searchable).

In case anybody else needs it, Mateusz Malczak gave me the link:
https://developer.apple.com/reference/swift 


Jan E.___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Why are Swift Loops slow?

2016-10-12 Thread Gerriet M. Denkmann via swift-users
uint64_t nbrBytes = 4e8;
uint64_t count = 0;
for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
{
count += byteIndex;
if ( ( byteIndex & 0x ) == 0 ) { count += 1.3; }  (AAA) 
};

Takes 260 msec.

Btw.: Without the (AAA) line the whole loop is done in 10 μsec. A really clever 
compiler!
And with “count += 1” instead of “count += 1.3” it takes 410 msec. Very 
strange. 
But this is beside the point here.


Now Swift:
let nbrBytes = 400_000_000
var count = 0
for byteIndex in 0 ..< nbrBytes
{
count += byteIndex
if ( ( byteIndex & 0x ) == 0 ) {count += Int(1.3);}
}

takes 390 msec - about 50 % more.

Release build with default options.

Gerriet.

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Counting in Threads

2016-10-12 Thread Gerriet M. Denkmann via swift-users

How to translate this to Swift:

__block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);
dispatch_apply( nbrInterations, queue, ^void(size_t idx)
{
uint64_t tCount = 0;
... do some counting ...
atomic_fetch_add_explicit( &counter, tCount, 
memory_order_relaxed );
}
)

Currently I am using:

var counter: UInt64 = 0
let dsema = DispatchSemaphore(value: 1)  
DispatchQueue.concurrentPerform( iterations: nbrInterations )
{ ( idx: size_t) -> Void in

var tCount: UInt64 = 0
... do some counting ...
_ = dsema.wait(timeout: .distantFuture) 
counter += tCount;
dsema.signal()  
}

Is there a better way?

Gerriet.

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users