-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Remove unnecessary heap allocated copies in Transformation actions #3231
Remove unnecessary heap allocated copies in Transformation actions #3231
Conversation
These results were obtained running the
|
The unit tests for each transformation were executed (using a different number of iterations, as some take more time than others). These results were obtained in a Debian (bullseye) container running on WSL on Windows Dev Kit 2023, and are provided as a reference. (Times in milliseconds. Averages of five runs)
|
1ae67da
to
6efb1cb
Compare
5f3aa66
to
a801f41
Compare
Regenerated PR to divide changes to make transformations in-place into multiple commits to simplify review. The original commit can be found in eduar-hte/ModSecurity@0a2dd1a. NOTE: This required making the first commit in the PR include the changes to the signature of |
de7a953
to
ffcbf27
Compare
ffcbf27
to
7c30fcb
Compare
- Removed inplace helper function from the class, as it's only referenced by the implementation.
…ments - utils::urldecode_nonstrict_inplace decodes inplace so key & value, which are values returned by utils::string::ssplit_pair can be just be modified and do not need to be copied. - Updated signature of utils::urldecode_nonstrict_inplace, as its two callers already have std::string values.
- This function already expects these arguments not to be null pointers, doesn't validate them and just dereference them. - In order to make this explicit and enforced by the compiler, they're now passed as references.
- Some of the Transformation classes would initialize their Action's action_kind using the default (using Transformation constructor without an action_kind parameter). - Others, however, would use that constructor and initialize action_kind manually in their constructor, but setting the default value (RunTimeBeforeMatchAttemptKind = 1), which was redundant. - Removed unused Transformation constructor to specify action_kind. - Converted Action::Kind into an 'enum class' to require using the enum constants (instead of integer values, which are difficult to track in the codebase and change)
- Moved them into modsecurity::utils::string to avoid polluting the global namespace.
b6eb3cb
to
a6d64bf
Compare
I've just pushed an update (integrating these changes into the corresponding existing commits) to address these. NOTE: I'm updating the comment to reflect that Sonarcloud quickly updated its findings, but still reports issues in code that is no longer there (!). This happened to me a couple of times last week while I was in the process of addressing other issues. It seems that the scan is sometimes done in stale code, so it's not accurate. I had to wait for a while and then generate a new push for the scan to be accurate. If you could re-run the scan in an hour (or two), I think we should see the 5 issues above go away. |
|
Yes, I knew that - I just asked you about this to know what do you think in long term. Thank you for all explanations - we discussed the Sonarcloud issues with @gberkes and we agreed that we have to review all issues (not just these ones) later, and fix them by category. Anyway, these summaries are awesome here and everyone can see that these are not new issues. Thanks again, and thanks for the great PR. I'm going to merge this now. |
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
- These were added to test changes in owasp-modsecurity/ModSecurity#3231
Submitted PR owasp-modsecurity/secrules-language-tests#10 to add more tests for the |
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
…stions - The following two warnings were generated after introducing the change to instantiate the DigestImpl template with the address of mbedtls_md5 or mbedtls_sha1: - warning: src/utils/sha1.h,62,error,danglingTemporaryLifetime,Using pointer that is a temporary. - warning: src/utils/sha1.h,60,style,constVariablePointer,Variable 'ret' can be declared as pointer to const - See owasp-modsecurity#3231 (comment)
what
Remove unnecessary heap allocated copies in most
Transformation
classes.why
The current implementation of most
Transformation
classes are copying thestd::string
values in order to modify them and then return them. This can be optimized by updating the signature ofTransformation::evaluate
to receive a reference of the value to be modified, and if the transformation allows it, modify it in-place.This change increases performance of the
benchmark
test by about 11%-12,5%.changes
std::string
buffer of the value where the transformation is being applied, instead of copying to a temporary (heap-allocated) string.Transformation::evaluate
signature to receive the value by reference and perform the transformation inplace, if possible (some transformations cannot be performed in place because they need a bigger buffer to produce the new value, such asbase64
encoding).RuleWithActions::executeTransformation
to compare the original and transformed value further improving performance.changed
bool flag in their implementation). Some transformations always transform their input value, so they just returntrue
. Notably, a few of the transformations had implementations that would compute achanged
flag, but that was currently unused.references
While working on updating the configuration to build on C++17 (and C++20), I found out through this comment that a PR to make the transformations in-place was submitted before in PR #2368 and was integrated into the v3.1 branch (though curiously the changes are not currently included in that branch).
This PR goes further in the review of the different in-place transformations to simplify and make their implementations more consistent and efficient. Additionally, because of the signature change of
Transformation::evaluate
included in this PR, an allocation and copy is avoided.misc
This is part of a series of PRs to improve performance of the library (4/n). Previous: #3222