You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "ibessonov (via GitHub)" <gi...@apache.org> on 2023/12/12 14:48:15 UTC
[PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
ibessonov opened a new pull request, #2950:
URL: https://github.com/apache/ignite-3/pull/2950
https://issues.apache.org/jira/browse/IGNITE-21063
Thank you for submitting the pull request.
To streamline the review process of the patch and ensure better code quality
we ask both an author and a reviewer to verify the following:
### The Review Checklist
- [ ] **Formal criteria:** TC status, codestyle, mandatory documentation. Also make sure to complete the following:
\- There is a single JIRA ticket related to the pull request.
\- The web-link to the pull request is attached to the JIRA ticket.
\- The JIRA ticket has the Patch Available state.
\- The description of the JIRA ticket explains WHAT was made, WHY and HOW.
\- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
- [ ] **Design:** new code conforms with the design principles of the components it is added to.
- [ ] **Patch quality:** patch cannot be split into smaller pieces, its size must be reasonable.
- [ ] **Code quality:** code is clean and readable, necessary developer documentation is added if needed.
- [ ] **Tests code quality:** test set covers positive/negative scenarios, happy/edge cases. Tests are effective in terms of execution time and resources.
### Notes
- [Apache Ignite Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Java+Code+Style+Guide)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "ibessonov (via GitHub)" <gi...@apache.org>.
ibessonov commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425030559
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void release(ByteBuffer buffer) {
+ assert buffer.position() == 0;
+ assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
+
+ queue.add(buffer);
Review Comment:
It won't be longer if user doesn't violate the contract
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "tkalkirill (via GitHub)" <gi...@apache.org>.
tkalkirill commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425283101
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
Review Comment:
Yes, but for a few cycles it’s not optimal, but it’s not scary
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
Review Comment:
Yes, but for a few cycles it’s not optimal, but it’s not scary
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "tkalkirill (via GitHub)" <gi...@apache.org>.
tkalkirill commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425009788
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
Review Comment:
```suggestion
/** Basic pool implementation with limited capacity. */
```
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
Review Comment:
doesn't look atomic and race-safe
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void release(ByteBuffer buffer) {
+ assert buffer.position() == 0;
+ assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
Review Comment:
The maximum size can only be determined externally, it is also not entirely obvious and somehow needs to be reflected in the method above or in the documentation
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void release(ByteBuffer buffer) {
+ assert buffer.position() == 0;
+ assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
+
+ queue.add(buffer);
Review Comment:
in the queue it turns out it may be larger than the maximum size, a little strange
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
Review Comment:
It’s a little unclear to me that it should create a buffer with a default size, can it be passed as an argument?
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/OptimizedMarshaller.java:
##########
@@ -100,15 +110,36 @@ public byte[] marshall(Object o) {
}
buffer = expandBuffer(buffer);
+
+ if (buffer.capacity() <= MAX_CACHED_BUFFER_BYTES && poolBuffer != null) {
+ poolBuffer = buffer;
+ } else if (poolBuffer != null) {
+ poolBuffer.position(0);
+ pool.release(poolBuffer);
+
+ poolBuffer = null;
+ }
}
+ // Prevent holding the reference for too long.
+ stream.setBuffer(EMPTY_BUFFER);
+
byte[] result = Arrays.copyOf(buffer.array(), buffer.position());
- buffer.position(0);
+ if (poolBuffer != null) {
+ poolBuffer.position(0);
+ pool.release(poolBuffer);
+ }
return result;
}
+ /**
+ * Invoked on empty buffer, before writing any data to it.
+ */
Review Comment:
```suggestion
/** Invoked on empty buffer, before writing any data to it. */
```
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
Review Comment:
It’s a little unclear to me that it should create a buffer with a default size, can it be passed as an argument?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "tkalkirill (via GitHub)" <gi...@apache.org>.
tkalkirill commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425287459
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void release(ByteBuffer buffer) {
+ assert buffer.position() == 0;
+ assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
+
+ queue.add(buffer);
Review Comment:
Since we personally discussed that this is a very private interface, therefore insist on improving it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "ibessonov (via GitHub)" <gi...@apache.org>.
ibessonov commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425029710
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
Review Comment:
It can be, but why would I over-complicate things. Maybe default size should be a parameter in the constructor, but do we really need that?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "ibessonov (via GitHub)" <gi...@apache.org>.
ibessonov commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425030045
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void release(ByteBuffer buffer) {
+ assert buffer.position() == 0;
+ assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
Review Comment:
Ok, I'll reflect that
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "ibessonov (via GitHub)" <gi...@apache.org>.
ibessonov commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425099147
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
Review Comment:
This code is correct
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "ibessonov (via GitHub)" <gi...@apache.org>.
ibessonov merged PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-21063 Optimize memory usage in optimized marshaller [ignite-3]
Posted by "tkalkirill (via GitHub)" <gi...@apache.org>.
tkalkirill commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425286016
##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+ /** Max pool size. */
+ private final int capacity;
+
+ /** Queue with cached buffers. */
+ private final Queue<ByteBuffer> queue;
+
+ /** Pool size. */
+ private final AtomicInteger size = new AtomicInteger();
+
+ /**
+ * Constructor.
+ *
+ * @param capacity Max pool size.
+ */
+ public DefaultByteBuffersPool(int capacity) {
+ this.capacity = capacity;
+
+ queue = new ConcurrentLinkedQueue<>();
+ }
+
+ @Override
+ public @Nullable ByteBuffer borrow() {
+ ByteBuffer buffer = queue.poll();
+
+ if (buffer != null) {
+ return buffer;
+ }
+
+ if (size.get() < capacity && size.getAndIncrement() < capacity) {
+ return ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
Review Comment:
I would do it through the method, the constructor is a little worse, at your discretion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org