6 ms·
I can make two observations and hypotheses from the profiling results, which agree with Linus' conclusion of "bad user-level code": - Memory allocation/dealloc
by userbinator 12y ago
I can make two observations and hypotheses from the profiling results, which agree with Linus' conclusion of "bad user-level code":
- Memory allocation/deallocation are taking the most time.
- All the percentages for each function are very small.
The former is a characteristic of code which heavily abuses dynamic allocation. It's surprising to see how many programmers are not aware of the overhead it adds and would malloc()/free() frivolously when something simpler would suffice. This is also often accompanied by copious amounts of unnecessarily copying data around. I've worked with small embedded systems where every use of dynamic allocation would need to be justified thoroughly in code reviews; perhaps these developers would benefit from being put through the same process.
The latter is a phenomenon which arises from "excessive modularity": the functionality of the system has been split into so many little pieces that the time each function contributes to the overall total is tiny. Instead of seeing an obvious "80% of the time is being spent here" that could easily be targeted for optimisation, that 80% is scattered amongst several dozen functions each taking 1-2% each. The bottleneck isn't concentrated in one area --- the whole system is uniformly inefficient. It's extremely difficult to optimise a system like this because nothing in particular stands out as being optimisable. I've had to optimise some large Java applications that were like this, and the solution was basically to remove most of the code and rewrite it to get rid of many chains of indirection.
- avar 12y agoThe way you generally deal with the second problem is to have a profiler that can give you the inclusive time spent in a subroutine, not just the exclusive time. The inclusive time is the time spent in that sub and in subs it calls, the exclusive time is time spent only in that sub. When you have inclusive time it usually becomes easy to find that function that's spending 80% of the time in either itself or in the things it calls.
- FeepingCreature 12y agoSomebody should modify perf to log "time spent in call", "time spent in stackframe setup", "time spent in mov", "time spent in branches", etc.
- DannyBee 12y agoSo, you can get some of these kinds of cycle counts by changing the event that gets used for record. https://perf.wiki.kernel.org/index.php/Tutorial#Events https://perf.wiki.kernel.org/index.php/Tutorial#Events So you could use record -e branch_instructions, for example. Past that, things like "time spent in stackframe setup" is not sane to measure on an optimized binary, because the instructions hat set up the stack frame may be all over the place (due to shrink wrapping, etc). This means the overhead of counting them would probably require instrumentation and be quite high (and perf is sampling based, not instrumentation based)
- FeepingCreature 12y ago> Past that, things like "time spent in stackframe setup" is not sane to measure on an optimized binary, because the instructions hat set up the stack frame may be all over the place (due to shrink wrapping, etc). Doesn't debug info already account for stuff like this? So we can give line number info if you break on any instruction? Seems to me you'd "just" need to augment debug info with performance classes, then let the profiler display worry about classification after the fact.
- andor 12y agoThe percentages are small because a) This is the output of perf top, a system-wide trace of the last few seconds b) The application uses lots of library functions c) Due to lots of locking in the application, the cpu doesn't spend much time in the app itself To find out which high-level functions (that in turn call library functions) take the most time, use perf record to get call stacks.
- corford 12y agoFor the second problem, sounds like the dbus devs should follow sqlite's lead: http://permalink.gmane.org/gmane.comp.db.sqlite.general/90549 http://permalink.gmane.org/gmane.comp.db.sqlite.general/9054...
- yoklov 12y agoFrequently the second problem is exacerbated by cache misses. Every operation becomes (much!) slower, but nothing stands out. Not a popular opinion here, but excessive modularity like that is practically the definition of bad, hard to follow code though IMO. Ignoring performance issues, everything happens somewhere else, and large changes now take several times longer, since the changes are not local to a function. The code is several times longer than it otherwise would be due to all the function declarations... Its a nightmare. It tends to be the kind of code you feel productive while writing ("I'm cleaning up this 200 line function"), but is really just making the codebase worse (is there a general term for this kind of false productivity? It's a common problem I see).
- _pmf_ 12y ago> Its a nightmare. It tends to be the kind of code you feel productive while writing ("I'm cleaning up this 200 line function"), but is really just making the codebase worse (is there a general term for this kind of false productivity? It's a common problem I see). This issue is pervasive when Desktop/Web developers try to improve embedded software. I've achieved a thousandfold increase in performance by converting an embedded data logger from using printf to using a dedicated formatting function (most of the time was spent on parsing the format string and performing allocations).
- TheLoneWolfling 12y agoThe compiler doesn't do the parsing for printf at compile time for the common cases? That's semi-surprising.
- serve_yay 12y agoIndeed, decomposition is easier than extracting modules. This is often an argument in favor of modularity! :) As others have mentioned, the second issue can be addressed with tooling. But, is excessive modularity really the culprit? Are you saying you read the code and found that to be the case, or it's just a hunch? Reading through the comments here, it seems that whatever one's personal hobby-horse happens to be is what will get the blame.