You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "liuzqt (via GitHub)" <gi...@apache.org> on 2023/07/24 21:38:53 UTC

[GitHub] [spark] liuzqt commented on a diff in pull request #41782: [SPARK-44239][SQL] Free memory allocated by large vectors when vectors are reset

liuzqt commented on code in PR #41782:
URL: https://github.com/apache/spark/pull/41782#discussion_r1272771220


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -476,6 +476,23 @@ object SQLConf {
     .intConf
     .createWithDefault(10000)
 
+  val VECTORIZED_HUGE_VECTOR_RESERVE_RATIO =
+    buildConf("spark.sql.inMemoryColumnarStorage.hugeVectorReserveRatio")
+      .doc("spark will reserve requiredCapacity * this ratio memory next time")

Review Comment:
   I think it worth mention here this ratio is only effective when `VECTORIZED_HUGE_VECTOR_THRESHOLD` is enabled and required mem larger that threshold



##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/VectorReservePolicy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.spark.sql.execution.vectorized;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.spark.sql.internal.SQLConf;
+import org.apache.spark.unsafe.array.ByteArrayMethods;
+
+public abstract class VectorReservePolicy {
+
+  protected int defaultCapacity;
+
+  /**
+   * Upper limit for the maximum capacity for this column.
+   */
+  @VisibleForTesting
+  protected int MAX_CAPACITY = ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH;
+
+  /**
+   * True if this column has default values. Return the default values instead of NULL when the
+   * corresponding columns are not present in storage. We can not reset the data of column vectors
+   * that has default values.
+   */
+  protected boolean hasDefaultValue = false;
+
+  abstract int nextCapacity(int requiredCapacity);
+
+  boolean shouldCleanData() {

Review Comment:
   Maybe make this abstract as well? also for `defaultCapacity`. Does seem to be necessary in the base abstract class



##########
sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/VectorReservePolicy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.spark.sql.execution.vectorized;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.spark.sql.internal.SQLConf;
+import org.apache.spark.unsafe.array.ByteArrayMethods;
+
+public abstract class VectorReservePolicy {
+
+  protected int defaultCapacity;
+
+  /**
+   * Upper limit for the maximum capacity for this column.
+   */
+  @VisibleForTesting
+  protected int MAX_CAPACITY = ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH;
+
+  /**
+   * True if this column has default values. Return the default values instead of NULL when the
+   * corresponding columns are not present in storage. We can not reset the data of column vectors
+   * that has default values.
+   */
+  protected boolean hasDefaultValue = false;
+
+  abstract int nextCapacity(int requiredCapacity);
+
+  boolean shouldCleanData() {
+    return false;
+  }
+}
+
+class DefaultVectorReservePolicy extends VectorReservePolicy {
+
+  private int hugeThreshold;
+  private double hugeReserveRatio;
+  private int currentCapacity;
+
+  DefaultVectorReservePolicy(int defaultCapacity) {
+    this.defaultCapacity = defaultCapacity;
+    this.currentCapacity = defaultCapacity;
+    SQLConf conf = SQLConf.get();
+    hugeThreshold = conf.vectorizedHugeVectorThreshold();
+    hugeReserveRatio = conf.vectorizedHugeVectorReserveRatio();
+  }
+
+  @Override
+  public int nextCapacity(int requiredCapacity) {
+    if (hugeThreshold < 0 || requiredCapacity < hugeThreshold) {
+      currentCapacity = (int) Math.min(MAX_CAPACITY, requiredCapacity * 2L);
+    } else {
+      currentCapacity = (int) Math.min(MAX_CAPACITY, requiredCapacity * hugeReserveRatio);

Review Comment:
   This may lead to conter-intuitive behavior for example If the threshold is 1000, then require 999 might look more memory-hungry than 1001. How about we make it 
   ```
   (requiredCapacity - hugeThreshold) * hugeReserveRatio + hugeThreshold * 2L
   ``` 
   
   to be more consistent



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org