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



##########
File path: cpp/src/gandiva/cache.h
##########
@@ -39,17 +39,21 @@ class Cache {
 
   Cache() : Cache(GetCapacity()) {}
 
-  ValueType GetModule(KeyType cache_key) {
+  ::std::shared_ptr<Cache> create(size_t capacity) {
+    return ::std::make_shared<Cache>(cache_(capacity));

Review comment:
       `make_shared<Cache>(capacity)`

##########
File path: cpp/src/gandiva/expression_cache_key.h
##########
@@ -0,0 +1,126 @@
+// 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 <stddef.h>
+
+#include <thread>
+
+#include "arrow/util/hash_util.h"
+#include "gandiva/arrow.h"
+#include "gandiva/configuration.h"
+#include "gandiva/expression.h"
+#include "gandiva/selection_vector.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+class ExpressionCacheKey {
+ public:
+  ExpressionCacheKey(SchemaPtr schema, std::shared_ptr<Configuration> 
configuration,
+                     ExpressionVector expression_vector, SelectionVector::Mode 
mode,
+                     std::string type)
+      : type_(type),
+        schema_(schema),
+        mode_(mode),
+        uniqifier_(0),
+        configuration_(configuration) {
+    static const int kSeedValue = 4;
+    size_t result = kSeedValue;
+    for (auto& expr : expression_vector) {
+      std::string expr_as_string = expr->ToString();
+      expressions_as_strings_.push_back(expr_as_string);
+      arrow::internal::hash_combine(result, expr_as_string);
+      UpdateUniqifier(expr_as_string);
+    }
+    arrow::internal::hash_combine(result, static_cast<size_t>(mode));
+    arrow::internal::hash_combine(result, configuration->Hash());
+    arrow::internal::hash_combine(result, schema_->ToString());
+    arrow::internal::hash_combine(result, uniqifier_);
+    hash_code_ = result;
+  }
+
+  ExpressionCacheKey(SchemaPtr schema, std::shared_ptr<Configuration> 
configuration,
+                     Expression& expression, std::string type)
+      : type_(type), schema_(schema), uniqifier_(0), 
configuration_(configuration) {
+    static const int kSeedValue = 4;
+    size_t result = kSeedValue;
+    expressions_as_strings_.push_back(expression.ToString());
+    UpdateUniqifier(expression.ToString());
+
+    arrow::internal::hash_combine(result, configuration->Hash());
+    arrow::internal::hash_combine(result, schema_->ToString());
+    arrow::internal::hash_combine(result, uniqifier_);
+    hash_code_ = result;
+  }
+
+  void UpdateUniqifier(const std::string& expr) {
+    if (uniqifier_ == 0) {
+      // caching of expressions with re2 patterns causes lock contention. So, 
use
+      // multiple instances to reduce contention.
+      if (expr.find(" like(") != std::string::npos) {
+        uniqifier_ = std::hash<std::thread::id>()(std::this_thread::get_id()) 
% 16;
+      }
+    }
+  }
+
+  size_t Hash() const { return hash_code_; }
+
+  std::string Type() const { return type_; }
+
+  bool operator==(const ExpressionCacheKey& other) const {
+    if (type_ != other.type_) {
+      return false;
+    }
+
+    if (hash_code_ != other.hash_code_) {
+      return false;
+    }
+
+    if (!(schema_->Equals(*other.schema_, true))) {
+      return false;
+    }
+
+    if (configuration_ != other.configuration_) {
+      return false;
+    }
+
+    if (expressions_as_strings_ != other.expressions_as_strings_) {
+      return false;
+    }
+
+    if (uniqifier_ != other.uniqifier_) {
+      return false;
+    }
+
+    return true;
+  }
+
+  bool operator!=(const ExpressionCacheKey& other) const { return !(*this == 
other); }
+
+ private:
+  size_t hash_code_;
+  std::string type_;

Review comment:
       use enum for type

##########
File path: cpp/src/gandiva/filter.cc
##########
@@ -102,14 +46,30 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr 
condition,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
-  static Cache<FilterCacheKey, std::shared_ptr<Filter>> cache;
-  FilterCacheKey cache_key(schema, configuration, *(condition.get()));
-  auto cachedFilter = cache.GetModule(cache_key);
-  if (cachedFilter != nullptr) {
-    *filter = cachedFilter;
-    return Status::OK();
+  std::shared_ptr<Cache<ExpressionCacheKey, 
std::shared_ptr<llvm::MemoryBuffer>>>
+      shared_cache = LLVMGenerator::GetCache();
+
+  Condition conditionToKey = *(condition.get());
+
+  ExpressionCacheKey cache_key(schema, configuration, conditionToKey, 
"filter");
+  std::unique_ptr<ExpressionCacheKey> base_cache_key =
+      std::make_unique<ExpressionCacheKey>(cache_key);
+  std::shared_ptr<ExpressionCacheKey> shared_base_cache_key = 
std::move(base_cache_key);
+
+  bool llvm_flag = false;

Review comment:
       nit: use a better name like "is_cached" or something

##########
File path: cpp/src/gandiva/filter.cc
##########
@@ -102,14 +46,30 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr 
condition,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
-  static Cache<FilterCacheKey, std::shared_ptr<Filter>> cache;
-  FilterCacheKey cache_key(schema, configuration, *(condition.get()));
-  auto cachedFilter = cache.GetModule(cache_key);
-  if (cachedFilter != nullptr) {
-    *filter = cachedFilter;
-    return Status::OK();
+  std::shared_ptr<Cache<ExpressionCacheKey, 
std::shared_ptr<llvm::MemoryBuffer>>>
+      shared_cache = LLVMGenerator::GetCache();
+
+  Condition conditionToKey = *(condition.get());
+
+  ExpressionCacheKey cache_key(schema, configuration, conditionToKey, 
"filter");
+  std::unique_ptr<ExpressionCacheKey> base_cache_key =
+      std::make_unique<ExpressionCacheKey>(cache_key);
+  std::shared_ptr<ExpressionCacheKey> shared_base_cache_key = 
std::move(base_cache_key);

Review comment:
       directly create using std::make_shared instead of in three steps

##########
File path: cpp/src/gandiva/llvm_generator.h
##########
@@ -240,7 +269,7 @@ class GANDIVA_EXPORT LLVMGenerator {
   void AddTrace(const std::string& msg, llvm::Value* value = NULLPTR);
 
   std::unique_ptr<Engine> engine_;
-  std::vector<std::unique_ptr<CompiledExpr>> compiled_exprs_;
+  std::vector<std::shared_ptr<CompiledExpr>> compiled_exprs_;

Review comment:
       why this change?

##########
File path: cpp/src/gandiva/cache.h
##########
@@ -39,17 +39,21 @@ class Cache {
 
   Cache() : Cache(GetCapacity()) {}
 
-  ValueType GetModule(KeyType cache_key) {
+  ::std::shared_ptr<Cache> create(size_t capacity) {
+    return ::std::make_shared<Cache>(cache_(capacity));

Review comment:
       Looks like this method is not getting used. You can remove it.

##########
File path: cpp/src/gandiva/expression_cache_key.h
##########
@@ -0,0 +1,126 @@
+// 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 <stddef.h>
+
+#include <thread>
+
+#include "arrow/util/hash_util.h"
+#include "gandiva/arrow.h"
+#include "gandiva/configuration.h"
+#include "gandiva/expression.h"
+#include "gandiva/selection_vector.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+class ExpressionCacheKey {
+ public:
+  ExpressionCacheKey(SchemaPtr schema, std::shared_ptr<Configuration> 
configuration,
+                     ExpressionVector expression_vector, SelectionVector::Mode 
mode,
+                     std::string type)
+      : type_(type),
+        schema_(schema),
+        mode_(mode),
+        uniqifier_(0),
+        configuration_(configuration) {
+    static const int kSeedValue = 4;
+    size_t result = kSeedValue;
+    for (auto& expr : expression_vector) {
+      std::string expr_as_string = expr->ToString();
+      expressions_as_strings_.push_back(expr_as_string);
+      arrow::internal::hash_combine(result, expr_as_string);
+      UpdateUniqifier(expr_as_string);
+    }
+    arrow::internal::hash_combine(result, static_cast<size_t>(mode));
+    arrow::internal::hash_combine(result, configuration->Hash());
+    arrow::internal::hash_combine(result, schema_->ToString());
+    arrow::internal::hash_combine(result, uniqifier_);
+    hash_code_ = result;
+  }
+
+  ExpressionCacheKey(SchemaPtr schema, std::shared_ptr<Configuration> 
configuration,
+                     Expression& expression, std::string type)
+      : type_(type), schema_(schema), uniqifier_(0), 
configuration_(configuration) {
+    static const int kSeedValue = 4;
+    size_t result = kSeedValue;
+    expressions_as_strings_.push_back(expression.ToString());
+    UpdateUniqifier(expression.ToString());
+
+    arrow::internal::hash_combine(result, configuration->Hash());
+    arrow::internal::hash_combine(result, schema_->ToString());
+    arrow::internal::hash_combine(result, uniqifier_);
+    hash_code_ = result;
+  }
+
+  void UpdateUniqifier(const std::string& expr) {
+    if (uniqifier_ == 0) {
+      // caching of expressions with re2 patterns causes lock contention. So, 
use
+      // multiple instances to reduce contention.
+      if (expr.find(" like(") != std::string::npos) {
+        uniqifier_ = std::hash<std::thread::id>()(std::this_thread::get_id()) 
% 16;
+      }
+    }
+  }
+
+  size_t Hash() const { return hash_code_; }
+
+  std::string Type() const { return type_; }
+
+  bool operator==(const ExpressionCacheKey& other) const {
+    if (type_ != other.type_) {
+      return false;
+    }
+
+    if (hash_code_ != other.hash_code_) {
+      return false;
+    }
+
+    if (!(schema_->Equals(*other.schema_, true))) {
+      return false;
+    }
+
+    if (configuration_ != other.configuration_) {
+      return false;
+    }
+
+    if (expressions_as_strings_ != other.expressions_as_strings_) {
+      return false;
+    }
+
+    if (uniqifier_ != other.uniqifier_) {
+      return false;
+    }
+
+    return true;
+  }
+
+  bool operator!=(const ExpressionCacheKey& other) const { return !(*this == 
other); }
+
+ private:
+  size_t hash_code_;
+  std::string type_;

Review comment:
       on second thought, why is type required? Shouldn't the generated code be 
same for filter and project

##########
File path: cpp/src/gandiva/llvm_generator.h
##########
@@ -49,14 +51,41 @@ class GANDIVA_EXPORT LLVMGenerator {
   static Status Make(std::shared_ptr<Configuration> config,
                      std::unique_ptr<LLVMGenerator>* llvm_generator);
 
-  /// \brief Build the code for the expression trees for default mode. Each
-  /// element in the vector represents an expression tree
-  Status Build(const ExpressionVector& exprs, SelectionVector::Mode mode);
+  static std::shared_ptr<Cache<ExpressionCacheKey, 
std::shared_ptr<llvm::MemoryBuffer>>>
+  GetCache();
+
+  /// \brief Build the code for the expression trees for default mode with a 
LLVM
+  /// ObjectCache. Each element in the vector represents an expression tree
+  template <class KeyType>
+  Status Build(const ExpressionVector& exprs, SelectionVector::Mode mode,
+               GandivaObjectCache<KeyType>& obj_cache) {
+    selection_vector_mode_ = mode;
+
+    for (auto& expr : exprs) {
+      auto output = annotator_.AddOutputFieldDescriptor(expr->result());
+      ARROW_RETURN_NOT_OK(Add(expr, output));
+    }
+
+    engine_->SetLLVMObjectCache(obj_cache);

Review comment:
       I see the only change is this line. How about keeping the earlier method 
as is and adding a new method to llvm_generator call 
SetLLVMObjectCache(obj_cache) which is called by projector/filter before Build()

##########
File path: cpp/src/gandiva/filter.cc
##########
@@ -102,14 +46,30 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr 
condition,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
-  static Cache<FilterCacheKey, std::shared_ptr<Filter>> cache;
-  FilterCacheKey cache_key(schema, configuration, *(condition.get()));
-  auto cachedFilter = cache.GetModule(cache_key);
-  if (cachedFilter != nullptr) {
-    *filter = cachedFilter;
-    return Status::OK();
+  std::shared_ptr<Cache<ExpressionCacheKey, 
std::shared_ptr<llvm::MemoryBuffer>>>
+      shared_cache = LLVMGenerator::GetCache();
+
+  Condition conditionToKey = *(condition.get());
+
+  ExpressionCacheKey cache_key(schema, configuration, conditionToKey, 
"filter");
+  std::unique_ptr<ExpressionCacheKey> base_cache_key =
+      std::make_unique<ExpressionCacheKey>(cache_key);
+  std::shared_ptr<ExpressionCacheKey> shared_base_cache_key = 
std::move(base_cache_key);
+
+  bool llvm_flag = false;
+
+  std::shared_ptr<llvm::MemoryBuffer> prev_cached_obj;
+  prev_cached_obj = shared_cache->GetObjectCode(*shared_base_cache_key);
+
+  // Verify if previous filter obj code was cached
+  if (prev_cached_obj != nullptr) {
+    ARROW_LOG(DEBUG)
+        << "[DEBUG][CACHE-LOG][INFO]: Filter object code WAS already cached!";

Review comment:
       nit: DEBUG and INFO in same message?

##########
File path: cpp/src/gandiva/filter.cc
##########
@@ -102,14 +46,30 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr 
condition,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
-  static Cache<FilterCacheKey, std::shared_ptr<Filter>> cache;
-  FilterCacheKey cache_key(schema, configuration, *(condition.get()));
-  auto cachedFilter = cache.GetModule(cache_key);
-  if (cachedFilter != nullptr) {
-    *filter = cachedFilter;
-    return Status::OK();
+  std::shared_ptr<Cache<ExpressionCacheKey, 
std::shared_ptr<llvm::MemoryBuffer>>>
+      shared_cache = LLVMGenerator::GetCache();
+
+  Condition conditionToKey = *(condition.get());
+
+  ExpressionCacheKey cache_key(schema, configuration, conditionToKey, 
"filter");
+  std::unique_ptr<ExpressionCacheKey> base_cache_key =
+      std::make_unique<ExpressionCacheKey>(cache_key);
+  std::shared_ptr<ExpressionCacheKey> shared_base_cache_key = 
std::move(base_cache_key);

Review comment:
       also you are unnecessary allocating. just use cache_key and make 
GandivaObjectCache can take an object instead of shared_pointer.

##########
File path: cpp/src/gandiva/gandiva_object_cache.h
##########
@@ -0,0 +1,86 @@
+// 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
+
+#if defined(_MSC_VER)
+#pragma warning(push)
+#pragma warning(disable : 4244)
+#pragma warning(disable : 4141)
+#pragma warning(disable : 4146)
+#pragma warning(disable : 4267)
+#pragma warning(disable : 4624)
+#endif
+
+#include <llvm/ExecutionEngine/ObjectCache.h>
+#include <llvm/Support/MemoryBuffer.h>
+
+#include <chrono>
+
+#include "gandiva/cache.h"
+
+namespace gandiva {
+/// Class that enables the LLVM to use a custom rule to deal with the object 
code.
+template <class CacheKey>
+class GandivaObjectCache : public llvm::ObjectCache {
+ public:
+  GandivaObjectCache(
+      std::shared_ptr<Cache<CacheKey, std::shared_ptr<llvm::MemoryBuffer>>>& 
cache,
+      std::shared_ptr<CacheKey>& key) {
+    cache_ = cache;
+    cache_key_ = key;
+    // Start measuring code gen time
+    begin_time_ = std::chrono::high_resolution_clock::now();
+  }
+
+  ~GandivaObjectCache() {}
+
+  void notifyObjectCompiled(const llvm::Module* M, llvm::MemoryBufferRef Obj) {
+    // Stop measuring time and  calculate the elapsed time to compile the 
object code
+    auto end_time = std::chrono::high_resolution_clock::now();
+    auto elapsed_time =
+        std::chrono::duration_cast<std::chrono::milliseconds>(end_time - 
begin_time_)
+            .count();
+
+    std::unique_ptr<llvm::MemoryBuffer> obj_buffer =
+        llvm::MemoryBuffer::getMemBufferCopy(Obj.getBuffer(), 
Obj.getBufferIdentifier());
+    std::shared_ptr<llvm::MemoryBuffer> obj_code = std::move(obj_buffer);
+
+    ValueCacheObject<std::shared_ptr<llvm::MemoryBuffer>> value_cache(
+        obj_code, elapsed_time, obj_code->getBufferSize());
+
+    cache_->PutObjectCode(*cache_key_.get(), value_cache);
+  }
+
+  std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module* M) {
+    std::shared_ptr<llvm::MemoryBuffer> cached_obj =
+        cache_->GetObjectCode(*cache_key_.get());
+    auto null = std::nullptr_t();
+    if (cached_obj != null) {

Review comment:
       just use nullptr




-- 
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]


Reply via email to