

{"id":1307,"date":"2014-05-18T21:36:07","date_gmt":"2014-05-18T19:36:07","guid":{"rendered":"http:\/\/fabsk.eu\/blog\/?p=1307"},"modified":"2023-12-11T21:59:03","modified_gmt":"2023-12-11T19:59:03","slug":"java-pipedinputstream-contention","status":"publish","type":"post","link":"https:\/\/fabsk.eu\/blog\/2014\/05\/18\/java-pipedinputstream-contention\/","title":{"rendered":"Java: PipedInputStream contention"},"content":{"rendered":"\n<p>I met a performance issue with <em><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/PipedInputStream.html\">PipedInputStream<\/a><\/em>\/<em>PipedOutputStream<\/em>, and here is how I finally solved it.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>I had a compressed XML file that I wanted to process (with StAX\/JAXB), and it would be a good opportunity to put the decompression on one thread\/core, and the XML processing on another. Fortunately, Java propose a nice pair of classes allowing to pass data from one thread to another: <em>PipedInputStream<\/em> and <em>PipedOutputStream<\/em>.<\/p>\n\n\n\n<p>So I would have two threads:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the first one would read the file, decompress it and write on the <em>PipedOutputStream<\/em>.<\/li>\n\n\n\n<li>the other one would parse\/unmarshal the XML from an <em>InputStream<\/em>, which by chance would be a <em>PipedInputStream<\/em><\/li>\n<\/ul>\n\n\n\n<p>The result did not look great: it was slower that the single-threaded solution! I tried to increase the buffer size of the pipe, with limited success (I could only sometimes get the same performance level with 16Mo of buffer).<\/p>\n\n\n\n<p>After some profiling, I notice that the <em>PipedInputStream<\/em> was spending a good amount of time in a <em>wait()<\/em> with a delay of 1 second! Then I finally got it: the reader will sometimes <strong>poll<\/strong> when waiting for data. But it would not prevent the program to work, it would degrade the performance silently.<\/p>\n\n\n\n<p>To prevent that, the solution was to call explicitly \u00ab<strong>flush()<\/strong>\u00bb on the <em>PipedOutputStream<\/em> from time to time, it would notify the reader thread that data is available.<\/p>\n\n\n\n<p>In my case, my decompression thread used <em>IOUtils.copy()<\/em> to pass data from the compressed stream to the pipe stream. And once in a while, when the reader had consumed all the available data, I was hit by the 1s delay. So I replaced it by a manual copy, with a <em>flush()<\/em> after each loop.<\/p>\n\n\n\n<p>Now the performance is very good: slightly better than decompressing with \u00ab\u00a0unxz\u00a0\u00bb and passing the data through a pipe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I met a performance issue with PipedInputStream\/PipedOutputStream, and here is how I finally solved it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,6,28],"tags":[],"class_list":["post-1307","post","type-post","status-publish","format-standard","hentry","category-dev","category-informatique","category-java","\"lang=\"en"],"_links":{"self":[{"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/posts\/1307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/comments?post=1307"}],"version-history":[{"count":6,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/posts\/1307\/revisions"}],"predecessor-version":[{"id":3004,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/posts\/1307\/revisions\/3004"}],"wp:attachment":[{"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/media?parent=1307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/categories?post=1307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabsk.eu\/blog\/wp-json\/wp\/v2\/tags?post=1307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}