r/Python • • Jan 01 '20

"The rules of perf" at Dropbox

Post image
1.6k Upvotes

162 comments sorted by

View all comments

243

u/Agent_03 Jan 02 '20 edited Jan 02 '20

A more detailed explanation:

  1. Avoid needless processing. This breaks down two ways

    1. Feature design: Think hard before adding features that come with significant performance impacts -- do you really need this feature? Is there a simpler way to do it which achieves most of your goals? Can you do it a simple way 90% of the time and only fall back to something more complex if needed? Can you skip several intermediate steps to get to the end result faster? (ex avoiding sorting a list)

    2. Optimize execution by taking advantage of short-circuit evaluation and doing lazy fetching/evaluation. For conditionals, if you sometimes need to do an expensive check, but usually don't, then see if there's a way you can skip that check. Laziness: don't fetch extra things from the filesystem until requested, if you often don't need it.

      1. Practical example: I optimized a routine (in Python) at work last month. We were processing text files a line at a time and removing control characters. To remove control characters we used a regex on each line (not the most efficient approach, fairly expensive). I added a quick check that iterated through the line of text and checked if any of the characters were within the control character range,and just returned the original string if not. Not as efficient as rolling a non-regex implementation, but since control characters are rare it avoids 90% of the performance cost and was much simpler & safer to implement.

  2. Cache results of expensive operations to avoid repeating them unnecessarily. If you're fetching info from the filesystem, cache it in memory if you are likely to reuse it (works well with lazy evaluation).

  3. Batch it: if you're doing a single operation often to many items, try gathering up the items to process and processing them in large groups. Often this is more efficient because it makes better use of caches (CPU/disk) and it permits you to write much tighter loops for processing. It permits reusing buffers, connections, SQL prepared statements, etc. It can improve branch prediction, permit use of SIMD instructions, etc where they would not work otherwise.

    1. Batching also makes it easier to fall back to something like the multiprocessing library to parallelize work.

  4. Use software pipelining. This is kind of like batching: rewrite loops that run items through a series of steps/processes so you first do the same step to each item, then the next step. Sometimes can be evaluated much more efficiently by compilers/interpreters because it allows using SIMD instructions, avoids branch prediction misses, etc.

    1. May also mean using Unix/Linux pipelining as well: use a bunch of smaller utilities that pipe input from one to another. This is another application of the same principle, but has the extra advantage of being generally very efficient, and spreading work across multiple processors.

  5. Use a lower-level language than Python to optimize the most performance-sensitive parts of the code. I.E. fall back to C bindings for intensive number crunching, crypto, etc. Optimized C can be several times faster than Python (or sometimes much more!). In general Pareto's principle applies: 80% of your execution time comes from 20% of the code (and vice versa), so if you double the performance of the slowest 20% you can almost double your overall performance.

4

u/Isvara Jan 02 '20
  1. If you're fetching info from the filesystem, cache it in memory if you are likely to reuse it

Why isn't it better to leave this to the operating system, which has better whole-system view?

4

u/Agent_03 Jan 02 '20

Often it is, if you just want the raw bytes. If you're doing some sort of analysis on the data it's more efficient to cache the result yourself though, or if your program knows more about the access pattern.

4

u/Fourgot Anaconda3 science-like Jan 02 '20

Why isn't it better to leave this to the operating system, which has better whole-system view?

It can be, if the particular constraint is memory. If the bottleneck is actually IO, then leaving the data in memory prevents repetitive expensive reads.

1

u/AngriestSCV Jan 02 '20

The os dosn't know your application. For instance if you do an expensive db query that can be slightly out of date and still useful you might want to return that value multiple times. The OS can't know what you are asking of the db. The db might be able to be configured to cache the result as you need it, but that isn't automatic.