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.
@@ -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]+)*$")
