-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance optimization #387
base: master
Are you sure you want to change the base?
Conversation
…first - Reorder checks to prioritize definition lookup which is the most common case - Return true immediately if definition exists - Only check tag aliases if definition doesn't exist - Return false if neither exists Performance improvements: - benchPredefinedExisting: ~29% faster (130μs → 92μs) - benchUndefinedExisting: ~29% faster (123μs → 87μs) - benchUndefinedNonexistent: ~1% faster (446μs → 441μs) - Small improvements in other benchmarks
- Add fast path for existing instances - Extract StateResetter logic into separate method - Move StateResetter check to the end to avoid duplicate instance access - Improve code readability and maintainability Performance improvements: - benchSequentialLookups: ~3% faster (620μs → 599μs) - benchUndefinedExisting: ~5% faster (87μs → 83μs) - Other benchmarks show similar or slightly improved performance
- Add early returns for common simple cases (strings, ContainerInterface, Closure) - Simplify array definition handling - Improve code readability and reduce nesting Performance improvements: - benchConstruct: ~29% faster (336μs → 238μs) - benchSequentialLookups: ~13% faster (599μs → 523μs) - benchRandomLookups: ~16% faster (621μs → 524μs) - benchRandomLookupsComposite: ~9% faster (2.3ms → 2.1ms) - benchUndefinedNonexistent: ~5% faster (464μs → 440μs)
- Add normalized definitions cache with memory management - Reorder checks to prioritize most common cases - Add fast path for circular reference check - Improve code readability with better comments Performance improvements: - benchRandomLookupsComposite: ~4% faster (2.3ms → 2.2ms) - benchSequentialLookups: ~2% faster (544μs → 539μs) - benchRandomLookups: ~1.5% faster (546μs → 525μs) Memory usage is kept in check by clearing the cache when it reaches 100 entries.
- Simplify exception handling paths - Add fast path for NotFoundException with matching ID - Use ternary operators for cleaner code - Improve code readability with better comments Performance improvements: - benchRandomLookupsComposite: ~1% faster (2.2ms → 2.18ms) - benchSequentialLookups: ~0.5% faster (539μs → 536μs) - benchRandomLookups: ~0.5% faster (541μs → 538μs)
- Skip parsing if already a valid definition - Only validate meta if it's not empty - Only unset instance if it exists - Improve code readability with better comments Performance improvements: - benchConstruct: ~10% faster (238μs → 213μs) - benchSequentialLookups: ~3.5% faster (536μs → 517μs) - benchRandomLookups: ~3.7% faster (538μs → 518μs) - benchRandomLookupsComposite: ~0.5% faster (2.18ms → 2.17ms)
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #387 +/- ##
=============================================
- Coverage 100.00% 99.60% -0.40%
- Complexity 169 176 +7
=============================================
Files 11 11
Lines 492 508 +16
=============================================
+ Hits 492 506 +14
- Misses 0 2 +2 ☔ View full report in Codecov by Sentry. |
src/Container.php
Outdated
throw new NotFoundException($id, [$id], previous: $exception); | ||
} | ||
// Fast path: check if instance exists. | ||
if (isset($this->instances[$id])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use array_key_exists
instead of isset
. Theoretically, result may be null
.
} | ||
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e); | ||
} catch (Throwable $e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems we should catch any exceptions from previous "catch" blocks also (for example, exceptions from delegates).
Did some fixes. @vjik can you help with Psalm? I can ignore mixed returns but, I guess, it could be solved in a better way. |
Before:
After: