Skip to content

Commit f18ea71

Browse files
IGNITE-28848 ZooKeeper discovery: stream marshalZip through DeflaterOutputStream to cut allocations and peak memory (#13319)
1 parent 2272b31 commit f18ea71

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

modules/zookeeper/src/main/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoveryImpl.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package org.apache.ignite.spi.discovery.zk.internal;
1919

20+
import java.io.BufferedOutputStream;
2021
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
2123
import java.io.Serializable;
2224
import java.util.ArrayList;
2325
import java.util.Arrays;
@@ -39,7 +41,7 @@
3941
import java.util.concurrent.atomic.AtomicBoolean;
4042
import java.util.concurrent.atomic.AtomicReference;
4143
import java.util.zip.DataFormatException;
42-
import java.util.zip.Deflater;
44+
import java.util.zip.DeflaterOutputStream;
4345
import java.util.zip.Inflater;
4446
import java.util.zip.InflaterInputStream;
4547
import org.apache.ignite.Ignite;
@@ -4064,33 +4066,21 @@ private <T> T unmarshalZip(byte[] zipBytes) throws Exception {
40644066

40654067
/**
40664068
* @param obj Object.
4067-
* @return Bytes.
4069+
* @return Zip-compressed marshalled bytes.
40684070
* @throws IgniteCheckedException If failed.
40694071
*/
40704072
byte[] marshalZip(Object obj) throws IgniteCheckedException {
40714073
assert obj != null;
40724074

4073-
return zip(U.marshal(marsh, obj));
4074-
}
4075-
4076-
/**
4077-
* @param bytes Bytes to compress.
4078-
* @return Zip-compressed bytes.
4079-
*/
4080-
private static byte[] zip(byte[] bytes) {
4081-
Deflater deflater = new Deflater();
4082-
4083-
deflater.setInput(bytes);
4084-
deflater.finish();
4075+
GridByteArrayOutputStream out = new GridByteArrayOutputStream();
40854076

4086-
GridByteArrayOutputStream out = new GridByteArrayOutputStream(bytes.length);
4087-
4088-
final byte[] buf = new byte[bytes.length];
4089-
4090-
while (!deflater.finished()) {
4091-
int cnt = deflater.deflate(buf);
4092-
4093-
out.write(buf, 0, cnt);
4077+
// BufferedOutputStream's 8 KB buffer coalesces JdkMarshaller's ~1 KB ObjectOutputStream
4078+
// block-data writes into fewer Deflater JNI calls.
4079+
try (BufferedOutputStream zipOut = new BufferedOutputStream(new DeflaterOutputStream(out))) {
4080+
U.marshal(marsh, obj, zipOut);
4081+
}
4082+
catch (IOException e) {
4083+
throw new IgniteCheckedException("Failed to marshal object: " + obj, e);
40944084
}
40954085

40964086
return out.toByteArray();

0 commit comments

Comments
 (0)