Can the compiler optimize repeated pushes to Vec/String?


There was an interesting talk on type system tips at RustConf 2017. Sean Griffin at one point walks through some macro expansions and compile-time optimizations that are performed for a typical Diesel query generator.

He ends by talking about the last step which the compiler couldn’t do (at least at the time of the talk):


is equivalent to:


but was not an available transformation.

Still the video is really fun to watch.


In theory it could. Vec (and thus String) push(), however, has some optimizer-hostile code: Performance comparison between kvec and C