Copilot commented on code in PR #861: URL: https://github.com/apache/incubator-graphar/pull/861#discussion_r2867118392
########## cpp/src/graphar/lru_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 + +#include <cstddef> +#include <list> +#include <unordered_map> +#include <utility> + +namespace graphar { + +template <typename Key, typename Value, typename Hash = std::hash<Key>> +class LRUCache { + public: + explicit LRUCache(size_t capacity) : capacity_(capacity) {} + + Value* Get(const Key& key) { + auto it = map_.find(key); + if (it == map_.end()) { + return nullptr; + } + items_.splice(items_.begin(), items_, it->second); + return &it->second->second; + } + + void Put(const Key& key, Value value) { + auto it = map_.find(key); + if (it != map_.end()) { + it->second->second = std::move(value); + items_.splice(items_.begin(), items_, it->second); + return; + } + if (capacity_ == 0) { + return; + } + if (map_.size() >= capacity_) { + auto& back = items_.back(); + map_.erase(back.first); + items_.pop_back(); + } + items_.emplace_front(key, std::move(value)); + map_[key] = items_.begin(); + } + + void Clear() { + map_.clear(); + items_.clear(); + } + + size_t Size() const { return map_.size(); } + + private: + size_t capacity_; + std::list<std::pair<Key, Value>> items_; + std::unordered_map<Key, typename std::list<std::pair<Key, Value>>::iterator, + Hash> + map_; +}; + +struct PairHash { + template <typename T1, typename T2> + size_t operator()(const std::pair<T1, T2>& p) const { + auto h1 = std::hash<T1>{}(p.first); + auto h2 = std::hash<T2>{}(p.second); + return h1 ^ (h2 << 32); Review Comment: `PairHash` combines hashes via `h1 ^ (h2 << 32)`. Shifting by 32 is undefined behavior when `size_t` is 32-bit, and even on 64-bit this is a weak combiner (high collision risk). Please switch to a portable hash-combine that doesn't assume `size_t` width (e.g., `h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1<<6) + (h1>>2))` or an equivalent width-agnostic approach). ```suggestion size_t h1 = std::hash<T1>{}(p.first); size_t h2 = std::hash<T2>{}(p.second); // Width-agnostic hash combine (inspired by boost::hash_combine) constexpr size_t kMul = static_cast<size_t>(0x9e3779b97f4a7c15ULL); h1 ^= h2 + kMul + (h1 << 6) + (h1 >> 2); return h1; ``` ########## cpp/src/graphar/lru_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 + +#include <cstddef> +#include <list> +#include <unordered_map> +#include <utility> + +namespace graphar { + +template <typename Key, typename Value, typename Hash = std::hash<Key>> +class LRUCache { + public: Review Comment: `LRUCache` uses `std::hash<Key>` as the default `Hash` template parameter but this header doesn't include `<functional>`. Relying on transitive includes from `<unordered_map>` is non-portable and can break builds on some standard library implementations; please include `<functional>` explicitly here. ########## cpp/src/graphar/arrow/chunk_reader.cc: ########## @@ -1024,6 +1051,8 @@ AdjListPropertyArrowChunkReader::GetChunk() { GAR_RETURN_NOT_OK( CastTableWithSchema(chunk_table_, schema_, &chunk_table_)); } + chunk_cache_.Put(std::make_pair(vertex_chunk_index_, chunk_index_), + chunk_table_); } Review Comment: `AdjListPropertyArrowChunkReader` caches `chunk_table_` only by `(vertex_chunk_index_, chunk_index_)`, but the actual table content/shape depends on `filter_options_` (Filter/Select). If Filter/Select is changed and the reader returns to an already-cached chunk, it may return stale results from the previous filter/projection. Please invalidate the cache on Filter/Select changes or incorporate the effective filter/projection into the cache key. ########## cpp/src/graphar/arrow/chunk_reader.cc: ########## @@ -260,6 +259,7 @@ VertexPropertyArrowChunkReader::GetChunkV2() { GAR_RETURN_NOT_OK( CastTableWithSchema(chunk_table_, schema_, &chunk_table_)); } + chunk_cache_.Put(chunk_index_, chunk_table_); } Review Comment: `VertexPropertyArrowChunkReader` now caches `chunk_table_` only by `chunk_index_`, but the loaded table also depends on `filter_options_` (Filter/Select) and `property_names_` / selected columns. If a caller changes Filter/Select and seeks back to a previously-read chunk, the cache can return a table produced with the old options, which is a behavior regression versus reloading. Please either (a) include the effective filter/projection in the cache key, or (b) clear/invalidate `chunk_cache_` (and `chunk_table_`) whenever Filter/Select options change. -- 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]
