Can the compiler optimize repeated pushes to Vec/String?


#1

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):

a.push_str("h")
a.push_str("e")
a.push_str("y")

is equivalent to:

a.push_str("hey")

but was not an available transformation.

Still the video is really fun to watch.


#2

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