projjal commented on a change in pull request #9813:
URL: https://github.com/apache/arrow/pull/9813#discussion_r604831679



##########
File path: cpp/src/gandiva/cache.cc
##########
@@ -38,6 +42,22 @@ int GetCapacity() {
   return capacity;
 }
 
+int GetCacheTypeToUse() {
+  int cache_type;
+  const char* env_cache_type = std::getenv("GANDIVA_CACHE_TYPE");

Review comment:
       Lets not use environment variable to configure the behaviour, its better 
to use some configuration option. The configuration class in Gandiva contains 
only a single projector/filter level options. Maybe the first projector/filter 
that is built in gandiva can have a config option to set the cache policy, 
which cannot be changed afterwards.

##########
File path: cpp/src/gandiva/base_cache.h
##########
@@ -0,0 +1,55 @@
+// 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.
+
+#pragma once
+
+#include <list>
+#include <unordered_map>
+#include <utility>
+
+#include "arrow/util/optional.h"
+
+namespace gandiva {
+
+template <class Key, class Value>
+class BaseCache {

Review comment:
       Can you add some comments?

##########
File path: cpp/src/gandiva/lower_value_used_cache.h
##########
@@ -0,0 +1,103 @@
+// 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.
+
+#pragma once
+
+#include <list>
+#include <set>
+#include <unordered_map>
+#include <utility>
+
+#include "arrow/util/optional.h"
+#include "gandiva/base_cache.h"
+
+// modified cache to support evict policy of lower value used.
+namespace gandiva {
+// a cache which evicts the lower value used item when it is full
+template <class Key, class Value>
+class LowerValueUsedCache : public BaseCache<Key, Value> {
+ public:
+  struct hasher {
+    template <typename I>
+    std::size_t operator()(const I& i) const {
+      return i.Hash();
+    }
+  };
+  using map_type = std::unordered_map<
+      Key, std::pair<Value, typename std::set<std::pair<uint64_t, 
Key>>::iterator>,
+      hasher>;
+
+  explicit LowerValueUsedCache(size_t capacity) : BaseCache<Key, 
Value>(capacity) {}
+
+  LowerValueUsedCache<Key, Value>() : BaseCache<Key, Value>() {}
+
+  size_t size() const override { return map_.size(); }
+
+  size_t capacity() const override { return this->cache_capacity_; }
+
+  bool empty() const override { return map_.empty(); }
+
+  bool contains(const Key& key) override { return map_.find(key) != 
map_.end(); }
+
+  void insert(const Key& key, const Value& value,
+              const uint64_t value_to_order) override {
+    typename map_type::iterator i = map_.find(key);
+    if (i == map_.end()) {
+      // insert item into the cache, but first check if it is full
+      if (size() >= this->cache_capacity_) {
+        // check if the value should be inserted on cache, otherwise just 
return
+        if (value_to_order <= lvu_set_.begin()->first) return;
+
+        // cache is full, evict the least recently used item
+        evict();
+      }
+
+      // insert the new item
+      lvu_set_.insert(std::make_pair(value_to_order, key));
+      map_[key] = std::make_pair(value, lvu_set_.begin());

Review comment:
       Why adding lvu_set_.begin()

##########
File path: cpp/src/gandiva/cache.h
##########
@@ -31,29 +32,40 @@ int GetCapacity();
 GANDIVA_EXPORT
 void LogCacheSize(size_t capacity);
 
+GANDIVA_EXPORT
+int GetCacheTypeToUse();
+
 template <class KeyType, typename ValueType>
 class Cache {
  public:
-  explicit Cache(size_t capacity) : cache_(capacity) { LogCacheSize(capacity); 
}
+  explicit Cache(size_t capacity, int cache_type_to_use) {
+    if (cache_type_to_use == 0) {
+      this->cache_ = new LowerValueUsedCache<KeyType, ValueType>(capacity);

Review comment:
       Why use plain pointers? Its also not being freed. Please use unique_ptr

##########
File path: cpp/src/gandiva/lower_value_used_cache.h
##########
@@ -0,0 +1,103 @@
+// 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.
+
+#pragma once
+
+#include <list>
+#include <set>
+#include <unordered_map>
+#include <utility>
+
+#include "arrow/util/optional.h"
+#include "gandiva/base_cache.h"
+
+// modified cache to support evict policy of lower value used.
+namespace gandiva {
+// a cache which evicts the lower value used item when it is full
+template <class Key, class Value>
+class LowerValueUsedCache : public BaseCache<Key, Value> {

Review comment:
       Just evicting based on the build times seems would be problematic since 
it might just keep only infrequently used large expressions. You should use a 
combination of build time and some aging policy.
   Also can you change the name. It sounds confusing.




-- 
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:
[email protected]


Reply via email to