Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Bean/Annotation/Model/AnnotationRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,14 @@ public function removeClassRelation($className): void
{
$classRelations = &$this->classRelations;
$className = (array) $className;
// Optimize lookup by using array keys instead of in_array
$classNameMap = array_flip($className);
foreach ($classRelations as $annotationClass => &$list)
{
$haveUnset = false;
foreach ($list as $i => $item)
{
if (\in_array($item->getClass(), $className))
if (isset($classNameMap[$item->getClass()]))
{
unset($list[$i]);
$haveUnset = true;
Expand All @@ -402,12 +404,14 @@ public function removeMethodRelation(string $className, $methodName): void
{
$methodRelations = &$this->methodRelations;
$methodName = (array) $methodName;
// Optimize lookup by using array keys instead of in_array
$methodNameMap = array_flip($methodName);
foreach ($methodRelations as $annotationClass => &$list)
{
$haveUnset = false;
foreach ($list as $i => $item)
{
if ($item->getClass() === $className && \in_array($item->getMethod(), $methodName))
if ($item->getClass() === $className && isset($methodNameMap[$item->getMethod()]))
{
unset($list[$i]);
$haveUnset = true;
Expand All @@ -430,12 +434,14 @@ public function removePropertyRelation(string $className, $propertyName): void
{
$propertyRelations = &$this->propertyRelations;
$propertyName = (array) $propertyName;
// Optimize lookup by using array keys instead of in_array
$propertyNameMap = array_flip($propertyName);
foreach ($propertyRelations as $annotationClass => &$list)
{
$haveUnset = false;
foreach ($list as $i => $item)
{
if ($item->getClass() === $className && \in_array($item->getProperty(), $propertyName))
if ($item->getClass() === $className && isset($propertyNameMap[$item->getProperty()]))
{
unset($list[$i]);
$haveUnset = true;
Expand All @@ -458,12 +464,14 @@ public function removeConstantRelation(string $className, $constantName): void
{
$constantRelations = &$this->constantRelations;
$constantName = (array) $constantName;
// Optimize lookup by using array keys instead of in_array
$constantNameMap = array_flip($constantName);
foreach ($constantRelations as $annotationClass => &$list)
{
$haveUnset = false;
foreach ($list as $i => $item)
{
if ($item->getClass() === $className && \in_array($item->getConstant(), $constantName))
if ($item->getClass() === $className && isset($constantNameMap[$item->getConstant()]))
{
unset($list[$i]);
$haveUnset = true;
Expand Down
20 changes: 17 additions & 3 deletions src/Bean/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class AnnotationParser
*/
private array $classes = [];

/**
* 文件名到类名的反向映射.
*
* @var array<string, string>
*/
private array $fileToClassMap = [];

/**
* 处理器类名映射.
*/
Expand Down Expand Up @@ -62,14 +69,14 @@ class AnnotationParser

public function __construct()
{
$this->initIncludeFiles = get_included_files();
$this->initIncludeFiles = array_flip(get_included_files());
$this->enableAnnotationCache = Config::get('@app.imi.annotation.cache', false);
AnnotationReader::addGlobalIgnoredName('noRector');
}

public function parse(string $className, bool $transaction = true, ?string $fileName = null): bool
{
$autoload = (null === $fileName) || (!isset($this->files[$fileName]) && (!\in_array($fileName, $this->initIncludeFiles)));
$autoload = (null === $fileName) || (!isset($this->files[$fileName]) && !isset($this->initIncludeFiles[$fileName]));
if (!class_exists($className, $autoload) && !interface_exists($className, false) && !trait_exists($className, false))
{
if ($autoload && !isset($this->files[$fileName]) && null !== $fileName)
Expand Down Expand Up @@ -151,6 +158,7 @@ public function parseClass(\ReflectionClass $ref): void
{
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->fileToClassMap[$fileName] = $className;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -245,6 +253,7 @@ public function parseMethod(\ReflectionClass $ref, \ReflectionMethod $method): v
{
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->fileToClassMap[$fileName] = $className;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -339,6 +348,7 @@ public function parseProp(\ReflectionClass $ref, \ReflectionProperty $prop): voi
{
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->fileToClassMap[$fileName] = $className;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -432,6 +442,7 @@ public function parseConst(\ReflectionClass $ref, \ReflectionClassConstant $cons
{
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->fileToClassMap[$fileName] = $className;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -516,6 +527,7 @@ public function parseMethodParameter(\ReflectionClass $ref, \ReflectionMethod $r
{
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->fileToClassMap[$fileName] = $className;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -764,7 +776,7 @@ public function parseIncr(array $files): void
{
unset($thisFiles[$file]);
}
if (!($className = array_search($file, $thisClasses)))
if (!($className = $this->fileToClassMap[$file] ?? false))
{
if (is_file($file))
{
Expand Down Expand Up @@ -813,6 +825,7 @@ public function getStoreData(): array
return [
$this->files,
$this->classes,
$this->fileToClassMap,
];
}

Expand All @@ -823,6 +836,7 @@ public function loadStoreData(array $data): void
{
$this->files = $data[0];
$this->classes = $data[1];
$this->fileToClassMap = $data[2] ?? [];
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Db/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,9 @@ public function column($fields, ?string $key = null): array
{
$key = null;
}
if ($key && !\in_array($key, $fields))
// Optimize by converting to associative array for O(1) lookup
$fieldsMap = array_flip($fields);
if ($key && !isset($fieldsMap[$key]))
{
$fields[] = $key;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Model/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ public function __construct(string $modelClass, bool $inherit = false)
$this->bean = $entity->bean ?? true;
$this->incrUpdate = $entity->incrUpdate ?? false;
$serializableFieldNames = $parsedSerializableFieldNames = $fieldNames = [];
// Optimize field lookup by using array keys instead of in_array
$serializableFieldsMap = $serializables ? array_flip($serializables->fields) : [];
foreach ($fields as $fieldName => $column)
{
$fieldNames[] = $fieldName;
Expand Down Expand Up @@ -366,7 +368,7 @@ public function __construct(string $modelClass, bool $inherit = false)
}
elseif ($serializables)
{
if (\in_array($name, $serializables->fields))
if (isset($serializableFieldsMap[$name]))
{
// 在黑名单中的字段剔除
if ('deny' === $serializables->mode)
Expand Down
4 changes: 3 additions & 1 deletion src/Validate/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,16 @@ protected function __validateAll(&$data, bool $break): bool
$thisResults = [];
$result = true;
$sceneOption = $this->scene[$this->currentScene] ?? null;
// Optimize field lookup by using array keys instead of in_array
$sceneOptionMap = $sceneOption ? array_flip($sceneOption) : null;
foreach ($this->rules as $annotation)
{
if (!$annotation instanceof Condition)
{
continue;
}
$annotationName = $annotation->name;
if ($sceneOption && !\in_array($annotationName, $sceneOption))
if ($sceneOptionMap && !isset($sceneOptionMap[$annotationName]))
{
continue;
}
Expand Down