I noticed that the emitting of the trace code is relatively slow. It takes a few percentages (6%) of the total duration of a restore script. Including Localhost traffic to the database.
First I disabled all the listeners inside the app config. This brought it down to 0.7% of the total time needed to rescript the db.
I propose one additional minor tweak to prevent formatting the message at all. That brings the required processing time down to 0.088%.