diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-07-15 14:31:01 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-07-15 14:31:08 +0200 |
commit | 78c1a2db7c42b7d2cd5a036a5ef19bb95c679b86 (patch) | |
tree | e21d13fe176061825b7d6335831a9ae3795ae70b /lib | |
parent | 24a206f6429f6fa29b9aa1ce39b7e4694e39b046 (diff) | |
download | libquotient-78c1a2db7c42b7d2cd5a036a5ef19bb95c679b86.tar.gz libquotient-78c1a2db7c42b7d2cd5a036a5ef19bb95c679b86.zip |
Connection::user(): validate after lookup, not before
If userMap only holds valid ids, there's no reason to spend time
validating the sought id: if it's invalid, it won't be found. And
lookups over a hash map are cheap.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index 701f78c2..81151135 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -1456,12 +1456,14 @@ User* Connection::user(const QString& uId) { if (uId.isEmpty()) return nullptr; + if (const auto v = d->userMap.value(uId, nullptr)) + return v; + // Before creating a user object, check that the user id is well-formed + // (it's faster to just do a lookup above before validation) if (!uId.startsWith('@') || serverPart(uId).isEmpty()) { qCCritical(MAIN) << "Malformed userId:" << uId; return nullptr; } - if (d->userMap.contains(uId)) - return d->userMap.value(uId); auto* user = userFactory()(this, uId); d->userMap.insert(uId, user); emit newUser(user); |