Performance critical code in Swift


(Игорь Никитин) #1

Hello!
Is it possible for Swift to be as fast as C when writing performance critical code? Of course if using C Standard Library for instead of Foundation (and so on) and getting rid of dynamic dispatch and reference types.
Or I need just to use C?


(Daniel Dunbar) #2

Yes, it is possible. Exactly how much use of Unsafe style idioms and other performance-focused "workarounds" it requires depends a lot on the code in question. Can you say more about your problem area?

- Daniel

···

On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users <swift-users@swift.org> wrote:

Hello!
Is it possible for Swift to be as fast as C when writing performance critical code? Of course if using C Standard Library for instead of Foundation (and so on) and getting rid of dynamic dispatch and reference types.
Or I need just to use C?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jens Persson) #3

You could write some examples in both C and Swift in order to gain
experience in how to write your Swift code so that it will (probably) run
as fast as (or faster than) your corresponding C code.

I've done this for a number of different performance critical things and it
is often possible to get the Swift version as fast as the C version.

(If you find something that is not possible (or unnecessarily cumbersome)
to write as fast in Swift as in C, you could probably file a
bug/improvement on bugs.swift.org. I did this some time ago when I noticed
that Swift's optimizer missed an opportunity to unroll loops in a certain
situation and it turned out that a fix/optimizer-improvement was already on
its way.)

You must of course profile/microbenchmark your code in some meaningful way,
setting all relevant compiler flags for both C and Swift, preventing dead
code elimination, measuring average or perhaps median times of lots of
tests, making sure what should and shouldn't be statically knowable, etc.

In Swift you probably want -O -whole-module-optimization, and (rarely) you
might want/need to disable safety checks.

/Jens

···

On Sat, Oct 1, 2016 at 10:33 PM, Daniel Dunbar via swift-users < swift-users@swift.org> wrote:

Yes, it is possible. Exactly how much use of Unsafe style idioms and other
performance-focused "workarounds" it requires depends a lot on the code in
question. Can you say more about your problem area?

- Daniel

> On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users < > swift-users@swift.org> wrote:
>
> Hello!
> Is it possible for Swift to be as fast as C when writing performance
critical code? Of course if using C Standard Library for instead of
Foundation (and so on) and getting rid of dynamic dispatch and reference
types.
> Or I need just to use C?
> _______________________________________________
> 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


(Игорь Никитин) #4

I need to write a specialized data storage (database) for some types that are never changes. This struct can be used as an example:

struct User {
    let id: Int32
    let name: UnsafePointer<UInt8>
    let type: Int32
    let location: UnsafePointer<UInt8>
}

SQLite is super slow. I make few millions inserts and sqlite_step is the problem here (this function takes 15 seconds for all job).
So I need to save to the disk few million instances of this struct as fast as possible (this is why I need a custom db)
For such of tasks C (or maybe C++) is a good choice. But how can Swift do this as fast as C?
Of course I need to use low level C I/O api, but there are another things that I need to know?

···

1 окт. 2016 г., в 23:33, Daniel Dunbar <daniel_dunbar@apple.com> написал(а):

Yes, it is possible. Exactly how much use of Unsafe style idioms and other performance-focused "workarounds" it requires depends a lot on the code in question. Can you say more about your problem area?

- Daniel

On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users <swift-users@swift.org> wrote:

Hello!
Is it possible for Swift to be as fast as C when writing performance critical code? Of course if using C Standard Library for instead of Foundation (and so on) and getting rid of dynamic dispatch and reference types.
Or I need just to use C?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Игорь Никитин) #5

I got it. Thanks for your tips!

···

2 окт. 2016 г., в 0:13, Jens Persson <jens@bitcycle.com> написал(а):

You could write some examples in both C and Swift in order to gain experience in how to write your Swift code so that it will (probably) run as fast as (or faster than) your corresponding C code.

I've done this for a number of different performance critical things and it is often possible to get the Swift version as fast as the C version.

(If you find something that is not possible (or unnecessarily cumbersome) to write as fast in Swift as in C, you could probably file a bug/improvement on bugs.swift.org. I did this some time ago when I noticed that Swift's optimizer missed an opportunity to unroll loops in a certain situation and it turned out that a fix/optimizer-improvement was already on its way.)

You must of course profile/microbenchmark your code in some meaningful way, setting all relevant compiler flags for both C and Swift, preventing dead code elimination, measuring average or perhaps median times of lots of tests, making sure what should and shouldn't be statically knowable, etc.

In Swift you probably want -O -whole-module-optimization, and (rarely) you might want/need to disable safety checks.

/Jens

On Sat, Oct 1, 2016 at 10:33 PM, Daniel Dunbar via swift-users <swift-users@swift.org> wrote:
Yes, it is possible. Exactly how much use of Unsafe style idioms and other performance-focused "workarounds" it requires depends a lot on the code in question. Can you say more about your problem area?

- Daniel

> On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users <swift-users@swift.org> wrote:
>
> Hello!
> Is it possible for Swift to be as fast as C when writing performance critical code? Of course if using C Standard Library for instead of Foundation (and so on) and getting rid of dynamic dispatch and reference types.
> Or I need just to use C?
> _______________________________________________
> 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


(Tino) #6

My general advice for performance critical code (especially when it's about stuff like serialization and parsing):
If you just want a working solution without spending much time, stick with C.
Swift can be fast, but it's harder to write slow code in C.

If, on the other hand, coding time is no big concern:
Use Swift, try to get it as fast as possible (can take time) — and if this isn't enough, switch to C (which might end up to be to slow as well :wink:

- Tino


(Jens Persson) #7

There's no reason why writing data to disk should be slower in Swift than
in C/C++.

(For the name and location properties, I assume you want to save Strings, a
sequence of characters or something rather than just the value of the
UnsafePointer<UInt8>.)

···

On Sat, Oct 1, 2016 at 11:22 PM, Игорь Никитин <devnikor@icloud.com> wrote:

I got it. Thanks for your tips!

2 окт. 2016 г., в 0:13, Jens Persson <jens@bitcycle.com> написал(а):

You could write some examples in both C and Swift in order to gain
experience in how to write your Swift code so that it will (probably) run
as fast as (or faster than) your corresponding C code.

I've done this for a number of different performance critical things and
it is often possible to get the Swift version as fast as the C version.

(If you find something that is not possible (or unnecessarily cumbersome)
to write as fast in Swift as in C, you could probably file a
bug/improvement on bugs.swift.org. I did this some time ago when I
noticed that Swift's optimizer missed an opportunity to unroll loops in a
certain situation and it turned out that a fix/optimizer-improvement was
already on its way.)

You must of course profile/microbenchmark your code in some meaningful
way, setting all relevant compiler flags for both C and Swift, preventing
dead code elimination, measuring average or perhaps median times of lots of
tests, making sure what should and shouldn't be statically knowable, etc.

In Swift you probably want -O -whole-module-optimization, and (rarely) you
might want/need to disable safety checks.

/Jens

On Sat, Oct 1, 2016 at 10:33 PM, Daniel Dunbar via swift-users < > swift-users@swift.org> wrote:

Yes, it is possible. Exactly how much use of Unsafe style idioms and
other performance-focused "workarounds" it requires depends a lot on the
code in question. Can you say more about your problem area?

- Daniel

> On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users < >> swift-users@swift.org> wrote:
>
> Hello!
> Is it possible for Swift to be as fast as C when writing performance
critical code? Of course if using C Standard Library for instead of
Foundation (and so on) and getting rid of dynamic dispatch and reference
types.
> Or I need just to use C?
> _______________________________________________
> 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


(Игорь Никитин) #8

It's isn't just about storing, but also parsing (I parse big json with yajl, the C json parser) and processing that data.
And right, name and location is strings

···

2 окт. 2016 г., в 0:24, Jens Persson <jens@bitcycle.com> написал(а):

There's no reason why writing data to disk should be slower in Swift than in C/C++.

(For the name and location properties, I assume you want to save Strings, a sequence of characters or something rather than just the value of the UnsafePointer<UInt8>.)

On Sat, Oct 1, 2016 at 11:22 PM, Игорь Никитин <devnikor@icloud.com> wrote:
I got it. Thanks for your tips!

2 окт. 2016 г., в 0:13, Jens Persson <jens@bitcycle.com> написал(а):

You could write some examples in both C and Swift in order to gain experience in how to write your Swift code so that it will (probably) run as fast as (or faster than) your corresponding C code.

I've done this for a number of different performance critical things and it is often possible to get the Swift version as fast as the C version.

(If you find something that is not possible (or unnecessarily cumbersome) to write as fast in Swift as in C, you could probably file a bug/improvement on bugs.swift.org. I did this some time ago when I noticed that Swift's optimizer missed an opportunity to unroll loops in a certain situation and it turned out that a fix/optimizer-improvement was already on its way.)

You must of course profile/microbenchmark your code in some meaningful way, setting all relevant compiler flags for both C and Swift, preventing dead code elimination, measuring average or perhaps median times of lots of tests, making sure what should and shouldn't be statically knowable, etc.

In Swift you probably want -O -whole-module-optimization, and (rarely) you might want/need to disable safety checks.

/Jens

On Sat, Oct 1, 2016 at 10:33 PM, Daniel Dunbar via swift-users <swift-users@swift.org> wrote:
Yes, it is possible. Exactly how much use of Unsafe style idioms and other performance-focused "workarounds" it requires depends a lot on the code in question. Can you say more about your problem area?

- Daniel

> On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users <swift-users@swift.org> wrote:
>
> Hello!
> Is it possible for Swift to be as fast as C when writing performance critical code? Of course if using C Standard Library for instead of Foundation (and so on) and getting rid of dynamic dispatch and reference types.
> Or I need just to use C?
> _______________________________________________
> 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


(Maury Markowitz) #9

All things considered, the choice of language *should* have minor impact on the outcome here - IO should be orders of magnitude slower than the processing. Of course, we all know examples where this is not the case, especially when you combine it with libraries like Cocoa that may or may not be very well optimized for IO.

That said, it seems like your data is simple enough that I'd suggest "just do it", and make a program that writes and then reads a million rows to a text file using Cocoa's primitives. Pure C with prinf/scanf will almost certainly be faster, but I'm also curious to know how much.

On a broader note, I have yet to see a true modern replacement for SQLite on the embedded side. There are any number of lightweight document stores, but they either have performance worse than SQLite, or are not really suitable for embedded use. I've seen a number of online solutions, and perhaps that is the right way to go these days. Anyone here used an online document store for largish data in Swift?

···

On Oct 1, 2016, at 4:50 PM, Игорь Никитин via swift-users <swift-users@swift.org> wrote:

For such of tasks C (or maybe C++) is a good choice. But how can Swift do this as fast as C?
Of course I need to use low level C I/O api, but there are another things that I need to know?


(Игорь Никитин) #10

Thanks for advices!
I’m switches to C and will write data to file instead of sqlite
It will give me 5x-10x performance growth for writing
But it will also increase the time for reading, I think (I've not measured it)

I’m also looked for some sqlite alternative, but didn’t found
Realm is fast for reading, but not for writing
And it gives some memory overhead


(Marco S Hyman) #11

On a broader note, I have yet to see a true modern replacement for SQLite on the embedded side. There are any number of lightweight document stores, but they either have performance worse than SQLite, or are not really suitable for embedded use.

Realm ???

It’s faster than sqlite and easy to use. At least I found it easy in a simple swift test app I wrote a while back. Haven’t played with it since the switch to swift 3.0.


(David Hart) #12

That fact that Realm has an ORM layer, and that batch inserts are noticeably slower than in SQLite makes it less performant in certain scenarios. The fact that is does not support JOIN queries also causes issues in other performance scenarios.

···

On 2 Oct 2016, at 20:26, Marco S Hyman via swift-users <swift-users@swift.org> wrote:

On a broader note, I have yet to see a true modern replacement for SQLite on the embedded side. There are any number of lightweight document stores, but they either have performance worse than SQLite, or are not really suitable for embedded use.

Realm ???

It’s faster than sqlite and easy to use. At least I found it easy in a simple swift test app I wrote a while back. Haven’t played with it since the switch to swift 3.0.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users