Java: Efficiency of the readLine method of the BufferedReader and possible alternatives

前端 未结 4 2002
一生所求
一生所求 2021-02-13 22:24

We are working to reduce the latency and increase the performance of a process written in Java that consumes data (xml strings) from a socket via the readLine() method of the Bu

4条回答
  •  情歌与酒
    2021-02-13 22:41

    If you know the character-encoding of the incoming data you may want to write your own class that performs reading of binary data, looking for your specific end-of-line terminator. This may remove a lot of unnecessary encoding/decoding and copying. Make sure you implement something with a re-usable buffers (e.g. NIO's CharBuffer or ByteBuffer classes would come to mind, or a correctly initialized StringBuilder if you need String instances). Make sure you've got enough space in the buffer, 32Ki to 64Ki is nothing for current computers.

    Once you've gotten the data in a usable container you can use any trick in the book (multiple threads, executors etc.) to handle the data efficiently. Remember, the only way to slow down a current CPU is to hit cache-misses - large/dynamic data sets, spurious copying - or branches - unnecessary loops, if statements and what's more and of course kernel calls and I/O.

提交回复
热议问题