TsReaper commented on a change in pull request #10797: 
[FLINK-15172][table-blink] Optimize the operator algorithm to lazily allocate 
memory
URL: https://github.com/apache/flink/pull/10797#discussion_r365168981
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/util/LazyMemorySegmentPool.java
 ##########
 @@ -0,0 +1,118 @@
+/*
+ * 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.flink.table.runtime.util;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.runtime.memory.MemoryAllocationException;
+import org.apache.flink.runtime.memory.MemoryManager;
+
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * {@link MemorySegmentPool} that lazy allocate memory pages from {@link 
MemoryManager}.
+ */
+public class LazyMemorySegmentPool implements MemorySegmentPool, Closeable {
+
+       private static final long PER_REQUEST_MEMORY_SIZE = 16 * 1024 * 1024;
+
+       private final Object owner;
+       private final MemoryManager memoryManager;
+       private final boolean freeMore;
+       private final ArrayList<MemorySegment> cachePages;
+       private final int maxPages;
+       private final int perRequestPages;
+
+       private int pageUsage;
+
+       public LazyMemorySegmentPool(Object owner, MemoryManager memoryManager, 
int maxPages) {
+               this(owner, memoryManager, maxPages, false);
+       }
+
+       public LazyMemorySegmentPool(Object owner, MemoryManager memoryManager, 
int maxPages, boolean freeMore) {
+               this.owner = owner;
+               this.memoryManager = memoryManager;
+               this.freeMore = freeMore;
+               this.cachePages = new ArrayList<>();
+               this.maxPages = maxPages;
+               this.pageUsage = 0;
+               this.perRequestPages = Math.max(1, (int) 
(PER_REQUEST_MEMORY_SIZE / memoryManager.getPageSize()));
+       }
+
+       @Override
+       public int pageSize() {
+               return this.memoryManager.getPageSize();
+       }
+
+       @Override
+       public void returnAll(List<MemorySegment> memory) {
+               this.pageUsage -= memory.size();
+               if (this.pageUsage < 0) {
+                       throw new RuntimeException("Return too more memories.");
+               }
+               this.cachePages.addAll(memory);
+
+               if (this.freeMore && this.cachePages.size() >= 
this.perRequestPages) {
 
 Review comment:
   Is this `freeMore` mode actually in use? How much does it impact the 
performance?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to