Skip to content

Commit

Permalink
Make UUID validation RegEx a static class member
Browse files Browse the repository at this point in the history
When analyzing a callgrind profile of a full library rescan, I noticed
that there are a lot of CPU instructions coming from libpcre2, with the
call originating in the UUID validation code.

Previously, the regular expression used for UUID validation was parsed
from a string for every invocation of the validation function. This is
unnecessary. A library rescan will validate thousands of UUIDs (roughly
250k calls to `Uuid::isValid` with 200'000 instructions per call on my
laptop for a library rescan). By moving the code to a static class
member, I achieved a 15% speedup for a full library scan.
  • Loading branch information
dbrgn committed Jan 23, 2020
1 parent 16bdf52 commit 2df00b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions libs/librepcb/common/uuid.cpp
Expand Up @@ -30,16 +30,22 @@
******************************************************************************/
namespace librepcb {

/*******************************************************************************
* Static Members
******************************************************************************/

// Initialize validation regex
const QRegularExpression Uuid::mValidationRe(
"\\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\z");

/*******************************************************************************
* Static Methods
******************************************************************************/

bool Uuid::isValid(const QString& str) noexcept {
// check format of string (only accept EXACT matches!)
QRegularExpression re(
"\\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\z");
QRegularExpressionMatch match =
re.match(str, 0, QRegularExpression::PartialPreferCompleteMatch);
QRegularExpressionMatch match = mValidationRe.match(
str, 0, QRegularExpression::PartialPreferCompleteMatch);
if (!match.hasMatch()) return false;

// check type of uuid
Expand Down
5 changes: 5 additions & 0 deletions libs/librepcb/common/uuid.h
Expand Up @@ -158,6 +158,11 @@ class Uuid final {

private: // Data
QString mUuid; ///< Guaranteed to always contain a valid UUID

/**
* @brief mRe Regular expression for validating UUIDs.
*/
static const QRegularExpression mValidationRe;
};

/*******************************************************************************
Expand Down

0 comments on commit 2df00b0

Please sign in to comment.