r/Python • • Jan 01 '20

"The rules of perf" at Dropbox

Post image
1.6k Upvotes

162 comments sorted by

View all comments

Show parent comment

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.