Skip to content

Commit

Permalink
Merge pull request #2373 from zephir-lang/mass-code-refactor
Browse files Browse the repository at this point in the history
Mass code refactor
  • Loading branch information
Jeckerson authored Jul 17, 2022
2 parents 11e54de + 2e1344f commit 79dc763
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 507 deletions.
28 changes: 12 additions & 16 deletions Library/ArgInfoDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,19 @@ private function renderEnd(): void
case '0:variable':
case '1:variable':
if (isset($parameter['cast'])) {
switch ($parameter['cast']['type']) {
case 'variable':
$value = $parameter['cast']['value'];
$this->codePrinter->output(
sprintf(
"\tZEND_ARG_OBJ_INFO(%d, %s, %s, %d)",
$this->passByReference($parameter),
$parameter['name'],
escape_class($this->compilationContext->getFullName($value)),
(int) $this->allowNull($parameter)
)
);
break;

default:
throw new Exception('Unexpected exception');
if ($parameter['cast']['type'] !== 'variable') {
throw new Exception('Unexpected exception');
}

$this->codePrinter->output(
sprintf(
"\tZEND_ARG_OBJ_INFO(%d, %s, %s, %d)",
$this->passByReference($parameter),
$parameter['name'],
escape_class($this->compilationContext->getFullName($parameter['cast']['value'])),
(int) $this->allowNull($parameter)
)
);
} else {
$this->codePrinter->output(
sprintf(
Expand Down
10 changes: 1 addition & 9 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@

use function Zephir\add_slashes;

/**
* Zephir\Backends\ZendEngine3\Backend.
*/
class Backend extends BackendZendEngine2
{
protected $name = 'ZendEngine3';
Expand Down Expand Up @@ -1001,12 +998,7 @@ public function copyOnWrite(Variable $target, $var, CompilationContext $context)

public function forStatement(Variable $exprVariable, $keyVariable, $variable, $duplicateKey, $duplicateHash, $statement, $statementBlock, CompilationContext $compilationContext)
{
/*
* Create a hash table and hash pointer temporary variables.
*/
//$arrayPointer = $compilationContext->symbolTable->addTemp('HashPosition', $compilationContext);
//$arrayHash = $compilationContext->symbolTable->addTemp('HashTable', $compilationContext);
/*
/**
* Create a temporary zval to fetch the items from the hash.
*/
$compilationContext->headersManager->add('kernel/fcall');
Expand Down
7 changes: 2 additions & 5 deletions Library/Backends/ZendEngine3/FcallManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
use Zephir\Fcall\FcallManagerInterface;
use function Zephir\file_put_contents_ex;

/**
* Zephir\Backends\ZendEngine3\FcallManager.
*/
class FcallManager implements FcallManagerInterface
{
protected $requiredMacros = [];
protected array $requiredMacros = [];

public function macroIsRequired($macro)
public function macroIsRequired($macro): bool
{
return isset($this->requiredMacros[$macro]);
}
Expand Down
2 changes: 2 additions & 0 deletions Library/Builder/Statements/LetStatementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Builder\Statements;

/**
Expand Down
13 changes: 7 additions & 6 deletions Library/Cache/ClassEntryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Cache;

use Zephir\CompilationContext;
use Zephir\Variable;

/**
* ClassEntryCache.
*
* Classes located in the PHP userland are cached to avoid further relocates
*/
class ClassEntryCache
{
protected $cache = [];
protected array $cache = [];

/**
* Retrieves/Creates a class entry cache.
Expand All @@ -29,11 +30,11 @@ class ClassEntryCache
* @param bool $dynamic
* @param CompilationContext $compilationContext
*
* @return \Zephir\Variable
* @return Variable
*/
public function get($className, $dynamic, CompilationContext $compilationContext)
public function get(string $className, bool $dynamic, CompilationContext $compilationContext): Variable
{
/*
/**
* Creates a guard variable if the class name is not dynamic
*/
if (!$dynamic) {
Expand Down
25 changes: 10 additions & 15 deletions Library/Cache/FunctionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Cache;

use Zephir\Call;
use Zephir\CompilationContext;
use Zephir\Passes\CallGathererPass;

/**
* FunctionCache.
*
* Calls in Zephir implement monomorphic and polymorphic caches to
* improve performance. Method/Functions lookups are cached in a standard
* first-level method lookup cache.
Expand All @@ -36,14 +35,14 @@
*/
class FunctionCache
{
protected $cache = [];
protected array $cache = [];

protected $gatherer;
protected ?CallGathererPass $gatherer = null;

/**
* FunctionCache constructor.
*
* @param CallGathererPass $gatherer
* @param CallGathererPass|null $gatherer
*/
public function __construct(CallGathererPass $gatherer = null)
{
Expand All @@ -54,13 +53,12 @@ public function __construct(CallGathererPass $gatherer = null)
* Retrieves/Creates a function cache for a function call.
*
* @param string $functionName
* @param Call $call
* @param CompilationContext $compilationContext
* @param bool $exists
*
* @return string
*/
public function get($functionName, CompilationContext $compilationContext, Call $call, $exists)
public function get(string $functionName, CompilationContext $compilationContext, bool $exists): string
{
if (isset($this->cache[$functionName])) {
return $this->cache[$functionName].', '.SlotsCache::getExistingFunctionSlot($functionName);
Expand All @@ -73,13 +71,10 @@ public function get($functionName, CompilationContext $compilationContext, Call
$cacheSlot = SlotsCache::getFunctionSlot($functionName);

$number = 0;
if (!$compilationContext->insideCycle) {
$gatherer = $this->gatherer;
if ($gatherer) {
$number = $gatherer->getNumberOfFunctionCalls($functionName);
if ($number <= 1) {
return 'NULL, '.$cacheSlot;
}
if (!$compilationContext->insideCycle && $this->gatherer !== null) {
$number = $this->gatherer->getNumberOfFunctionCalls($functionName);
if ($number <= 1) {
return 'NULL, '.$cacheSlot;
}
}

Expand Down
39 changes: 18 additions & 21 deletions Library/Cache/MethodCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Cache;

use ReflectionClass;
use ReflectionException;
use Zephir\ClassDefinition;
use Zephir\CompilationContext;
use Zephir\MethodCallWarmUp;
use Zephir\Passes\CallGathererPass;
use Zephir\Variable;

/**
* MethodCache.
*
* Calls in Zephir implement monomorphic and polymorphic caches to
* improve performance. Method/Functions lookups are cached in a standard
* first-level method lookup cache.
Expand All @@ -38,14 +40,12 @@
*/
class MethodCache
{
protected $cache = [];
protected array $cache = [];

protected $gatherer;
protected ?CallGathererPass $gatherer = null;

/**
* MethodCache.
*
* @param CallGathererPass $gatherer
* @param CallGathererPass|null $gatherer
*/
public function __construct(CallGathererPass $gatherer = null)
{
Expand All @@ -60,8 +60,10 @@ public function __construct(CallGathererPass $gatherer = null)
* @param Variable $caller
*
* @return string
*
* @throws ReflectionException
*/
public function get(CompilationContext $compilationContext, $methodName, Variable $caller)
public function get(CompilationContext $compilationContext, string $methodName, Variable $caller): string
{
$compiler = $compilationContext->compiler;

Expand Down Expand Up @@ -185,27 +187,22 @@ public function get(CompilationContext $compilationContext, $methodName, Variabl
/**
* Checks if the class is suitable for caching.
*
* @param ClassDefinition $classDefinition
* @param ClassDefinition|ReflectionClass|null $classDefinition
*
* @return bool
*/
private function isClassCacheable($classDefinition)
private function isClassCacheable($classDefinition = null): bool
{
if ($classDefinition instanceof ClassDefinition) {
return true;
}
if ($classDefinition instanceof \ReflectionClass) {
if ($classDefinition->isInternal() && $classDefinition->isInstantiable()) {
$extension = $classDefinition->getExtension();
switch ($extension->getName()) {
case 'Reflection':
case 'Core':
case 'SPL':
return true;
}
}

if (!($classDefinition instanceof ReflectionClass)) {
return false;
}

return false;
return $classDefinition->isInternal() &&
$classDefinition->isInstantiable() &&
in_array($classDefinition->getExtension()->getName(), ['Reflection', 'Core', 'SPL']);
}
}
33 changes: 13 additions & 20 deletions Library/Cache/SlotsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir\Cache;

use Zephir\ClassMethod;

/**
* SlotsCache.
*
* In order to reduce memory allocation when calling functions and method_exists
* Zephir provides a global cache that store pointers to resolved functions
* that aren't dynamical reducing the time to lookup functions and methods
*/
class SlotsCache
{
const MAX_SLOTS_NUMBER = 512;
private static $slot = 1;
public const MAX_SLOTS_NUMBER = 512;

private static int $slot = 1;

private static $cacheMethodSlots = [];
private static array $cacheMethodSlots = [];

private static $cacheFunctionSlots = [];
private static array $cacheFunctionSlots = [];

/**
* Returns or creates a cache slot for a function.
Expand All @@ -36,7 +37,7 @@ class SlotsCache
*
* @return int
*/
public static function getFunctionSlot($functionName)
public static function getFunctionSlot(string $functionName): int
{
if (isset(self::$cacheFunctionSlots[$functionName])) {
return self::$cacheFunctionSlots[$functionName];
Expand All @@ -59,13 +60,9 @@ public static function getFunctionSlot($functionName)
*
* @return int
*/
public static function getExistingFunctionSlot($functionName)
public static function getExistingFunctionSlot(string $functionName): int
{
if (isset(self::$cacheFunctionSlots[$functionName])) {
return self::$cacheFunctionSlots[$functionName];
}

return 0;
return self::$cacheFunctionSlots[$functionName] ?? 0;
}

/**
Expand All @@ -75,7 +72,7 @@ public static function getExistingFunctionSlot($functionName)
*
* @return int
*/
public static function getMethodSlot(ClassMethod $method)
public static function getMethodSlot(ClassMethod $method): int
{
$className = $method->getClassDefinition()->getCompleteName();
$methodName = $method->getName();
Expand All @@ -101,15 +98,11 @@ public static function getMethodSlot(ClassMethod $method)
*
* @return int
*/
public static function getExistingMethodSlot(ClassMethod $method)
public static function getExistingMethodSlot(ClassMethod $method): int
{
$className = $method->getClassDefinition()->getCompleteName();
$methodName = $method->getName();

if (isset(self::$cacheMethodSlots[$className][$methodName])) {
return self::$cacheMethodSlots[$className][$methodName];
}

return 0;
return self::$cacheMethodSlots[$className][$methodName] ?? 0;
}
}
Loading

0 comments on commit 79dc763

Please sign in to comment.