Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling the cost of polymorphism?

I have a codebase that is "needlessly" polymorphic in that almost every function is polymorphic in some way (why not, when you can?), but the end program uses functions with only a handful of concrete types. I've started spending some time throwing in SPECIALIZE and INLINABLE pragmas to try to bring down the performance cost of all this polymorphism, but with the size of my code it's pretty hit and miss. Is there a way to tell from profiling how much time is spent "doing the the things polymorphism needs" at runtime, for each function?

(Note: I've asked this question without knowing if such a thing is even technically possible or if "the things polymorphism needs" is well-defined enough).

like image 634
gspr Avatar asked Jul 18 '12 10:07

gspr


1 Answers

The process of determining costs is:

  • Construct a benchmark - with criterion, or some other measurement tool
  • Profile - with ghc's profiling support
  • Read the core - with ghc-core, if the performance causes are not obvious

Typically you will identify some operation that is too slow; compile with profiling and determine precisely which components are costly, and then inspect the code to optimize it (e.g. by specializing data structures or functions, changing algorithms, or making other changes).

For performance critical work you will then go and inspect the Core to see if micro-tuning the compiler can help.

like image 92
Don Stewart Avatar answered Nov 20 '22 16:11

Don Stewart