-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat(src/utils): add cache to is_namedtuple
and is_structseq
#121
feat(src/utils): add cache to is_namedtuple
and is_structseq
#121
Conversation
include/utils.h
Outdated
inline bool IsStructSequenceClass(const py::handle& type) { | ||
return PyType_Check(type.ptr()) && IsStructSequenceClassImpl(type); | ||
if (PyType_Check(type.ptr())) [[likely]] { | ||
static auto cache = std::unordered_map<py::handle, bool, TypeHash, TypeEq>{}; | ||
auto it = cache.find(type); | ||
if (it != cache.end()) [[likely]] { | ||
return it->second; | ||
} | ||
bool result = IsStructSequenceClassImpl(type); | ||
cache.emplace(type, result); | ||
(void)py::weakref(type, py::cpp_function([type](py::handle weakref) -> void { | ||
cache.erase(type); | ||
weakref.dec_ref(); | ||
})) | ||
.release(); | ||
return result; | ||
} | ||
return false; | ||
} |
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.
Is this the best way to cache Python objects while cooperating with the Python GC? Could you have a look of this @Skylion007? Thanks!
54a7c56
to
8352ae9
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #121 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 8
Lines 777 777
=========================================
Hits 777 777
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
10ef302
to
6f9c7d6
Compare
edd4ccf
to
f17d905
Compare
d24f48d
to
dd7e0cd
Compare
dd7e0cd
to
05f3825
Compare
314cd17
to
a42ad86
Compare
Description
Describe your changes in detail.
Add a static cache in C++ to cache the result of
is_namedtuple
andis_structseq
. I got about 15% performance gain in the benchmark.Motivation and Context
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax
close #15213
if this solves the issue #15213Types of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply:Checklist
Go over all the following points, and put an
x
in all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!
make format
. (required)make lint
. (required)make test
pass. (required)