Skip to content
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

Simplified constructors, copy constructors & assignment operators #3248

Merged

Conversation

eduar-hte
Copy link
Contributor

@eduar-hte eduar-hte commented Sep 3, 2024

what

Several constructors, copy constructors & assignment operators in the codebase can be simplified or even removed because they're not used.

why

Simplify the codebase making it easier to maintain.

NOTE: In the case of RuleMessage, it addresses one of the FIXME in the codebase, see here

misc

This is part of a series of PRs to improve performance of the library (5/n). Previous: #3231

@eduar-hte
Copy link
Contributor Author

eduar-hte commented Sep 3, 2024

Failed conditions 8.8% Duplication on New Code (required ≤ 3%)

The tool is reporting duplicate code on changes to data the initialization of data members that were simplified (in src/transaction.cc).

The other two files mentioned are examples that use the same code to log rules (this PR only adjusts the existing lines to update how the data is retrieved). In library code (let's say, src dir) I'd obviously favor refactoring the code, but in the case of examples, it makes sense that they're independent to make it easier to use them as a simple reference and starting point for library users.

This PR is actually about removing code, but Sonarcloud will Sonarcloud...😊

@eduar-hte
Copy link
Contributor Author

eduar-hte commented Sep 3, 2024

The tool is reporting duplicate code on changes to data the initialization of data members that were simplified (in src/transaction.cc).

I didn't look into updating the Transaction constructors in this PR, because these changes originated in those to simplify RuleMessage. These commits were part of another branch where use of RuleMessage is simplified, but I wanted to send them in a different PR to limit the scope of changes, as we've previously discussed.

PS: I'll take a look at the Transaction constructors and see if they can be simplified as well. They actually look identical, other than the id parameter, so they could be refactor to use a delegating constructor.

@eduar-hte
Copy link
Contributor Author

PS: I'll take a look at the Transaction constructors and see if they can be simplified as well. They actually look identical, other than the id parameter, so they could be refactor to use a delegating constructor.

Done

PS: Notice that Sonarcloud once again run on stale code after the push, and is still showing the old version of transaction.cc and thus still flags duplicate code (in code that no longer exists). ¯_(ツ)_/¯

…ker & Action

- Declare other unsupported copy constructor & assignment operators as
  deleted too (RuleWithActions, RuleUnconditional & RuleScript)
…ues in RuleMessage

- Because the lifetime of the RuleMessage instances do not extend beyond
  the lifetime of the enclosing RuleWithActions & Transaction,
  RuleMessage can just reference it and simplify its definition.
- Additionally, make the references const to show that it doesn't modify it.
- Replace RuleMessage copy constructor with default implementations.
- Removed unused RuleMessage assignment operator (which cannot be implemented
  now that it has reference members).
- Removed constructor from RuleMessage pointer.
- Addressed Sonarcloud suggestions: Do not use the constructor's
  initializer list for data member "xxx". Use the in-class initializer
  instead.
@eduar-hte
Copy link
Contributor Author

Issues 17 New issues 0 Accepted issues

Edited commits to address the following issues reported by Sonarcloud:

Did not address:

- Leverage delegating constructor to avoid code duplication between the
  two available Transaction constructors.
  - The constructor without 'id' argument delegates to the one that
    receives it by providing `nullptr` as a value, which is used to
    flag that an id needs to be generated.
- Simplified constructor by removing member initialization where the
  default constructor will be invoked.
Copy link

sonarcloud bot commented Sep 4, 2024

@eduar-hte
Copy link
Contributor Author

eduar-hte commented Sep 5, 2024

This is part of a series of PRs to improve performance of the library (5/n). Previous: #3231

Even though the goal of the PR is to simplify the codebase, commit 2ad87f6 (Reference RuleWithActions & Transaction object instead of copying values in RuleMessage) provides some performance improvements because it removes a number of variables copied from the associated rule & transaction.

These results were obtained running the benchmark test with 100'000 iterations with the OWASP CRS v4 and are presented as a reference:

  • Mac mini (1st generation)
    • v3/master: 83.44 secs
    • This PR: 81.44 secs (2.4% improvement)
  • Debian (bullseye) container running on WSL on Windows Dev Kit 2023
    • v3/master: 121.83 secs
    • This PR: 117.45 secs (3.59% improvement)

@airween
Copy link
Member

airween commented Sep 9, 2024

Hi @eduar-hte

thanks again for this great PR!

Just one reminder to myself:

PS: I'll take a look at the Transaction constructors and see if they can be simplified as well. They actually look identical, other than the id parameter, so they could be refactor to use a delegating constructor.

Seems like the Transaction class public constructors are (not) a bit different that it documented in README.md:

Transaction *modsecTransaction = new Transaction(modsec, rules);

or in source documentation, so we need to align them.

@airween airween merged commit 9e02b3c into owasp-modsecurity:v3/master Sep 9, 2024
49 checks passed
@eduar-hte eduar-hte deleted the simplified-constructors branch September 9, 2024 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants