Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is anyone else alarmed to see raw PHP spanking Flask, Sinatra, and other Python/Ruby frameworks?

If not, can anyone explain in layman's terms why that should be the case?



> Flask

I don't know about Sinatra or anything else but I can give you the reason why Flask out of the box performs like it does. Unlike PHP Flask is threadsafe and uses locks and thread or greenlet local storage. This level of indirection adds a basic performance penalty when executed on a single threaded server like gunicorn. There are ways to change that if you think that becomes a performance problem by replacing the stack object with one that does not do dispatching and by removing the locks.

This in theory is easy to do, it's just generally not a real issue since most Flask apps will actually wait on database more than they waste CPU time.

Additionally Flask has a huge request handling overhead which will be primarily noticable in dummy benchmarks that don't do much in view functions. If you have an application that actually barely executes anything in the views you can subclass the Flask object to remove some of the functionality you normally always pay for (request processors, session initialization, message flashing etc.)

Lastly Flask has some sanity checks when Python is not run with `-O` that simplify debugging. Depending on which code paths you hit you might be paying for some of that.

In the database test the primary slowness is most likely not Flask but the way the database is configured.

Generally if you ever encounter Flask to be an actual performance problem I am more than happy to point you to ways to get more speed out of it. Generally speaking though there is not much point micro-optimizing until you notice it is a problem.


But the other database tests were run against the same DB server and configuration. How can Flask's performance be the DB's fault?


> But the other database tests were run against the same DB server and configuration. How can Flask's performance be the DB's fault?

They are written against the core database layer or the framework (in case of Django the Django ORM) and in case of Flask: Flask-SQLAlchemy. The default config of both of these things is to not pool connections. That is not true for all other frameworks.

Also I'm not blaming anything here, just pointing out that there are differences in the setup.


Why shouldn't that be the case? PHP has been designed from the get go to be super fast and 5.4 introduced a bunch of optimizations that have sped it up even more so. Ruby was designed for programmer happiness, PHP was designed for performance and getting sh*t done.


PHP technically wasn't designed


> PHP has been designed from the get go to be super fast

PHP was never designed to be fast. As far as execution speed goes, PHP will be in the same ballpark as Perl?Python/Ruby. PHP comes up as faster in these tests because of all the extra things the frameworks are doing. See the_mitsuhiko comment above regarding flask for an example.


Not sure about ruby, but django doesn't ship with connection pooling, so that is probably why its database performance is terrible. It could be configured to use pooling.

With such a trivial request handler like the one in benchmark, only a small percentage of the execution is actually taken up in the view, so all of the time you're seeing in the benchmark is pure overhead of the framework. A longer running view across all of these frameworks would cause things to even out a bit. All that being said, however, it's not an unfair comparison. After all, a benchmark of this nature should be a measure of overhead because that's really all there is to measure.


Correct, because we chose MySQL as the database backend, both Django and Flask are punished for not having a connection pool. We've received requests to do the test using Postgres which does have a connection pool, and we hope to eventually get to that.


What are your using for db access? If you are using SQLAlchemy or Flask-SQLAlchemy, that should automatically do connection pooling.


Flask-SQLAlchemy does not use a pool by default. This can be changed by setting the `SQLALCHEMY_POOL_SIZE` config variable though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: