138 points · by mooreds · 4y ago · 76 comments
The origin story is cool as heck, too. As discussed many many times on HN: http://pacifict.com/Story/
It’s been a while, but I wrote a few different “slow start” functions for a load balancer and used Graphing Calculator to not only illustrate the behavior, but also show which parameters were tunable.
I hope he decides to also open source the C++ version. It would be interesting to be able to see both versions, and to compare them.
Perhaps one of the greatest benefits of open sourcing both versions, that I can see, is that others could then look at the two versions of Ron’s code for inspiration on how to go about porting their own legacy applications from C++ to Swift.
Aside from that, it also provides historical context that future programmers and future programming language creators could learn from. Both in terms of good things and any of the lesser good sides.
The author is being kind. Once outside the prescribed path, you’re in deep trouble. And that’s the thing with declarative code. There must be a great escape hatch. The framework can’t foresee all possible use cases.
Apple certainly has done a much better job of picking a development strategy and iterating on it than Microsoft has over the past couple of decades.
Apple could really use some good competition, because Windows and desktop Linux aren’t enough.
Apple is very much in the camp of maybe tolerating third parties, but only so long as they only ever do stuff in the bounds of what Apple wants them to do
> There must be a great escape hatch.
or you wait for the next os release (which really hurts adoption and dirties code with lots of workarounds)This is impressive.
When people make 'size' comparisons, I sometimes wonder if they'd best be done using lzip or something on the two code bases -- because the compressors are trying to remove redundancy in coding, and if one uses lots of whitespace in C++ to enhance readability, this should not punish C++ in the size department.
You generally want some 'system' ('language' seems too restrictive) to enable one to capture the algorithms rapidly and accurately in some notation (again, 'language' seems not quite the right word), and to as quickly as possible convert said notation into working binary (compiler, interpreter, JIT, etc all fair game) that satisfies the user need.
I claim (wrongly?) that these days, we generally only need a decent 'glue' notation that allows us to mash together existing libraries/classes/etc to produce the final product. Some of this glue would be sort-of templates, lispish macros, some would just be e.g. an FFT routine. But only by burying complexity in well-tested, lower-level chunks will we be able to build big systems that don't collapse.
Sorry, enough soapbox. I guess I'm suggesting that it's interesting to compare Swift to C++. It sort of begs the question: what is 'the best' way to get some functionality (e.g. this cool calc app) written?
https://programming-language-benchmarks.vercel.app/swift-vs-...
It's really hard not to like reducing code size to 30% of C++ for long term maintenance IMHO.
I spent 15 years doing C++ and read so many books but always felt the language would get in the way. When switching to the Swift stuff just worked the way it should. No need to read tons of books to find clever tricks to get around inherent limitations of C++.
C++ was good for it’s time but it is strange that people think one cannot radically improve on what C++ is with newer languages. It is tied down with backwards compatibility with a 50 year old language.
That had lots of advantages, but downsides has caught up with advantages.
I suspect [Ancient C/C++] weighed down with tech debt and utilising a plethora of old-code frameworks to Swift is what let him save 30%. If he did the port to Modern C++ and modern libs, he would have likely saved 50%.
[[nodiscard]] constexpr int Add(const int& a, const int& b) const noexcept { return a + b; }
Modern C++ does help with most type specification, looping over containers, writing constructors and making value classes. It's definitely not all bad, but there's still a lot of boilerplate to C++.
I get that people stick to C++ because it is too costly to port. But it doesn’t make a lot of sense to stick to C++ if you got the option to use Swift, Rust or Go.
Swift avoids null pointers, undefined static initializations and de-initialization order, virtual call disasters when calling a virtual from a constructor. Protocol oriented programming solves a lot of problems you tend to run into in C++ when doing object-oriented programming.
Template specification used to need a lot of decoration but with auto most of that is gone.
For example, a few areas where Swift reduces boilerplate and the syntax it provides to do so:
- a complete null/optional story: first class optional types (T? = Option<T>), null chaining (obj?.prop), and null coalescing (T ?? "default")
- error handling: fn() throws Err (= fn() -> Result<T,Err>), try, try?, try!, do/catch (out-of-band error handling), w/o stack unwinding, see nulls
- and struct/enum lifecycle management: constructors, getter/setter props, defaults, named functions, named tuples, etc. (this list is loong)
Individually each feature is small and could possibly be argued as bloat or overhead since you can accomplish the same with just regular functions and Rust-like code and abstractions, but combined they make the language feel more expressive (and more fun IMO) than would otherwise be possible and often without sacrificing performance. They also serve to reduce cognitive overhead in the same way using for loops is nicer than desugaring to while loops. This is nothing to say of the syntax itself, and a bunch of other small features; Swift really is a case where the whole is greater than the sum of of its parts.I would describe dealing with those issues as "boilerplate" because it's just a lot of code you should not have to write.
Swift is so much more comfortable and fun.
(btw I still love C++ and cross platform support completely flips the script)
I'm not sure we're thinking of the same standard library if you would describe it as thin...[0]
[0]: https://docs.microsoft.com/en-us/cpp/standard-library/cpp-st...
I suspect he just wanted to learn Swift and that's a good reason enough to make this rewrite, as he admits in the final paragraphs of the article: "I’ve enjoyed learning Swift and am much happier with the state of the code now"
C++:
int first[] = {1,2,3,4};
int second[] = {5,6,3,4};
std::vector<int> v(8);
std::vector<int>::iterator it;
std::sort (first,first + 4); // 1 2 3 4
std::sort (second,second + 4); // 3 4 5 6
it = std::set_union (first, first + 4, second, second + 4, v.begin());
v.resize(it - v.begin());
for(it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
Swift: let a = Set([1,2,3,4])
let b = Set([5,6,3,4])
let c = a.union(b)
print(c) //prints "[1, 2, 3, 5, 6, 4]" #include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std::ranges;
int main()
{
std::vector a = {1,2,3,4};
std::vector b = {5,6,3,4};
sort(a);
sort(b);
decltype(a) c;
set_union (a, b, back_inserter(c));
copy(c, std::ostream_iterator<int>(std::cout, "\n"));
}
https://gcc.godbolt.org/z/44Ksf1hK8Note the value type deduction when declaring the vectors, the use of back_inserter to avoid having to resize the result (c) vector, the use of std::ranges to do away with all the begin + end crapola, and the way ranges and iterators are inter-mixed.
C++ is always going to be a bit more verbose in trivial cases, but it's not that bad, and exposes a lot more flexibility in complex ones.
std::set a{1,2,3};
std::set b{5,6,3,4};
std::set c{a};
c.insert(b.begin, b.end());
for(auto x: c)
std::cout<<c<<' c;
std::cout << '\n';Another question, have you tried using Accelerate framework to solve performance bottlenecks (or save yourself from having to write your own calc code)?
In principle, I could get rid of reference counting overhead by using value types or immutable data. I couldn't see a simple path to doing that without re-architecting everything (with no guarantee that the end result would not just have different performance issues.) For the moment, I'm awaiting compiler improvements before re-evaluating the tradeoffs. There is certainly room for the compiler to reason better on eliding retain/release. https://github.com/apple/swift/issues/58549
Yes, the code does use Accelerate where applicable. That is one component of the numeric evaluation. It addresses the lowest level of things like evaluate the sin function on every array element, or multiply there arrays element wise. Performance tuning is a game of whack-a-mole. There's always another bottleneck somewhere.
Can you say more about the parse tree impact on performance?
Is the expression left in a tree form and effectively "interpreted" from the AST for all points? Is that the critical path?
My basic question would be: why not go back to flex/bison/yacc/whatever via C-FFI? (But I think it would still be bad, since you'll want to get to a Swift data structure for your ops and those will still have the Arc issues)
https://github.com/apple/swift/tree/main/docs/CppInteroperab...
https://forums.swift.org/t/swift-and-c-interoperability-work...
(I read the article hours ago and didn't notice you'd posted. Btw, I think we would have gotten better conversation if you had said "Hey, this is me! AMA" as a top-level comment)
*Objective-C++
To be honest, swift feels more like a bad parody on Scala.