Starting a tech startup with C++
medium.com
medium.com
At my job, we are rewriting some pretty terrible vb5 code. It's easy to just WTF a lot, and complain that there are truly insane things inside of it. The flipside of that, is that this code was quickly built and quickly allowed for a revenue stream.
The author mentioned that thrown away python code provides no value, but I'm not convinced of that statement. The development time could have been significantly shortened, allowing for a revenue stream while the site was rebuilt in C++. While this may seem like wasted development, it could have created incoming money at three months rather than eight months, helping keep the lights on. Further, there are conceivably problems that would come about during development that would impact their design, which they would have to figure out. Solving these problems are irrespective of the language implementation, and using a dynamic but slower language could help them nail down theoretical issues in algorithms, api, and infrastructure, before coding in the c++ system.
A counter point to this though, is that often, there is no budget for a rewrite. I'm wondering if they would be constrained by the prototype becomes long lasting production like in our case.
I think another misconception is that you need pointers at all. Smart pointers are still pointers. Its better to use value semantic all the way when you can. And you usually can. Why place something on the heap if it can live on the stack?
>Well said! Dynamic memory allocation is a really big performance hit
Unless its such a large huge object that stackoverflow is a possibility I cant imagine why would one do that.
So, if you want to allocate N objects, where N is not known at compile time, (I think) you have to use heap allocation anyway. Normally I just use vector<>, and call reserve() if I feel like extra performance-y. Sure, it's much slower than C99-style variable length arrays, but 99% of the time it's still fast enough.
More importantly, it plays nice with other C++ functionalities. For example, zero-copy construction using emplace_back() inside a for loop. (And if you throw an exception in the middle you're guaranteed that destructors are called exactly for those that are already constructed.)
And, yeah its disappointing C99 arrays did not make it to C++, one can use alloca to live a little dangerously, better to wrap it so that it allocates on the heap only if more than a certain size has been asked for. Too bad there is no portable way to find out how much stack space does the current process still have. There should have been a system call for that.
VLAs are not part of the standard but GCC supports them. Clang however does not, at least not without a compiler flag I think.
To be sure, though, being able to make proper value types is a strength of C++. Also we should point out most non-trivial value types make extensive (and necessary!) use of the heap under the hood (eg string, vector)
For instance this does not do any expensive copies:
std::string getStr() { auto huge_str = getDatabaseDump(); return huge_str; }
void readStr(const std::string& str ){ //read str.. }
int main() { auto str = getStr(); readStr(str); }
We as users of that library type should not additionally put that object on the heap though, if we dont have too. Its simply unnecessary.
Its makes for one extra unnecessary pointer indirection, it adds unnecessary reference counting if using smart pointers, or unnecessary manual memory management if we use raw pointers. It complicates the interface for our functions if we have to wrap types in smart pointers.
As for c++ references. Yes that's true. As I understand it they are implemented as pointers internally by compilers. But in important ways they behave more like values, rather than pointers. As in if you copy or assign to them, they copy or assign the value (not the pointer). If you take their address they give the address of the value (not the pointer). Because of this there are not as many pitfalls to using them as there is with pointers. I see them mostly just as aliases for values.
In the example above I could have passed the string by value (instead of by reference) to the function. It would still not have done a expensive copy, because of copy elision optimisation. I probably should have done that. It looks a bit funny, and takes some getting used too.
Relevant: https://web.archive.org/web/20140205194657/http://cpp-next.c...
The problem with copy elision is that it's an optional compiler optimization and you have to check if the compiler applies it to you particular piece of code. That doesn't scale for large code bases (nor for small ones imho).
C#/.NET doesn't get a lot of love from the startup community, but it's mature, stable, fast, full of advanced features, and has good library support.
Java and other JVM languages have similar advantages and better compatibility with Unix-based OSes. Java itself is a little long in the tooth, but there is the option of alternate JVM languages.
I'd probably be most tempted to check out Go if I was working on something that absolutely had to wring the best possible performance out of my hardware. I don't honestly know that much about it, but it has a reputation for getting you most of the performance of C++ without the complexity.
But I will say that if the author has lots of experience in C++ and comfort with the ecosystem, and not much in any of those other languages, then by all means go with C++. Getting your product out there is more important than getting the perfect language.
Go isn't billed as a language that is designed to do that. It's faster than Ruby and Python, for sure, but it made many runtime performance sacrifices in the name of ease of use and compilation speed.
I wasn't just referring to the GC.
If you're starting from scratch, it is important to strongly consider which language and platform best suits your needs. Anecdotally, a lot of companies have found success using newer languages because they tend to attract a better proportion of talented people.
Such as?
So, if it was apples to apples, I'd probably cite Ada, Eiffel, Modula-3, or Component Pascal as the closest to C++ in terms of performance and key features while being safer and more readable. Each has had years of work, support from commercial sector (except Modula-3 now), and programs tend to work more than break after a compile.
That said, your Rust work is exciting and I hope it gets in that same category. It's just so new and evolving that its not in C++'s class in terms of risk or predictability. Not yet.
Your points are all legit. Unfortunately, the only way to get an old language is to start with a new one, and then let time pass.
And with Python you can reason that if code parses it isn't going to have undefined behavior (for example, segfaults randomly thousands of lines away from the actual problem due to heap corruption, or exploitable use-after-free vulnerabilities).
Type safety only has the benefits you describe if the language is actually type safe.
If you seriously "refuse to review Python code" because you feel you need to visually check indentation, I think you might want to take a step back and evaluate your methods.
Atomic reference counting does not have minuscule costs. See, for example, http://www.hboehm.info/gc/nonmoving/html/slide_11.html
In particular, note that if you use shared_ptr everywhere you will be end up with much slower code than you would have if you had a good GC. In other words, you end up slower than Java, with less safety and more verbosity.
Browser engines have used custom non-thread-safe reference counted pointers where possible extensively for this reason.
Furthermore, I always have to say it: shared_ptr is not memory safe.
I think there's a really big part of this case study that isn't mentioned (perhaps because there is currently no data): bringing on more engineers. It really seems as if the number of C++ developers ready to be a part of the startup ecosystem is small. That is the impression that sites like HN leaves me with at least.
In reality, you can only choose programming languages by balancing all the factors in question, and C++ has plenty of disadvantages for databases: it's unsafe, it's difficult to learn, compile times are poor, there's no package manager, etc.
(Honestly, I'd rather call Cargo a "project manager" , because it does far more than manage packages -- it builds your project and its dependencies, too. Having a completely standard way to build applications and libraries is an enormous win over C++. The discovery and management of a project's packaged dependencies is of secondary importance to me.)
[1]: http://docs.basho.com/riak/latest/dev/references/http/ [2]: https://www.rethinkdb.com/docs/administration-tools/
In any case, you now seem to be primarily interested in arguing that database programmers are smarter than all other programmers, which, needless to say, is about the most uninteresting conversation we could possibly be having.
Perl 6's gradual type system seems interesting and one plus of Go is that it brought some well-deserved attention to concise static typing.
Any other good places to start learning about C++ 11/14 for complete beginners with JS and other programming languages?
I'm very much interested to create proof-of-concept for high performance SaaS services and play with C++ as it might be useful to build Node.js extension later.
The C++ Programming language (4th ed)[2] is for experienced programmers. A tour of C++ is the short version of the former.[3]
[1]: http://www.stroustrup.com/programming.html
Would be cool to know more about that.
Crates.io has 3700 crates in stock. That said, there are still gaps, like with any young ecosystem. It's really progressing nicely though. And given Rust's domain, there are packages for really interesting stuff, like OS dev...
Starting startup with language X depends on how that language were used in industry, starting hardware startup with Python/Ruby is strange, but for web startup its totally fine. Now think about creating web site in C++ with average C++ developer.
There is also cost of talent, if you own expert C++ developer than probably writing web site in C++ can make sense, because you have a talent who can manage and fix every bug/feature. Finding professional Python/Ruby web site developers are easier than finding C++ web site developer.
auto start = std::chrono::system_clock::now();
// benchmark something here
auto end = std::chrono::system_clock::now();
is concise. I think that start, end = benchmark do
# benchmark something here
end
is concise.Deleted comment
auto start = std::chrono::system_clock::now();This is easy enough to test with ab (apache bench), siege, or jmeter, by just cranking up the number of concurrent requests. I've seen 100x differences in implementations of web servers before, 40x for python vs c++ is pretty believable.
Today you can get 15MB/s to S3. A not uncommon mysql node might do 10MB/s. Good luck trying to push that throughput with python or Ruby on a single process through a http response. Much faster I/O exists with sequential HD, SSD, and RAM based I/O. Most networks are built with 1Gbit Ethernet allowing 100MB/s that won't get saturated in the slower languages without going parallel which can increase total system complexity and lines of code.
Note: Also good to do it on cheap, throwaways because this sort of thing burns out the CPU's. Better that box than my main one.