wankunde commented on code in PR #41782: URL: https://github.com/apache/spark/pull/41782#discussion_r1277068049
########## 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: Thanks @liuzqt for your review. For `(requiredCapacity - hugeThreshold) * hugeReserveRatio + hugeThreshold * 2L` is equal to `requiredCapacity * hugeReserveRatio + hugeThreshold * (2L - hugeReserveRatio)`. If hugeThreshold= 100M, and the requiredCapacity = 1000, before this change, spark will only allocate 2000 bytes while after this change, spark will allocate 1000 * 1.2 + 100M * 0.8 ~= 80M bytes, will small column vectors take up too much memory? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
