Skip to content

Commit

Permalink
DX-24414: Make gandiva cache size configurable (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
projjal authored Aug 7, 2020
1 parent 5598bc6 commit 53d3935
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 25 additions & 2 deletions cpp/src/gandiva/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include <cstdlib>
#include <iostream>
#include <mutex>

#include "gandiva/lru_cache.h"
Expand All @@ -26,7 +28,12 @@ namespace gandiva {
template <class KeyType, typename ValueType>
class Cache {
public:
explicit Cache(size_t capacity = CACHE_SIZE) : cache_(capacity) {}
explicit Cache(size_t capacity) : cache_(capacity) {
std::cout << "Creating gandiva cache with capacity: " << capacity << std::endl;
}

Cache() : Cache(GetCapacity()) {}

ValueType GetModule(KeyType cache_key) {
arrow::util::optional<ValueType> result;
mtx_.lock();
Expand All @@ -42,8 +49,24 @@ class Cache {
}

private:
static int GetCapacity() {
int capacity;
const char* env_cache_size = std::getenv("GANDIVA_CACHE_SIZE");
if (env_cache_size != nullptr) {
capacity = std::atoi(env_cache_size);
if (capacity <= 0) {
std::cout << "Invalid cache size provided. Using default cache size."
<< std::endl;
capacity = DEFAULT_CACHE_SIZE;
}
} else {
capacity = DEFAULT_CACHE_SIZE;
}
return capacity;
}

LruCache<KeyType, ValueType> cache_;
static const int CACHE_SIZE = 250;
static const int DEFAULT_CACHE_SIZE = 500;
std::mutex mtx_;
};
} // namespace gandiva
2 changes: 2 additions & 0 deletions cpp/src/gandiva/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <list>
#include <iostream>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -107,6 +108,7 @@ class LruCache {

private:
void evict() {
std::cout << "Evicted a cache item from the lru cache" << std::endl;
// evict item from the end of most recently used list
typename list_type::iterator i = --lru_list_.end();
map_.erase(*i);
Expand Down

0 comments on commit 53d3935

Please sign in to comment.