casbin / casbin-cpp

An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++
https://casbin.org
Apache License 2.0
218 stars 61 forks source link

feat: fix memory leak in Enforce() #246

Closed ailiujiarui closed 2 months ago

ailiujiarui commented 2 months ago

Fix: https://github.com/casbin/casbin-cpp/issues/243

Description

When the enforce function is called 10,000 times, the memory stays at 3MB for a while before it linearly increases to around 40MB. Using the performance analysis tool built into VS2022, snapshots were taken during the program’s runtime to compare the differences in memory and call counts. Clicking on the snapshot allows you to see which functions were called in the stack and what objects exist. After repeated comparisons, it was found that after the number of calls to GetExprtkFunction increased, many smart pointer objects were created and not released in time, causing the memory to continue to rise. I modified the function’s running logic, using an object pool to store the required objects and returning objects as needed. After completion, the memory occupied by the same number of calls dropped to 4~5MB.

Screenshots/Testimonials

before: image

now: image

casbin-bot commented 2 months ago

@EmperorYP7 @sheny1xuan @cs1137195420 please review

hsluoyz commented 2 months ago

Hi @fishsclue, can you test this PR?

github-actions[bot] commented 2 months ago

:tada: This PR is included in version 1.57.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: