mozilla / mentat

UNMAINTAINED A persistent, relational store inspired by Datomic and DataScript.
https://mozilla.github.io/mentat/
Apache License 2.0
1.65k stars 115 forks source link

Avoid monomorphizing large functions multiple times #774

Open thomcc opened 6 years ago

thomcc commented 6 years ago

Related to #772.

Most of this is taken from https://gist.github.com/thomcc/09f48b222e4fdf69c43479ee65daf2ab (which is taken from a list of top functions of mentat_ffi when compiled as a cdylib)

If you look at that listing there are a number of duplicated functions, which are almost all going to be a result of the function being monomorphized multiple times. We should avoid these for anything large.

IntoIterator seems like a frequent offender here. In other code ,stuff like Into and AsRef are often responsible.

There are probably more that just get inlined too and contribute to their caller getting bigger. There's a balance between this and performance though, so it's not 100% cut and dry, but for huge functions the overhead of e.g. collecting an iterator into a vector seems likely to be negligible.

ncalexan commented 6 years ago

This is very interesting, @thomcc. I quite like the IntoIterator pattern, but if it's costing us huge amounts of duplicated code that's not good. Does the impl Trait or Box<Iter> pattern help with the code bloat, perhaps at the cost of runtime performance? (For most of the things we're doing we don't care about the input iterator performance at all; it's dwarfed by the processing overhead.)