Skip to content

Commit 63a583f

Browse files
refactor(urlparser): compute suffix position via arithmetic
Improve performance by directly calculating the '.' position using size arithmetic, avoiding string concatenation and rfind call. Also removes unnecessary temporary allocation.
1 parent 7cfeebc commit 63a583f

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/urlparser.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,14 @@ void urlparser::hostname::ensure_parsed() const noexcept {
857857

858858
fulldomain_ = host_;
859859
suffix_ = urlparser::psl::instance().suffix_of(host_);
860-
size_t suffix_pos = fulldomain_.rfind("." + suffix_);
860+
// suffix_ is, by construction, always the tail of fulldomain_ (suffix_of()
861+
// matches against a reversed copy of host_, so whatever it returns is
862+
// exactly the trailing suffix.size() characters). So the position of the
863+
// '.' right before it is a direct arithmetic offset - no need to
864+
// allocate "." + suffix_ and rfind() it back out of fulldomain_.
865+
size_t suffix_pos = (suffix_.size() < fulldomain_.size())
866+
? fulldomain_.size() - suffix_.size() - 1
867+
: std::string::npos;
861868
size_t subdomain_pos = 0;
862869
if (suffix_pos == std::string::npos || suffix_pos < 1) return;
863870

0 commit comments

Comments
 (0)