Skip to content

Commit

Permalink
Improve ada_url_parse performance (fix #73)
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones authored Jan 13, 2025
1 parent 57679e6 commit b100c28
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: adaR
Title: A Fast 'WHATWG' Compliant URL Parser
Version: 0.3.3
Version: 0.3.3.9999
Authors@R:
c(person("David", "Schoch", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2952-4812")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# adaR 0.3.3.9999

* improved ada_url_parse performance(#73)

# adaR 0.3.3

* * bumped ada-url to 2.9.0
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ practical circumstances.
bench::mark(
ada = ada_url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag", decode = FALSE),
urltools = urltools::url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag"),
iterations = 1, check = FALSE
check = FALSE
)
```

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ practical circumstances.
bench::mark(
ada = ada_url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag", decode = FALSE),
urltools = urltools::url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag"),
iterations = 1, check = FALSE
check = FALSE
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 ada 1.24ms 1.24ms 806. 0B 0
#> 2 urltools 812.7µs 812.7µs 1230. 0B 0
#> 1 ada 2.83µs 3.24µs 286849. 0B 28.7
#> 2 urltools 123.41µs 134.36µs 7277. 0B 36.8
```

For further benchmark results, see `benchmark.md` in `data_raw`.
Expand Down
2 changes: 1 addition & 1 deletion src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// Rcpp_ada_parse
DataFrame Rcpp_ada_parse(const CharacterVector& input_vec, bool decode);
List Rcpp_ada_parse(const CharacterVector& input_vec, bool decode);
RcppExport SEXP _adaR_Rcpp_ada_parse(SEXP input_vecSEXP, SEXP decodeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down
21 changes: 15 additions & 6 deletions src/adaR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ std::string charsub(const ada_string stringi, bool to_unicode = true) {
}

// [[Rcpp::export]]
DataFrame Rcpp_ada_parse(const CharacterVector& input_vec, bool decode) {
List Rcpp_ada_parse(const CharacterVector& input_vec, bool decode) {
unsigned int n = input_vec.length();
CharacterVector href(n);
CharacterVector protocol(n);
Expand All @@ -32,6 +32,7 @@ DataFrame Rcpp_ada_parse(const CharacterVector& input_vec, bool decode) {
CharacterVector pathname(n);
CharacterVector search(n);
CharacterVector hash(n);
Rcpp::IntegerVector row_name(n);
for (unsigned int i = 0; i < n; i++) {
String s = input_vec[i];
std::string_view input(s.get_cstring());
Expand Down Expand Up @@ -60,6 +61,7 @@ DataFrame Rcpp_ada_parse(const CharacterVector& input_vec, bool decode) {
hash[i] = NA_STRING;
}
ada_free(url);
row_name[i] = i + 1;
}
if (decode) {
href = Rcpp_url_decode2(href);
Expand All @@ -73,13 +75,20 @@ DataFrame Rcpp_ada_parse(const CharacterVector& input_vec, bool decode) {
search = Rcpp_url_decode2(search);
hash = Rcpp_url_decode2(hash);
}
return (DataFrame::create(Named("href") = href, _["protocol"] = protocol,
_["username"] = username, _["password"] = password,
_["host"] = host, _["hostname"] = hostname,
_["port"] = port, _["pathname"] = pathname,
_["search"] = search, _["hash"] = hash));

List result = List::create(Named("href") = href, _["protocol"] = protocol,
_["username"] = username, _["password"] = password,
_["host"] = host, _["hostname"] = hostname,
_["port"] = port, _["pathname"] = pathname,
_["search"] = search, _["hash"] = hash);
// as data.frame is expensive - create from the list
result.attr("row.names") = row_name;
result.attr("class") = "data.frame";

return result;
}


// higher-order function for all Rcpp_ada_has_*
LogicalVector Rcpp_ada_has(const CharacterVector& url_vec,
std::function<bool(ada_url)> func) {
Expand Down

0 comments on commit b100c28

Please sign in to comment.