perf: Tweak to userinfo parse to not repeat @ check

This commit is contained in:
2025-09-15 14:39:39 +01:00
parent 027f94e666
commit 897124be27
2 changed files with 49 additions and 42 deletions

View File

@@ -168,7 +168,7 @@ fn parse_authority(str: String) -> Result(#(Uri, String), Nil) {
} }
fn parse_authority_part(str: String) -> Result(#(Uri, String), Nil) { fn parse_authority_part(str: String) -> Result(#(Uri, String), Nil) {
let #(userinfo, rest) = parse_userinfo(str, "") let #(userinfo, rest) = parse_userinfo(str)
use #(host, rest) <- result.try(parse_host(rest)) use #(host, rest) <- result.try(parse_host(rest))
@@ -180,8 +180,12 @@ fn parse_authority_part(str: String) -> Result(#(Uri, String), Nil) {
} }
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
fn parse_userinfo(str: String, userinfo: String) -> #(Option(String), String) { fn parse_userinfo(str: String) -> #(Option(String), String) {
use <- bool.guard(when: !string.contains(str, "@"), return: #(None, str)) use <- bool.guard(when: !string.contains(str, "@"), return: #(None, str))
do_parse_userinfo(str, "")
}
fn do_parse_userinfo(str: String, userinfo: String) -> #(Option(String), String) {
case str { case str {
"@" <> rest -> #(Some(userinfo), rest) "@" <> rest -> #(Some(userinfo), rest)
"" -> #(None, userinfo <> str) "" -> #(None, userinfo <> str)
@@ -202,7 +206,7 @@ fn parse_userinfo(str: String, userinfo: String) -> #(Option(String), String) {
str, str,
) )
{ {
Ok(#(part, rest)) -> parse_userinfo(rest, userinfo <> part) Ok(#(part, rest)) -> do_parse_userinfo(rest, userinfo <> part)
Error(_) -> #(None, userinfo <> str) Error(_) -> #(None, userinfo <> str)
} }
} }
@@ -421,7 +425,7 @@ fn parse_ipv4address(str: String) {
// / "1" 2DIGIT ; 100-199 // / "1" 2DIGIT ; 100-199
// / "2" %x30-34 DIGIT ; 200-249 // / "2" %x30-34 DIGIT ; 200-249
// / "25" %x30-35 ; 250-255 // / "25" %x30-35 ; 250-255
pub fn parse_dec_octet(str: String) -> Result(#(String, String), Nil) { fn parse_dec_octet(str: String) -> Result(#(String, String), Nil) {
try_parsers( try_parsers(
[ [
parse_this_then(_, [ parse_this_then(_, [
@@ -486,7 +490,7 @@ pub fn parse_dec_octet(str: String) -> Result(#(String, String), Nil) {
} }
// reg-name = *( unreserved / pct-encoded / sub-delims ) // reg-name = *( unreserved / pct-encoded / sub-delims )
pub fn parse_reg_name(str: String) { fn parse_reg_name(str: String) {
// can't error // can't error
case do_parse_reg_name(str, "") { case do_parse_reg_name(str, "") {

View File

@@ -1,6 +1,5 @@
import gleam/uri as uri2 import gleam/uri as uri2
import gluri as uri import gluri as uri
import gluri/internal/parser
import glychee/benchmark import glychee/benchmark
import glychee/configuration import glychee/configuration
@@ -15,43 +14,43 @@ pub fn main() {
// ip_benchmark() // ip_benchmark()
} }
@target(erlang) // @target(erlang)
pub fn ip_benchmark() { // pub fn ip_benchmark() {
benchmark.run( // benchmark.run(
[ // [
benchmark.Function("ip_benchmark", fn(data) { // benchmark.Function("ip_benchmark", fn(data) {
fn() { // fn() {
let _ = parser.parse_dec_octet(data) // let _ = parser.parse_dec_octet(data)
Nil // Nil
} // }
}), // }),
], // ],
[ // [
benchmark.Data("173", "173"), // benchmark.Data("173", "173"),
benchmark.Data("5", "5"), // benchmark.Data("5", "5"),
benchmark.Data("200", "200"), // benchmark.Data("200", "200"),
benchmark.Data("255", "255"), // benchmark.Data("255", "255"),
benchmark.Data("fail", "2b"), // benchmark.Data("fail", "2b"),
], // ],
) // )
} // }
@target(erlang) // @target(erlang)
pub fn reg_name_benchmark() { // pub fn reg_name_benchmark() {
benchmark.run( // benchmark.run(
[ // [
benchmark.Function("reg_name_benchmark", fn(data) { // benchmark.Function("reg_name_benchmark", fn(data) {
fn() { // fn() {
let _ = parser.parse_reg_name(data) // let _ = parser.parse_reg_name(data)
Nil // Nil
} // }
}), // }),
], // ],
[ // [
benchmark.Data("long", "github.com"), // benchmark.Data("long", "github.com"),
], // ],
) // )
} // }
@target(erlang) @target(erlang)
pub fn parse_benchmark() { pub fn parse_benchmark() {
@@ -75,6 +74,10 @@ pub fn parse_benchmark() {
"long", "long",
"https://github.com/gleam-lang/stdlib/issues/523#issuecomment-3288230480", "https://github.com/gleam-lang/stdlib/issues/523#issuecomment-3288230480",
), ),
benchmark.Data(
"with user",
"https://test_name:user%20$$$@github.com/gleam-lang/stdlib/issues/523#issuecomment-3288230480",
),
benchmark.Data("ipv4", "https://192.255.36.4/"), benchmark.Data("ipv4", "https://192.255.36.4/"),
], ],
) )