Skip to content

Commit ad376ff

Browse files
mariushochthiemowmde
authored andcommitted
Optimized DispatchingEntityIdParser (#764)
This made the function quite a bit faster: 14.48% 48797.778 817218 - Wikibase\DataModel\Entity\DispatchingEntityIdParser::parse to 12.64% 42330.389 817218 - Wikibase\DataModel\Entity\DispatchingEntityIdParser::parse when JSON-dumping 10,000 Wikidata entities. Related to https://phabricator.wikimedia.org/T177486.
1 parent 551e51e commit ad376ff

1 file changed

Lines changed: 9 additions & 25 deletions

File tree

src/Entity/DispatchingEntityIdParser.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,19 @@ public function __construct( array $idBuilders ) {
3737
* @return EntityId
3838
*/
3939
public function parse( $idSerialization ) {
40-
$this->assertIdIsString( $idSerialization );
41-
42-
if ( empty( $this->idBuilders ) ) {
40+
if ( $this->idBuilders === [] ) {
4341
throw new EntityIdParsingException( 'No id builders are configured' );
4442
}
4543

44+
try {
45+
list( , , $localId ) = EntityId::splitSerialization( $idSerialization );
46+
} catch ( InvalidArgumentException $ex ) {
47+
// EntityId::splitSerialization performs some sanity checks which
48+
// might result in an exception. Should this happen, re-throw the exception message
49+
throw new EntityIdParsingException( $ex->getMessage(), 0, $ex );
50+
}
51+
4652
foreach ( $this->idBuilders as $idPattern => $idBuilder ) {
47-
try {
48-
list( , , $localId ) = EntityId::splitSerialization( $idSerialization );
49-
} catch ( InvalidArgumentException $ex ) {
50-
// EntityId::splitSerialization performs some sanity checks which
51-
// might result in an exception. Should this happen, re-throw the exception message
52-
throw new EntityIdParsingException( $ex->getMessage(), 0, $ex );
53-
}
5453
if ( preg_match( $idPattern, $localId ) ) {
5554
return $this->buildId( $idBuilder, $idSerialization );
5655
}
@@ -61,21 +60,6 @@ public function parse( $idSerialization ) {
6160
);
6261
}
6362

64-
/**
65-
* @param string $idSerialization
66-
*
67-
* @throws EntityIdParsingException
68-
*/
69-
private function assertIdIsString( $idSerialization ) {
70-
if ( !is_string( $idSerialization ) ) {
71-
throw new EntityIdParsingException(
72-
'$idSerialization must be a string, got ' . ( is_object( $idSerialization )
73-
? get_class( $idSerialization )
74-
: getType( $idSerialization ) )
75-
);
76-
}
77-
}
78-
7963
/**
8064
* @param callable $idBuilder
8165
* @param string $idSerialization

0 commit comments

Comments
 (0)