windytan / redsea

Command-line FM-RDS decoder with JSON output.
MIT License
390 stars 36 forks source link

Reduce the writes calls to output stream using streamstring #56

Closed athoik closed 6 years ago

athoik commented 6 years ago

This commit reduces the number of write calls to output stream by writing all calls to an intermediate stringstream. Then we are writing the contents of stringstream to out stream followed by the flush to make sure the data will apper asap on the output.

The line buffering also was removed, apperently it didn't work as expcected.

Before applying this commit several write calls occured:

write(2, "{", 1{) = 1 write(2, "\"di\"", 4"di") = 4 write(2, ":", 1:) = 1 write(2, "{", 1{) = 1 write(2, "\"compressed\"", 12"compressed") = 12 write(2, ":", 1:) = 1 write(2, "false", 5false) = 5 write(2, "}", 1}) = 1 write(2, ",", 1,) = 1 write(2, "\"group\"", 7"group") = 7 write(2, ":", 1:) = 1 write(2, "\"0A\"", 4"0A") = 4 write(2, ",", 1,) = 1 write(2, "\"is_music\"", 10"is_music") = 10 write(2, ":", 1:) = 1 write(2, "true", 4true) = 4 write(2, ",", 1,) = 1 write(2, "\"pi\"", 4"pi") = 4 write(2, ":", 1:) = 1 write(2, "\"0x1367\"", 8"0x1367") = 8 write(2, ",", 1,) = 1 write(2, "\"prog_type\"", 11"prog_type") = 11 write(2, ":", 1:) = 1 write(2, "\"Easy listening\"", 16"Easy listening") = 16 write(2, ",", 1,) = 1 write(2, "\"ta\"", 4"ta") = 4 write(2, ":", 1:) = 1 write(2, "false", 5false) = 5 write(2, ",", 1,) = 1 write(2, "\"tp\"", 4"tp") = 4 write(2, ":", 1:) = 1 write(2, "true", 4true) = 4 write(2, "}", 1}) = 1 write(2, "\n", 1 ) = 1

With the patch applied the data received on one write call.

write(2, "{\"di\":{\"stereo\":false},\"group\":\""..., 117{"di":{"stereo":false},"group":"0A","is_music":true,"pi":"0x1367","prog_type":"Easy listening","ta":false,"tp":true} ) = 117

On the PrintHexGroup, only flush required, most probably because stdin is buffered by default.