|
|
|
|
|
by nhaehnle
3842 days ago
|
|
Compiler optimizations as part of libraries is definitely something I would like to see more languages explore. I think this is something that could fit quite well into Rust, actually, with its philosophy of allowing compiler plugins. A simple example: Most high-level array implementations have a reserve method that allows you to specify the expected number of items added to the array. This allows allocating all required memory up front and hence avoids re-allocs and the associated copies. In many cases, it would be trivial for a compiler to automatically determine the size of the array and add a reserve call automatically (because the entries are added in a loop with an easily-determined iteration count). Yet compilers cannot be allowed to do that, because doing so changes the visible behaviour of the program. However, the library in which the array is implemented is free to define the semantics of the array. So that library could provide some hint to the compiler, in essence a library-provided optimization pass, which allows such reserve calls to be added automatically. I'm not saying that coming up with a good DSL to describe such optimization is easy, but it may well be worthwhile in the long run. |
|
[1]: https://wiki.haskell.org/Playing_by_the_rules
[2]: http://hackage.haskell.org/package/vector-0.7.1/docs/src/Dat...