| I think another way to view this is, while Swift could be performant, but idiomatic code in practically any other language may be utterly wrong in Swift. For instance recently this nested data structure "issue" was brought up (again) in the Swift community:
https://mjtsai.com/blog/2019/11/02/efficiently-mutating-nest... If you had a nested set in a dictionary: ```
var many_sets: [String:Set<Int>] = ...
let tmp_set = many_sets["a"]
tmp_set.insert(1)
many_sets["a"] = tmp_set
``` vs. ```
var many_sets: [String:Set<Int>] = ...
many_sets["a"].insert(1)
``` The performance is entirely different (e.g. you are making a copy of the Set in the first example). Prior to Swift 5, you would have had to potentially remove the set from the dictionary in order to make sure there were no unintentional copies. While the examples are contrived to some degree, I think at least a few new Swift programmers would lookup something in a dictionary, pass the value into a function thinking it's a reference, and then when they realize it isn't being changed in the dictionary, set the value in the dictionary after the function returns like: ```
var many_sets: [String:Set<Int>] = ...
let changed_set = process(set: many_sets["a"])
many_sets["a"] = changed_set
``` It is "easy" to understand what is happening when you know Swift's collections are value types and about copy on write and value vs reference semantics, but it is also an easy performance issue. Furthermore, when web framework benchmarks like:
https://www.techempower.com/benchmarks/#section=data-r18&hw=... show Java's Netty vs. Swift NIO (which is based on the architecture of Netty), I think that it indicates that you cannot just port code and expect anywhere near the same performance in Swift. |
I really wish Swift moved a notch towards C++ in some areas especially where the designer of the type can define the usage in very precise terms. Is it copyable? Is this method abstract? Maybe also struct deinit, etc etc.