On 20. 11. 2026 14:27, Yann Ylavic wrote:
On Thu, Nov 19, 2026 at 2:44 PM Branko Čibej via dev<[email protected]> wrote:
On 18. 11. 2026 09:59, Ruediger Pluem via dev wrote:
On 11/16/26 4:10 PM, Yann Ylavic wrote:
diff -ru a/uri/apr_uri.c b/uri/apr_uri.c
--- a/uri/apr_uri.c 2026-11-16 16:02:56.759516227 +0100
+++ b/uri/apr_uri.c 2026-11-16 16:02:56.784734796 +0100
@@ -999,6 +999,7 @@
if (have_zone_id) {
percent_decode_scope_zone_id(uptr->hostname);
}
+ uptr->port = apr_uri_port_of_scheme(uptr->scheme);
goto deal_with_path;
}
uptr->hostname = apr_pstrmemdup(p,
Sounds sensible. Maybe AI can help us generate all the test cases such that
we can be sure it works and keeps working after changes.
See attached "apr-uri-default-port.patch" (including tests).
I have an implementation ready that uses a generated minimal perfect hash
function for lookups. The hash function is O(strlen(key)) to calculate the hash
of the lookup key and O(1) to find the scheme. Except for the very first scheme
in in the list, where linear lookup needs just a single strcasecmp(), it's
faster by up to 5 times from the current lookup.
This adds quite some infrastructure to APR (Python generator,
template, generated .inc, uri_schemes.h, non-static apr__uri_schemes),
(and in the meantime I got rid of the strcasecmp() and replaced it with
strncmp(), gaining another 20%)
what about something like the attached "apr-uri-port-of-scheme.patch"
(including tests)?
It's simpler (IMHO), and in my testing it works faster (probably due
to the multiplies for the perfect hashing) and also avoids both
strcasecmp() (current code) and tolower() (your patch) which are
locale dependent.
Here are my numbers (ns per call, clock of 1.2 GHz, median over 5
interleaved rounds):
┌────────────────────────┬────────────────┬─────────────┬───────────────────────┐
│ scheme │ current linear │ switch │ perfect
hash │
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ http │ 13.7 │ 17.5 │ 51.8
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ https │ 33.4 │ 31.7 │ 54.3
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ prospero / Prospero │ 138.9 / 130.2 │ 27.8 / 31.3 │ 66.2 / 68.5
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ ssh / SSH │ 176.2 / 180.8 │ 35.8 / 36.8 │ 46.9 / 46.4
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ unknown │ 171.7 │ 5.8 │ 66.3
│
├────────────────────────┼────────────────┼─────────────┼───────────────────────┤
│ mean over all 24 names │ 100.8 │ 22.9 │ 53.5
│
└────────────────────────┴────────────────┴─────────────┴───────────────────────┘
I wonder why I got much a *much* bigger speedup factor from the perfect
hash than you did. Could be something about arm64 not being short of
registers compared to amd64?
Works for you?
Other than it being completely unreadable, anything is better than the
current linear search which incidentally also gets the "most commonly
used" bit completely wrong for the 21st century.
However, the unreadable objection implies unmaintainable, which my
solution isn't. It would be easy to change the perfect hash generator so
that the added globals go away. The generator itself is hardly
problematic, and avoiding the use of apr_tolower() is trivial as well --
in fact we already have an implementation in apr_cstr.c, it's just not
exported.
I guess, go with the switch() for now but to be future-prof we need
something better.
-- Brane