Message-ID: From: "zkorhone (@zkorhone)" To: "pgjdbc/pgjdbc" Date: Fri, 17 Nov 2023 00:09:29 +0000 Subject: Re: [pgjdbc/pgjdbc] issue #2963: BufferedOutputStream in PGStream should be replaced to something more efficient In-Reply-To: References: List-Id: X-GitHub-Author-Login: zkorhone X-GitHub-Comment-Id: 1815525893 X-GitHub-Comment-Type: issue_comment X-GitHub-Issue: 2963 X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/issues/2963#issuecomment-1815525893 Content-Type: text/plain; charset=utf-8 I looked at the implementation in Java 21. There should be no buffer resizing, if you use size parameter. Resizing only applies when size is not defined. I think biggest optimization would be to replace FilterOutputStream with custom delegate. FilterOutputStream writes to given OutputStream one byte at a time. With BufferedOutputStream that means writing 1000byte array causes 1000lock and unlock calls. While removing locks by implementing custom BufferedOutputStream would fix this, implementation with FilterOutputStream would still write to memory in a very inefficient way (JVM might be able to optimize this, but why rely on that) I would first try, if replacing FilterOutputStream does enough. If not, then consider unsynchronized version. Just be sure not to extend BufferedOutputStream when getting rid of FilterOutputStream. There seems to be virtual thread optimizations that are only present when getClass equals to BufferedOutputStream.class