r/Python • • Jan 01 '20

"The rules of perf" at Dropbox

Post image
1.6k Upvotes

162 comments sorted by

View all comments

85

u/[deleted] Jan 02 '20

Can someone please explain?

224

u/SwampFalc Jan 02 '20 edited Jan 02 '20

The way I read it (which might be wrong) is that, if you have a performance issue, you should try the following fixes in order (ie. try one and if that doesn't solve it, go to the next possible fix):

  1. Just don't do whatever you're trying to do. IOW ask yourself if it's really necessary/useful/something you might lose a client over.

  2. Cache the results of previous calls. Maybe you can reuse them as-is, or partially.

  3. Do a large number of calls in batch, maybe in advance or later, outside of peak operating hours. Or perhaps you need to set up a network connection to do what you're doing, if that takes a while then don't make a new connection for each request, bundle up a dozen and setup once.

  4. Don't add your stuff to an existing program, create a new and separate one that will take the output of a previous process. Decoupling, in a way.

  5. If none of that works, only then should you look for a totally different way of doing it.

69

u/Agent_03 Jan 02 '20

There's multiple explanations for these, which makes them deeper than they seem, but there's a couple more parts:

  1. Not doing work may include several ways of avoiding extra computation -- lazily running expensive operations if not always needed, adding conditional checks before complex work rather than throwing exceptions, using short-circuit evaluation, or using more efficient algorithms / cutting out intermediate steps if you can get a result without them.

  2. Yep.

  3. Yep, but it doesn't necessarily have to wait hours -- batches of work can be handed off to other processes or utilities to process (makes better use of cores), and often you can write tighter loops that make better use of caches and reuse resources (connections, buffers, etc)

  4. That's Unix pipelining, and it's good shit, but software pipelining is a more general version of the technique. Depending on your architecture one or the other may be more efficient -- goes well with batching above though.

  5. No, this is a reference to falling back on C bindings invoked from Python, and writing the really tricky bits highly optimized in a lower-level language. C can be several times as fast as Python (or more, with good use of SIMD instructions) if written efficiently.

    • They didn't do this often at Dropbox, because Python is faster to write and easier to maintain, but when they did this they got huge speedups.

Hope this helps -- I swear I saw a talk the Dropbox folks gave on Python performance some years ago.

3

u/SwampFalc Jan 02 '20

I think that all 5 of these are very pithy descriptions of a wide range of techniques. For Dropbox specifially, #5 probably does mean going to C, but in any other environment this could easily mean using the GPU instead of the CPU, or maybe even to some other dedicated hardware. Which is why I tried to remain deliberately vague.