-
Notifications
You must be signed in to change notification settings - Fork 12
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
Draft: Copy OpenCV's allocator #15
Conversation
Added a new compilation option (CVNP_ENABLE_ASAN), with which one shall run test_cvnp_cpp
cvnp/cvnp.cpp
Outdated
m.u = detail::g_cvnpAllocator.allocate(a); //, ndims, size, type, step); | ||
m.allocator = &detail::g_cvnpAllocator; | ||
|
||
// this doesn't leak, but I don't know why |
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.
I did check to see that the deallocator is eventually called. Things break if this addref isn't here.
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.
See note where I suggest to add a line u->refcount = 1
inside allocate
Hi, Many thanks for your proposition! It is indeed easier to read and maintain than mine, and also leads to less allocations, which is good! Notes: I instrumented the code to be able to follow the number of allocations (and also which branches are taken), here: I also changed a bit the style (new line before {, in order to be consistent with the rest of the file). I don't know if we will keep this when merging the final version (although it costs nothing in production), but it is interesting nonetheless to follow what happens. Could you merge this in your branch (even if we remove it in the end).
I did test your patch inside Dear ImGui Bundle. It seems to work perfectly, and the number of allocations drops down to zero after lots of usage of cvnp. ImGui Bundle is another C++/Python library which I developed. You can access an online C++ emscripten demo here: cvnp is heavily used in the demos inside the "ImmVision" tab (when running the python demos, of course) Based on my observation when used from python, those branches seem to also never be taken: void deallocate(cv::UMatData* u) const override
{
if(!u)
{
// We never reach here. We may perhaps want to throw
#ifdef DEBUG_ALLOCATOR
printf("CvnpAllocator::deallocate() with null ptr!!! nbAllocations=%d\n", nbAllocations);
#endif
return;
}
py::gil_scoped_acquire gil;
assert(u->urefcount >= 0);
assert(u->refcount >= 0);
if(u->refcount == 0)
{
PyObject* o = (PyObject*)u->userdata;
Py_XDECREF(o);
delete u;
#ifdef DEBUG_ALLOCATOR
--nbAllocations;
printf("CvnpAllocator::deallocate() nbAllocations=%d\n", nbAllocations);
#endif
}
else
{
// We never reach here. I think it is related to the fact that allocate(cv::UMatData* u, ...) is also never reached
// I don't know if is reasonable to throw here or to leave as it is (IMHO: leave it as it is)
#ifdef DEBUG_ALLOCATOR
++nbAllocations;
printf("CvnpAllocator::deallocate() - not doing anything since urefcount=%d nbAllocations=%d\n",
u->urefcount,
nbAllocations);
#endif
}
} |
cvnp/cvnp.cpp
Outdated
m.u = detail::g_cvnpAllocator.allocate(a); //, ndims, size, type, step); | ||
m.allocator = &detail::g_cvnpAllocator; | ||
|
||
// this doesn't leak, but I don't know why |
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.
See note where I suggest to add a line u->refcount = 1
inside allocate
I merged your changes. Excellent job, thanks a lot! |
Simpler version of fix for #13 , but I've only tested it locally in my copy of cvnp.