-
Notifications
You must be signed in to change notification settings - Fork 0
[Fix] Resolve cache obj double free #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @haochengxia, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request primarily focuses on resolving a critical memory management issue within the libCacheSim
Python bindings, specifically preventing potential double-free scenarios for cache objects. Beyond this crucial fix, it significantly enhances the project's build system to support new, optional cache algorithms (like GLCACHE, LRB, and 3LCache) and introduces a robust, comprehensive test suite to ensure the correctness and stability of the cache implementations.
Highlights
- Memory Management Fix: Addressed potential double-free issues for cache objects returned from C++ to Python. This was achieved by explicitly managing object lifetimes using
py::return_value_policy::reference
andstd::unique_ptr
within the Pybind11 bindings for methods likefind
,insert
,to_evict
, and object creation functions. - New Cache Feature Integration: Integrated support for optional
GLCACHE
,LRB
, and3L_CACHE
algorithms. This involved adding new CMake options, configuring the build system to find and link necessary external dependencies like XGBoost and LightGBM conditionally, and updating the Python API to expose relevant parameters. - Enhanced Build Process: Streamlined the project's build process by ensuring the
libCacheSim
submodule is always rebuilt with the correct configurations. Redundant manual build steps were removed from theinstall.sh
script, centralizing the build logic withinCMakeLists.txt
. - Comprehensive Test Suite: Introduced a new, extensive test suite (
tests/test_cache.py
) utilizingpytest
. This suite validates the functionality, edge cases, statistics, and operations of various cache algorithms, including both core and newly integrated optional ones, ensuring robust behavior. - Python API Refinements: Updated Python cache class parameters and docstrings in
libcachesim/cache.py
(e.g., forLeCaR
,ClockPro
,LRUProb
) to align with underlying C++ changes and new feature capabilities, improving consistency and usability.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request addresses a critical memory management issue by implementing py::return_value_policy::reference
and std::unique_ptr
with custom deleters in src/export_cache.cpp
. It also introduces a comprehensive test suite in tests/test_cache.py
and streamlines the build process with changes in CMakeLists.txt
and pyproject.toml
. The changes improve the stability, maintainability, and test coverage of the project.
CMakeLists.txt
Outdated
find_package(xgboost REQUIRED) | ||
include_directories(${XGBOOST_INCLUDE_DIR}) | ||
list(APPEND optional_dependency_libs xgboost::xgboost) | ||
add_compile_definitions(ENABLE_GLCACHE=1) | ||
message(STATUS "XGBOOST_INCLUDE_DIR=${XGBOOST_INCLUDE_DIR}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a check to ensure that XGBOOST_INCLUDE_DIR is not already defined before setting it. This can prevent potential conflicts if the variable is defined elsewhere.
if(ENABLE_GLCACHE)
find_package(xgboost REQUIRED)
if(NOT DEFINED XGBOOST_INCLUDE_DIR)
include_directories(${XGBOOST_INCLUDE_DIR})
endif()
list(APPEND optional_dependency_libs xgboost::xgboost)
add_compile_definitions(ENABLE_GLCACHE=1)
message(STATUS "XGBOOST_INCLUDE_DIR=${XGBOOST_INCLUDE_DIR}")
endif()
@@ -360,7 +362,7 @@ def __init__( | |||
update_weight: bool = True, | |||
lru_weight: float = 0.5, | |||
): | |||
cache_specific_params = f"update-weight={update_weight}, lru-weight={lru_weight}" | |||
cache_specific_params = f"update-weight={int(update_weight)}, lru-weight={lru_weight}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to see the explicit conversion of update_weight
to an integer. This ensures that the value passed to the underlying C++ code is of the correct type. Without this conversion, there could be unexpected behavior or errors due to type mismatches.
cache_specific_params = f"update-weight={int(update_weight)}, lru-weight={lru_weight}"
@@ -1,13 +1,7 @@ | |||
git submodule update --init --recursive | |||
# Sync submodules | |||
git submodule update --recursive --remote |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a check to ensure that the git submodule update command succeeds. This can be done by checking the exit code of the command and exiting the script with an error message if it fails.
git submodule update --recursive --remote | |
git submodule update --recursive --remote | |
if [ $? -ne 0 ]; then | |
echo "Error: git submodule update failed" | |
exit 1 | |
fi |
cache_obj_t* obj = self.find(&self, &req, update_cache); | ||
return py::cast(obj, py::return_value_policy::reference); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using py::cast
with py::return_value_policy::reference
is the correct way to return a reference to a C++ object from a pybind11 function. This ensures that the Python object holds a reference to the C++ object, and that the C++ object is not deleted while the Python object is still alive.
cache_obj_t* obj = self.find(&self, &req, update_cache); | |
return py::cast(obj, py::return_value_policy::reference); | |
cache_obj_t* obj = self.find(&self, &req, update_cache); | |
return py::cast(obj, py::return_value_policy::reference); |
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition self.get_occupied_byte(&self) + req.obj_size > self.cache_size
is used to determine if eviction is needed. This is a good optimization to avoid unnecessary eviction attempts when there is enough space in the cache.
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size; | |
}, | |
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size; |
No description provided.