Skip to content

Commit 6b5fba0

Browse files
committed
Add optimization that measures term @id definition once.
1 parent 9759c93 commit 6b5fba0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

jsonld.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,24 +4695,24 @@ protected function _compactIri(
46954695
}
46964696

46974697
// no term or @vocab match, check for possible CURIEs
4698+
$iri_len = strlen($iri);
46984699
$choice = null;
46994700
foreach($active_ctx->mappings as $term => $definition) {
4700-
// skip terms with colons, they can't be prefixes
4701-
if($definition && $definition->_term_has_colon) {
4701+
// skip null definitions and terms with colons, they can't be prefixes
4702+
if($definition === null || $definition->_term_has_colon) {
47024703
continue;
47034704
}
47044705
// skip entries with @ids that are not partial matches
4705-
if($definition === null ||
4706-
$definition->{'@id'} === $iri ||
4707-
strpos($iri, $definition->{'@id'}) !== 0) {
4706+
if(!($iri_len > $definition->_id_length &&
4707+
strpos($iri, $definition->{'@id'}) === 0)) {
47084708
continue;
47094709
}
47104710

47114711
// a CURIE is usable if:
47124712
// 1. it has no mapping, OR
47134713
// 2. value is null, which means we're not compacting an @value, AND
47144714
// the mapping matches the IRI)
4715-
$curie = $term . ':' . substr($iri, strlen($definition->{'@id'}));
4715+
$curie = $term . ':' . substr($iri, $definition->_id_length);
47164716
$is_usable_curie = (!property_exists($active_ctx->mappings, $curie) ||
47174717
($value === null && $active_ctx->mappings->{$curie} &&
47184718
$active_ctx->mappings->{$curie}->{'@id'} === $iri));
@@ -4990,6 +4990,9 @@ protected function _createTermDefinition(
49904990
}
49914991
}
49924992

4993+
// optimization to store length of @id once for _compactIri
4994+
$mapping->_id_length = strlen($mapping->{'@id'});
4995+
49934996
// IRI mapping now defined
49944997
$defined->{$term} = true;
49954998

0 commit comments

Comments
 (0)