perf: Replace O(n) array operations with O(1) hash lookups in hot paths #719
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Identified and optimized performance bottlenecks where
in_array()andarray_search()caused O(n) lookups in hot code paths including annotation parsing, model serialization, and validation loops.Changes
Bean/AnnotationParser.php
initIncludeFilesto associative array viaarray_flip()for O(1) file inclusion checksfileToClassMapreverse index to eliminatearray_search()inparseIncr()loopModel/Meta.php
serializableFieldsMapbefore field iteration loopValidate/Validator.php
sceneOptionMapbefore validation rule iterationBean/Annotation/Model/AnnotationRelation.php
removeClassRelation,removeMethodRelation,removePropertyRelation,removeConstantRelation)Db/Query/Query.php
fieldsMapbefore field existence checkExample
Performance Impact
Benchmark shows 6-43x speedup depending on array size. Most significant in nested loops (AnnotationRelation methods) and frequently-executed paths (annotation parsing, validation).
Compatibility
Backward compatible. AnnotationParser serialization updated to include new map with fallback:
$this->fileToClassMap = $data[2] ?? [].Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/repos/PHP-CS-Fixer/shim/zipball/f51b4aed90565c447136f1d015798f6f7c82490f/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/Seldaek/monolog/zipball/5cf826f2991858b54d5c3809bee745560a1042a7/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/Yurunsoft/ide-helper/zipball/ba278509ff47429632390166bcddf872ab26c1f9/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/composer/semver/zipball/198166618906cb2de69b95d7d47e5fa8aa1b2b95/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/doctrine/deprecations/zipball/459c2f5dd3d6a4633d3b5f46ee2b1c40f57d3f38/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/doctrine/instantiator/zipball/c6222283fa3f4ac679f8b9ced9a4e23f163e80d0/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/nikic/PHP-Parser/zipball/3a454ca033b9e06b63282ce19562e892747449bb/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/container/zipball/513e0666f7216c7459170d56df27dfcefe1689ea/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/http-server-handler/zipball/84c4fb66179be4caaf8e97bd239203245302e7d4/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/http-server-middleware/zipball/c1481f747daaa6a0782775cd6a8c26a1bf4a3829/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/94f8051919d1b0369a6bcc7931d679a511c03fe9/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/679e3ce485b99e84c775d28e2e96fade9a7fb50a/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phpstan/phpdoc-parser/zipball/1e0cd5370df5dd2e556a36b9c62f62e555870495/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/phpstan/phpstan/zipball/46e223dd68a620da18855c23046ddb00940b4014/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/rectorphp/rector/zipball/e61574288661334155de6e5f0f45497285abad5d/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/2b56bea83a09de3ac06bb18b92f068e60cc6f50b/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/ac91f01ccec49fb77bdc6fd1e548bc70f7faa3e5/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/code-unit/zipball/1fc9f64c0927627ef78ba436c9b17d967e68e120/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/comparator/zipball/67a2df3a62639eab2cc5906065e9805d4fd5dfc5/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/diff/zipball/ba01945089c3a293b01ba9badc29ad55b106b0bc/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/environment/zipball/830c43a844f1f8d5b7a1f6d6076b784454d8b7ed/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/exporter/zipball/14c6ba52f95a36c3d27c835d65efc7123c446e8c/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/global-state/zipball/b6781316bdcd28260904e7cc18ec983d0d2ef4f6/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/5c9eeac41b290a3712d88851518825ad78f45c71/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/b4f479ebdbf63ac605d183ece17d8d7fe49c15c7/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/php-invoker/zipball/5a10147d0aaf65b58940a0b72f71c9ac0423cc67/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/5da5f67fc95621df9ff4c4e5a84d6a8a2acf7c28/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/php-timer/zipball/5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9ecfec57835a5581bc888ea7e13b51eb55ab9dd3/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/539c6691e0623af6dc6f9c20384c120f963465a0/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/type/zipball/75e2c2a32f5e0b3aef905b9ed0b179b953b3d7c7/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/sebastianbergmann/version/zipball/c6c1022351a901512170118436c764e473f6de8c/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/380872130d3a5dd3ace2f4010d95125fde5d5c70/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/symfony/string/zipball/f96476035142921000338bad71e5247fbc138872/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/theseer/tokenizer/zipball/737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)https://api.github.com/repos/webmozarts/assert/zipball/541057574806f942c94662b817a50f63f7345360/usr/bin/php8.3 -n -c /tmp/Qixr4H /usr/bin/composer install --no-interaction(http block)/usr/bin/php8.3 -n -c /tmp/XoGhXy /usr/bin/composer install --no-interaction --prefer-dist(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.