precompile: fix performance issues with IO (#56370)
The string API here rapidly becomes unusably slow if dumping much debug
output during precompile. Fix the design here to use an intermediate IO
instead to prevent that.
(cherry picked from commit e4dc9d357a1a46ac4078ae81265f6edd9b593bdf)