From fec4ed05a7bbbc20236ef7610bb465a70e709ca2 Mon Sep 17 00:00:00 2001 From: Jon Michaelchuck Date: Sat, 3 Oct 2020 13:33:45 -0700 Subject: URI Validation Performance Improvements I've found _is_valid_uri to be a hotspot in serialization and deserialization. Benchmarking this version of the function against the original with 1e7 urls results in runtime improvement of ~2x. Tests against real RDF graphs with 2e6+ triples results in runtime improvement of ~8% in serialization and deserialization. --- rdflib/term.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/term.py b/rdflib/term.py index ac69c3bd..61ac2aec 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -74,7 +74,7 @@ _invalid_uri_chars = '<>" {}|\\^`' def _is_valid_uri(uri): - return all([ord(c) > 256 or not c in _invalid_uri_chars for c in uri]) + return not bool([c for c in uri if c in _invalid_uri_chars and ord(c) <= 256]) _lang_tag_regex = compile("^[a-zA-Z]+(?:-[a-zA-Z0-9]+)*$") -- cgit v1.2.1 From c95965080089b43f752beeb33f9fb1968c8c0fdd Mon Sep 17 00:00:00 2001 From: Jon Michaelchuck Date: Sat, 3 Oct 2020 15:01:30 -0700 Subject: This is faster and more legible. Removed the ord(c) check as it is effectively a no-op since ord(c) is <= 256 for all _invalid_uri_chars --- rdflib/term.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rdflib/term.py b/rdflib/term.py index 61ac2aec..f5984aea 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -74,7 +74,10 @@ _invalid_uri_chars = '<>" {}|\\^`' def _is_valid_uri(uri): - return not bool([c for c in uri if c in _invalid_uri_chars and ord(c) <= 256]) + for c in uri: + if c in _invalid_uri_chars: + return False + return True _lang_tag_regex = compile("^[a-zA-Z]+(?:-[a-zA-Z0-9]+)*$") -- cgit v1.2.1 From 6484fcd2ac771c111577133e23be4a250c0e20de Mon Sep 17 00:00:00 2001 From: Jon Michaelchuck Date: Sat, 3 Oct 2020 15:45:20 -0700 Subject: about 2x fast --- rdflib/term.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rdflib/term.py b/rdflib/term.py index f5984aea..3a822158 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -74,8 +74,8 @@ _invalid_uri_chars = '<>" {}|\\^`' def _is_valid_uri(uri): - for c in uri: - if c in _invalid_uri_chars: + for c in _invalid_uri_chars: + if c in uri: return False return True -- cgit v1.2.1