viirya commented on code in PR #6860: URL: https://github.com/apache/datafusion-comet/pull/6860#discussion_r4262976387
########## spark/src/main/scala/org/apache/spark/sql/comet/GeneratedClassCache.scala: ########## @@ -0,0 +1,90 @@ +/* + * 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.comet + +import java.util.{LinkedHashMap => JLinkedHashMap, Map => JMap} +import java.util.concurrent.atomic.AtomicLong + +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.DataType + +/** + * What code generated over a list of columns depends on: the type and nullability of each column, + * and the method size at which `CodegenContext.splitExpressions` splits the code for them. + */ +private[comet] case class ColumnLayout( + columns: Seq[(DataType, Boolean)], + methodSplitThreshold: Int) + +private[comet] object ColumnLayout { + + /** The layout of `columns` under the current `spark.sql.codegen.methodSplitThreshold`. */ + def of(columns: Seq[Expression]): ColumnLayout = + ColumnLayout(columns.map(c => (c.dataType, c.nullable)), SQLConf.get.methodSplitThreshold) +} + +/** + * Generated classes kept for the life of an executor, under a key for what their source depends + * on. + * + * Spark caches a compiled class under its source, so an operator that generates code in every + * partition still generates the source every time, which for a wide or deeply nested schema can + * take longer than the partition's rows. Keeping the class under a cheaper key generates it once. + * The cache holds the `maxEntries` most recently used values. + */ +private[comet] class GeneratedClassCache[K, V <: AnyRef]( + maxEntries: Int = GeneratedClassCache.MaxEntries) { + + /** Least recently used first. Guarded by `entries.synchronized`. */ + private val entries = new JLinkedHashMap[K, V](16, 0.75f, true) { + override def removeEldestEntry(eldest: JMap.Entry[K, V]): Boolean = size() > maxEntries + } + + private val generated = new AtomicLong(0) + + /** How many values this cache has generated, for tests. */ + def generatedCount: Long = generated.get() + + /** + * The value for `key`, generated if absent. `generate` returns the value and whether other + * callers may share it. It runs outside the lock, so callers that miss on the same key at once + * each generate the value, as they would without the cache. + */ + def getOrGenerate(key: K)(generate: => (V, Boolean)): V = { + val cached = entries.synchronized(entries.get(key)) + if (cached != null) { + cached + } else { + val (value, shareable) = generate + generated.incrementAndGet() + if (shareable) { + val _ = entries.synchronized(entries.putIfAbsent(key, value)) + } + value + } + } +} + +private[comet] object GeneratedClassCache { + + /** The default bound of Spark's compiled class cache, `spark.sql.codegen.cache.maxEntries`. */ + val MaxEntries = 100 Review Comment: `MaxEntries` is documented as the default of `spark.sql.codegen.cache.maxEntries`, but nothing reads that conf, and no caller passes the `maxEntries` constructor parameter. With this PR there are two of these caches, so an executor can keep up to 200 more compiled classes alive, including ones Spark's own cache has already evicted. Could the default read `SQLConf.get.codegenCacheMaxEntries` instead? It is a static conf available on every Spark version we build against. That way a user who lowers it to bound metaspace gets the same bound here. If a fixed 100 is intended, could we drop the unused parameter and say so in the comment? -- 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]
