Skip to content
ada-urlPublic

About

Rust bindings for Ada URL parser

Topics

Resources

Stars

123 stars

Watchers

4 watching

Forks

Repository files navigation

WHATWG URL parser for Rust

Fast WHATWG URL Specification compliant URL parser for Rust. Well-tested and widely used by Node.js since Node 18.

The Ada library passes the full range of tests from the specification, across a wide range of platforms (e.g., Windows, Linux, macOS). It fully supports the relevant Unicode Technical Standard.

Usage

See here for a usage example. You can run it locally with cargo run --example simple. Feel free to adjust it for exploring this crate further.

Features

std: Functionalities that require std. This feature is enabled by default, set no-default-features to true if you want no-std.

serde: Allow Url to work with serde. This feature is disabled by default. Enabling this feature without std would provide you only Serialize. Enabling this feature and std would provide you both Serialize and Deserialize.

libcpp: Build ada-url with libc++. This feature is disabled by default. Enabling this feature without libc++ installed would cause compile error.

bundled: Compile and statically link the bundled C++ ada sources. This feature is enabled by default. See "Linking against an external ada" below for how to disable it.

Linking against an external ada

By default (the bundled feature) the build script compiles the bundled C++ ada sources and links them statically. If ada is already provided by your host build system (e.g. Bazel, CMake, or a distribution package), compiling the bundled copy would produce a second, duplicate copy of ada in the final binary.

Disable the bundled feature to skip the bundled build and link against an external ada instead:

# Cargo.toml — re-enable `std` since disabling default features also drops it
ada-url = { version = "4", default-features = false, features = ["std"] }

When bundled is disabled, these optional environment variables configure how the external ada is linked:

Variable Description
ADA_LIB_DIR Optional directory added to the linker search path (cargo:rustc-link-search=native).
ADA_LIB_NAME Optional library to link, passed verbatim to cargo:rustc-link-lib (e.g. ada or static=ada).

If neither is provided, no link directive is emitted, leaving it to the host build system to provide the ada symbols at final link time. The external ada must expose the same C ABI (ada_*) and be ABI-compatible with this crate's version.

# Link against a system-installed libada in /usr/local/lib
ADA_LIB_DIR=/usr/local/lib ADA_LIB_NAME=ada \
  cargo build --no-default-features --features std

Performance

Ada is fast. The benchmark below shows 3.70 times faster URL parsing compared to url (cargo bench --bench parse):

BasicBench_AdaURL_CanParse/ada_url
                        time:   [1.2957 µs 1.3269 µs 1.3607 µs]
                        thrpt:  [566.30 MiB/s 580.73 MiB/s 594.72 MiB/s]

BasicBench_AdaURL_href/ada_url
                        time:   [2.7933 µs 2.8386 µs 2.8920 µs]
                        thrpt:  [266.45 MiB/s 271.46 MiB/s 275.86 MiB/s]

BasicBench_AdaURL_href/url
                        time:   [10.265 µs 10.491 µs 10.751 µs]
                        thrpt:  [71.675 MiB/s 73.451 MiB/s 75.068 MiB/s]

IDNA conversion is compared against the idna crate on a mixed set of international domain names (cargo bench --bench idna):

IdnaBench_ToASCII/ada_url
                        time:   [8.6471 µs 8.7932 µs 8.9471 µs]
                        thrpt:  [23.770 MiB/s 24.186 MiB/s 24.594 MiB/s]

IdnaBench_ToASCII/idna  time:   [8.1852 µs 8.3420 µs 8.5088 µs]
                        thrpt:  [24.994 MiB/s 25.494 MiB/s 25.982 MiB/s]

IdnaBench_ToUnicode/ada_url
                        time:   [10.202 µs 10.438 µs 10.700 µs]
                        thrpt:  [19.876 MiB/s 20.375 MiB/s 20.846 MiB/s]

IdnaBench_ToUnicode/idna
                        time:   [7.5930 µs 7.8360 µs 8.0856 µs]
                        thrpt:  [26.302 MiB/s 27.140 MiB/s 28.009 MiB/s]

Profile-Guided Optimization (PGO)

PGO can further speed up both the Rust code and the bundled C++ ada. The C++ sources are compiled with the cc crate, which honors CXX and CXXFLAGS, so they can be instrumented together with the Rust code using clang and cargo-pgo:

# 1. Build instrumented binaries and run a representative workload (here, the benchmarks)
CXX=clang++ CXXFLAGS="-fprofile-generate" cargo pgo bench
# 2. Merge the collected profiles
llvm-profdata merge -o ada.profdata target/pgo-profiles/*.profraw
# 3. Rebuild with the profile applied to both the Rust and the C++ code
CXX=clang++ CXXFLAGS="-fprofile-use=$PWD/ada.profdata" cargo pgo optimize bench

Use a clang and llvm-profdata with the same LLVM major version as rustc -vV, otherwise clang cannot read the profile. In your own project, use cargo pgo build and cargo pgo optimize build with a representative workload instead of the benchmarks.

Implemented traits

Url implements the following traits.

Trait(s) Description
Display Provides to_string and allows for the value to be used in format! macros (e.g. println!).
Debug Allows debugger output in format macros, ({:?} syntax)
PartialEq, Eq Allows for comparison, url1 == url2, url1.eq(url2)
PartialOrd, Ord Allows for ordering url1 < url2, done so alphabetically. This is also allows Url to be used as a key in a BTreeMap
Hash Makes it so that Url can be hashed based on the string representation. This is important so that Url can be used as a key in a HashMap
FromStr Allows for use with str's parse method
TryFrom<String>, TryFrom<&str> Provides try_into methods for String and &str
Borrow<str>, Borrow<[u8]> Used in some crates so that the Url can be used as a key.
Deref<Target=str> Allows for &Url to dereference as a &str. Also provides a number of string methods
AsRef<[u8]>, AsRef<str> Used to do a cheap reference-to-reference conversion.
Send Used to declare that the type can be transferred across thread boundaries.
Sync Used to declare that the type is thread-safe.

Development

justfile

The justfile contains commands (called "recipes") that can be executed by just for convenience.

Run all lints and tests:

just all

Skipping features:

just all --skip=libcpp,serde

License

This code is made available under the Apache License 2.0 as well as the MIT license.

Our tests include third-party code and data. The benchmarking code includes third-party code: it is provided for research purposes only and not part of the library.

About

Rust bindings for Ada URL parser

Topics

Resources

Stars

123 stars

Watchers

4 watching

Forks

Releases

Used by

Contributors

Languages