Skip to content

Commit

Permalink
Microoptimizations.
Browse files Browse the repository at this point in the history
array_key_exists() is a very tiny bit faster than isset().
  • Loading branch information
smuuf committed Dec 1, 2022
1 parent 6f738fc commit c6af30f
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Handlers/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function getFor($name, $strict = \true) {

// Using caching is of course faster than repeatedly building strings
// and checking classes and stuff.
if (isset(self::$handlersCache[$name])) {
if (\array_key_exists($name, self::$handlersCache)) {
return self::$handlersCache[$name];
}

Expand Down
10 changes: 8 additions & 2 deletions src/Handlers/Kinds/ArgumentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ class ArgumentList extends SimpleHandler {

protected static function handle(array $node, Context $context) {

if (!isset($node['args'])) {
if (!\array_key_exists('args', $node)) {
return [];
}

$args = [];
$kwargs = [];

foreach ($node['args'] as $arg) {
if (isset($arg['argKey'])) {
if (\array_key_exists('argKey', $arg)) {
$kwargs[$arg['argKey']['text']] = HandlerFactory::runNode($arg['argVal'], $context);
} else {

$result = HandlerFactory::runNode($arg['argVal'], $context);

// Argument might have been a starred expression, in which case
// the result is an array (see StarredExpression handler) - so
// let's unpack it.
// Single-starred expression is returned as a list array.
// Double-starred expression is returned as a dict array.
if (\is_array($result)) {
if (\array_is_list($result)) {
$args = [...$args, ...$result];
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Kinds/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function chain(
$value = $handler::chain($node['core'], $context, $subject);

// If there's chain, handle it, too.
if (isset($node['chain'])) {
if (\array_key_exists('chain', $node)) {
$handler = HandlerFactory::getFor($node['chain']['name']);
return $handler::chain($node['chain'], $context, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Kinds/Invocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function chain(
) {

$arguments = \null;
if (isset($node['args'])) {
if (\array_key_exists('args', $node)) {
$arguments = HandlerFactory::runNode($node['args'], $context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Kinds/Operand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected static function handle(array $node, Context $context) {
$value = HandlerFactory::runNode($node['core'], $context);

// If there's chain, handle it.
if (isset($node['chain'])) {
if (\array_key_exists('chain', $node)) {
$handler = HandlerFactory::getFor($node['chain']['name']);
return $handler::chain($node['chain'], $context, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Kinds/ReturnStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ReturnStatement extends SimpleHandler {
protected static function handle(array $node, Context $context) {

$retval = new CallRetval(
isset($node['subject'])
\array_key_exists('subject', $node)
? HandlerFactory::runNode($node['subject'], $context)
: \null
);
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/AssignmentTargets.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function assign(AbstractValue $value, Context $ctx) {
$targetIndex = 0;
foreach ($iter as $value) {

if (isset($this->targets[$targetIndex])) {
if (\array_key_exists($targetIndex, $this->targets)) {
$buffer[$this->targets[$targetIndex]] = $value;
} else {
throw new RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/CallArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function extract($names, $optional = []) {
foreach ($names as $name) {
if (
$name[0] !== '*'
&& !isset($final[$name])
&& !\array_key_exists($name, $final)
&& !\in_array($name, $optional, \true)
) {
throw new TypeError("Missing required argument '$name'");
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/FnContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function buildFromClosure(callable $fn, array $flags = []) {

$flagToStack = \in_array(self::FLAG_TO_STACK, $flags, \true);
$flagCallConventionArgsObject =
in_array(self::FLAG_CALLCONV_CALLARGS, $flags, \true);
\in_array(self::FLAG_CALLCONV_CALLARGS, $flags, \true);

$callConvention = $flagCallConventionArgsObject
? new CallArgsCallConvention($closure)
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/MapContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function set(AbstractValue $key, AbstractValue $value): void {
* @throws UnhashableTypeException
*/
public function hasKey(AbstractValue $key): bool {
return isset($this->values[self::buildScalarKey($key)]);
return \array_key_exists(self::buildScalarKey($key), $this->values);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Values/AbstractValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function buildAuto($value) {
}

$inner = \array_map(__METHOD__, $value);
if (!array_is_list($value)) {
if (!\array_is_list($value)) {
return new DictValue(Func::array_to_couples($inner));
}
return new ListValue($inner);
Expand Down
2 changes: 1 addition & 1 deletion src/Values/ModuleValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getStringRepr(): string {
* Return number of variables in the module's scope.
*/
public function getLength(): ?int {
return count($this->value->getVariables());
return \count($this->value->getVariables());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tools/phpcb-benchmarks/key_exists_vs_isset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require __DIR__ . "/../../vendor/autoload.php";
$bench = new \Smuuf\Phpcb\PhpBenchmark;

$arr = ['x' => 1, 'y' => 2, 'z' => false, "key" => 1, 'a' => 'b', 'c' => 0];
$arr2 = ['x' => 1, 'y' => 2, 'z' => false, "foo" => 1, 'a' => 'b', 'c' => 0];
$arr3 = ['x' => 1, 'y' => 2, 'z' => false, "key" => null, 'a' => 'b', 'c' => 0];
$arr = [...range(50, 500), 'x' => 1, 'y' => 2, 'z' => false, "key" => 1, 'a' => 'b', 'c' => 0];
$arr2 = [...range(50, 500), 'x' => 1, 'y' => 2, 'z' => false, "foo" => 1, 'a' => 'b', 'c' => 0];
$arr3 = [...range(50, 500), 'x' => 1, 'y' => 2, 'z' => false, "key" => null, 'a' => 'b', 'c' => 0];

$bench->addBench(function() use ($arr) {
$x = isset($arr['key']);
Expand Down

0 comments on commit c6af30f

Please sign in to comment.