mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
Compare commits
74 Commits
cherrypick
...
talker_che
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68a16aaa0c | ||
|
|
679b2b87b7 | ||
|
|
dbdae94673 | ||
|
|
4220967447 | ||
|
|
dfe16fb10f | ||
|
|
ce2e770cbe | ||
|
|
7f5f5c4064 | ||
|
|
04ecb6d3a6 | ||
|
|
484061148d | ||
|
|
b5b672765e | ||
|
|
482b7b0f5f | ||
|
|
722a4d7980 | ||
|
|
16cd7199aa | ||
|
|
1cbcce5b38 | ||
|
|
264c4bbb88 | ||
|
|
cbb5dcc3a0 | ||
|
|
d281c18a75 | ||
|
|
8ef12e990f | ||
|
|
5c57370755 | ||
|
|
8711dc83d0 | ||
|
|
043737e2c9 | ||
|
|
ed65e3c23d | ||
|
|
a897c1b31b | ||
|
|
dd9c3faa62 | ||
|
|
846d55b124 | ||
|
|
4f91d442af | ||
|
|
d0c3870af6 | ||
|
|
c8292035b6 | ||
|
|
63ee3a9966 | ||
|
|
a4c5fd922e | ||
|
|
8870b8ce54 | ||
|
|
1f7e1d120d | ||
|
|
bcc5278c83 | ||
|
|
8863b10cbe | ||
|
|
e0a5ae66f6 | ||
|
|
9fee26c524 | ||
|
|
248bf4c8c5 | ||
|
|
f4c4fe49a0 | ||
|
|
298cb65ac8 | ||
|
|
1b9db31c13 | ||
|
|
ca3cd2c8fd | ||
|
|
c91e15319b | ||
|
|
99e662124f | ||
|
|
03f54981f3 | ||
|
|
349efe6ba6 | ||
|
|
c2f0e027b6 | ||
|
|
f85036d20f | ||
|
|
db4d128d04 | ||
|
|
2c4e2ed251 | ||
|
|
7b4642f407 | ||
|
|
7d45d00d6a | ||
|
|
884df50a34 | ||
|
|
5710af2f9b | ||
|
|
9312ef46ea | ||
|
|
900cd68663 | ||
|
|
57e4196b95 | ||
|
|
358da8f96b | ||
|
|
ea2b6687f4 | ||
|
|
df00a2a5d2 | ||
|
|
d5983a4a0b | ||
|
|
125bccfa5a | ||
|
|
12b97c9368 | ||
|
|
424aaa3e22 | ||
|
|
2ec3a86a2f | ||
|
|
efed72cc39 | ||
|
|
4dc9e269cd | ||
|
|
d153ab4255 | ||
|
|
6924ccd07b | ||
|
|
26b843f791 | ||
|
|
8eafba4e4b | ||
|
|
ad6e9bbc3d | ||
|
|
bea8affcab | ||
|
|
1d7b9a9166 | ||
|
|
016c212063 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -18,5 +18,7 @@ pubspec_overrides.yaml
|
||||
melos_cherrypick.iml
|
||||
melos_cherrypick_workspace.iml
|
||||
melos_cherrypick_flutter.iml
|
||||
melos_benchmark_di.iml
|
||||
melos_talker_cherrypick_logger.iml
|
||||
|
||||
coverage
|
||||
354
CHANGELOG.md
354
CHANGELOG.md
@@ -3,6 +3,360 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v3.0.0-dev.0`](#talker_cherrypick_logger---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(talker_cherrypick_logger): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_flutter` - `v3.0.0-dev.0`](#cherrypick_flutter---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_flutter` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_flutter): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_generator` - `v3.0.0-dev.0`](#cherrypick_generator---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_generator` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_generator): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_annotations` - `v3.0.0-dev.0`](#cherrypick_annotations---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_annotations` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_annotations): sync version with cherrypick 3.0.0-dev.0
|
||||
|
||||
|
||||
## 2025-08-22
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.2`](#cherrypick_annotations---v112-dev2)
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.2`](#cherrypick_generator---v200-dev2)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_generator` - `v2.0.0-dev.2`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.2`
|
||||
|
||||
- **DOCS**(annotations): improve API documentation and usage example.
|
||||
|
||||
|
||||
## 2025-08-19
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.12`](#cherrypick---v300-dev12)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.12`](#cherrypick_flutter---v113-dev12)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.7`](#talker_cherrypick_logger---v110-dev7)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v1.1.3-dev.12`
|
||||
- `talker_cherrypick_logger` - `v1.1.0-dev.7`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.12`
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
|
||||
## 2025-08-19
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.11`](#cherrypick---v300-dev11)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.11`](#cherrypick_flutter---v113-dev11)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.6`](#talker_cherrypick_logger---v110-dev6)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v1.1.3-dev.11`
|
||||
- `talker_cherrypick_logger` - `v1.1.0-dev.6`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.11`
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
|
||||
## 2025-08-15
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.10`](#cherrypick---v300-dev10)
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.1`](#cherrypick_annotations---v112-dev1)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.10`](#cherrypick_flutter---v113-dev10)
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.1`](#cherrypick_generator---v200-dev1)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.5`](#talker_cherrypick_logger---v110-dev5)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.10`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.1`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.3-dev.10`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_generator` - `v2.0.0-dev.1`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.5`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.4`](#talker_cherrypick_logger---v110-dev4)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.4`
|
||||
|
||||
- **DOCS**(readme): update install instructions to use pub.dev as default method and remove obsolete git example.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.3`](#talker_cherrypick_logger---v110-dev3)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.3`
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.2`](#talker_cherrypick_logger---v110-dev2)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.2`
|
||||
|
||||
- Bump "talker_cherrypick_logger" to `1.1.0-dev.2`.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.0`](#cherrypick_generator---v200-dev0)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.9`](#cherrypick---v300-dev9)
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.0`](#cherrypick_annotations---v112-dev0)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.9`](#cherrypick_flutter---v113-dev9)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.0`](#talker_cherrypick_logger---v110-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_generator` - `v2.0.0-dev.0`
|
||||
|
||||
- **BREAKING** **DOCS**(generator): improve and unify English documentation and examples for all DI source files.
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.9`
|
||||
|
||||
- **DOCS**(readme): add talker_cherrypick_logger to Additional Modules section.
|
||||
- **DOCS**(api): improve all DI core code documentation with English dartdoc and examples.
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.0`
|
||||
|
||||
- **DOCS**(annotations): unify and improve English DartDoc for all DI annotations.
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.3-dev.9`
|
||||
|
||||
- **DOCS**(provider): add detailed English API documentation for CherryPickProvider Flutter integration.
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.0`
|
||||
|
||||
- **FEAT**(logging): add talker_dio_logger and talker_bloc_logger integration, improve cherrypick logger structure, add UI log screen for DI and network/bloc debug.
|
||||
- **DOCS**: add full English documentation and usage guide to README.md.
|
||||
- **DOCS**: add detailed English documentation and usage examples for TalkerCherryPickObserver.
|
||||
|
||||
|
||||
## 2025-08-12
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.8`](#cherrypick---v300-dev8)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.8`](#cherrypick_flutter---v113-dev8)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v1.1.3-dev.8`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.8`
|
||||
|
||||
- **REFACTOR**(tests): replace MockLogger with MockObserver in scope tests to align with updated observer API.
|
||||
- **FIX**(doc): remove hide symbol.
|
||||
- **FEAT**(core): add full DI lifecycle observability via onInstanceDisposed.
|
||||
- **DOCS**(logging): update Logging section in README with modern Observer usage and Talker integration examples.
|
||||
- **DOCS**(observer): improve documentation, translate all comments to English, add usage examples.
|
||||
- **DOCS**(README): add section with overview table for additional modules.
|
||||
- **DOCS**(README): refactor structure and improve clarity of advanced features.
|
||||
- **DOCS**(README): add 'Hierarchical Subscopes' section and update structure for advanced features clarity.
|
||||
|
||||
|
||||
## 2025-08-11
|
||||
|
||||
### Changes
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Contributors
|
||||
|
||||
- Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||
- Klim Yaroshevich <yarashevich_kv@st.by>
|
||||
- [Sergey Penkovsky](https://github.com/pese-git)
|
||||
- [Klim Yaroshevich](https://github.com/KlimYarosh)
|
||||
- [Alexey Popkov](https://github.com/AlexeyYuPopkov)
|
||||
@@ -1,4 +1,11 @@
|
||||
# Comparative DI Benchmark Report: cherrypick vs get_it vs riverpod
|
||||
# Comparative DI Benchmark Report: cherrypick vs get_it vs riverpod vs kiwi
|
||||
|
||||
## Benchmark Parameters
|
||||
|
||||
- chainCount = 100
|
||||
- nestingDepth = 100
|
||||
- repeat = 5
|
||||
- warmup = 2
|
||||
|
||||
## Benchmark Scenarios
|
||||
|
||||
@@ -11,41 +18,49 @@
|
||||
|
||||
---
|
||||
|
||||
## Comparative Table: chainCount=10, nestingDepth=10 (Mean, PeakRSS)
|
||||
## Comparative Table: chainCount=100, nestingDepth=100, repeat=5, warmup=2 (Mean time, µs)
|
||||
|
||||
| Scenario | cherrypick Mean (us) | cherrypick PeakRSS | get_it Mean (us) | get_it PeakRSS | riverpod Mean (us) | riverpod PeakRSS |
|
||||
|--------------------|---------------------:|-------------------:|-----------------:|---------------:|-------------------:|-----------------:|
|
||||
| RegisterSingleton | 13.00 | 273104 | 8.40 | 261872 | 9.80 | 268512 |
|
||||
| ChainSingleton | 13.80 | 271072 | 2.00 | 262000 | 33.60 | 268784 |
|
||||
| ChainFactory | 5.00 | 299216 | 4.00 | 297136 | 22.80 | 271296 |
|
||||
| AsyncChain | 28.60 | 290640 | 24.60 | 342976 | 78.20 | 285920 |
|
||||
| Named | 2.20 | 297008 | 0.20 | 449824 | 6.20 | 281136 |
|
||||
| Override | 7.00 | 297024 | 0.00 | 449824 | 30.20 | 281152 |
|
||||
| Scenario | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|-------|----------|
|
||||
| chainSingleton | 20.6 | 14.8 | 275.2 | 47.0 | 82.8 |
|
||||
| chainFactory | 90.6 | 71.6 | 357.0 | 46.2 | 79.6 |
|
||||
| register | 82.6 | 10.2 | 252.6 | 43.6 | 224.0 |
|
||||
| named | 18.4 | 9.4 | 12.2 | 10.2 | 10.8 |
|
||||
| override | 170.6 | 11.2 | 301.4 | 51.4 | 146.4 |
|
||||
| chainAsync | 493.8 | 34.0 | 5,039.0 | – | 87.2 |
|
||||
|
||||
## Maximum Load: chainCount=100, nestingDepth=100 (Mean, PeakRSS)
|
||||
|
||||
| Scenario | cherrypick Mean (us) | cherrypick PeakRSS | get_it Mean (us) | get_it PeakRSS | riverpod Mean (us) | riverpod PeakRSS |
|
||||
|--------------------|---------------------:|-------------------:|-----------------:|---------------:|-------------------:|-----------------:|
|
||||
| RegisterSingleton | 4.00 | 271072 | 1.00 | 262000 | 2.00 | 268688 |
|
||||
| ChainSingleton | 76.60 | 303312 | 2.00 | 297136 | 221.80 | 270784 |
|
||||
| ChainFactory | 80.00 | 293952 | 39.20 | 342720 | 195.80 | 308640 |
|
||||
| AsyncChain | 251.40 | 297008 | 18.20 | 450640 | 748.80 | 285968 |
|
||||
| Named | 2.20 | 297008 | 0.00 | 449824 | 1.00 | 281136 |
|
||||
| Override | 104.80 | 301632 | 2.20 | 477344 | 120.80 | 294752 |
|
||||
## Peak Memory Usage (Peak RSS, Kb)
|
||||
|
||||
| Scenario | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|--------|----------|
|
||||
| chainSingleton | 338,224 | 326,752| 301,856 | 195,520| 320,928 |
|
||||
| chainFactory | 339,040 | 335,712| 304,832 | 319,952| 318,688 |
|
||||
| register | 333,760 | 334,208| 300,368 | 327,968| 326,736 |
|
||||
| named | 241,040 | 229,632| 280,144 | 271,872| 266,352 |
|
||||
| override | 356,912 | 331,456| 329,808 | 369,104| 304,416 |
|
||||
| chainAsync | 311,616 | 434,592| 301,168 | – | 328,912 |
|
||||
|
||||
---
|
||||
|
||||
## Analysis
|
||||
|
||||
- **get_it** is the absolute leader in all scenarios, especially under deep/nested chains and async.
|
||||
- **cherrypick** is highly competitive and much faster than riverpod on any complex graph.
|
||||
- **riverpod** is only suitable for small/simple DI graphs due to major slowdowns with depth, async, or override.
|
||||
- **get_it** remains the clear leader for both speed and memory usage (lowest latency across most scenarios; excellent memory efficiency even on deep chains).
|
||||
- **kiwi** shows the lowest memory footprint in chainSingleton, but is unavailable for async chains.
|
||||
- **yx_scope** demonstrates highly stable performance for both sync and async chains, often at the cost of higher memory usage, especially in the register/override scenarios.
|
||||
- **cherrypick** comfortably beats riverpod, but is outperformed by get_it/kiwi/yx_scope, especially on async and heavy nested chains. It uses a bit less memory than yx_scope and kiwi, but can spike in memory/latency for override.
|
||||
- **riverpod** is unsuitable for deep or async chains—latency and memory usage grow rapidly.
|
||||
- **Peak memory (RSS):** usually around 320–340 MB for all DI; riverpod/kiwi occasionally drops below 300MB. named/factory scenarios use much less.
|
||||
- **Stability:** yx_scope and get_it have the lowest latency spikes; cherrypick can show peaks on override/async; riverpod is least stable on async (stddev/mean much worse).
|
||||
|
||||
### Recommendations
|
||||
- Use **get_it** for performance-critical and deeply nested graphs.
|
||||
- Use **cherrypick** for scalable/testable apps if a small speed loss is acceptable.
|
||||
- Use **riverpod** only if you rely on Flutter integration and your DI chains are simple.
|
||||
|
||||
- **get_it** (and often **kiwi**, if you don't need async): best for ultra-fast deep graphs and minimum peak memory.
|
||||
- **yx_scope**: best blend of performance and async stability; perfect for production mixed DI.
|
||||
- **cherrypick**: great for modular/testable architectures, unless absolute peak is needed; lower memory than yx_scope in some scenarios.
|
||||
- **riverpod**: only for shallow DI or UI wiring in Flutter.
|
||||
|
||||
---
|
||||
|
||||
_Last updated: August 8, 2025._
|
||||
_Last updated: August 20, 2025._
|
||||
_Please see scenario source for details._
|
||||
|
||||
@@ -1,51 +1,63 @@
|
||||
# Сравнительный отчет DI-бенчмарка: cherrypick vs get_it vs riverpod
|
||||
# Сравнительный отчет DI-бенчмарка: cherrypick vs get_it vs riverpod vs kiwi
|
||||
|
||||
## Параметры запуска:
|
||||
- chainCount = 100
|
||||
- nestingDepth = 100
|
||||
- repeat = 5
|
||||
- warmup = 2
|
||||
|
||||
## Описание сценариев
|
||||
|
||||
1. **RegisterSingleton** — регистрация и получение объекта-синглтона (базовая скорость DI).
|
||||
1. **RegisterSingleton** — регистрация и получение singleton (базовая скорость DI).
|
||||
2. **ChainSingleton** — цепочка зависимостей A → B → ... → N (singleton). Глубокий singleton-резолвинг.
|
||||
3. **ChainFactory** — все элементы цепочки — фабрики. Stateless построение графа.
|
||||
4. **AsyncChain** — асинхронная цепочка (async factory). Тестирует async/await граф.
|
||||
3. **ChainFactory** — все элементы цепочки — factory. Stateless построение графа.
|
||||
4. **AsyncChain** — асинхронная цепочка (async factory). Тест async/await графа.
|
||||
5. **Named** — регистрация двух биндингов с именами, разрешение по имени.
|
||||
6. **Override** — регистрация биндинга/цепочки в дочернем scope. Проверка override/scoping.
|
||||
6. **Override** — регистрация биндинга/цепочки в дочернем scope.
|
||||
|
||||
---
|
||||
|
||||
## Сводная таблица: chainCount=10, nestingDepth=10 (Mean, PeakRSS)
|
||||
## Сравнительная таблица: chainCount=100, nestingDepth=100, repeat=5, warmup=2 (среднее время, мкс)
|
||||
|
||||
| Сценарий | cherrypick Mean (мкс) | cherrypick PeakRSS | get_it Mean (мкс) | get_it PeakRSS | riverpod Mean (мкс) | riverpod PeakRSS |
|
||||
|--------------------|----------------------:|-------------------:|------------------:|---------------:|--------------------:|-----------------:|
|
||||
| RegisterSingleton | 13.00 | 273104 | 8.40 | 261872 | 9.80 | 268512 |
|
||||
| ChainSingleton | 13.80 | 271072 | 2.00 | 262000 | 33.60 | 268784 |
|
||||
| ChainFactory | 5.00 | 299216 | 4.00 | 297136 | 22.80 | 271296 |
|
||||
| AsyncChain | 28.60 | 290640 | 24.60 | 342976 | 78.20 | 285920 |
|
||||
| Named | 2.20 | 297008 | 0.20 | 449824 | 6.20 | 281136 |
|
||||
| Override | 7.00 | 297024 | 0.00 | 449824 | 30.20 | 281152 |
|
||||
| Сценарий | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|-------|----------|
|
||||
| chainSingleton | 20.6 | 14.8 | 275.2 | 47.0 | 82.8 |
|
||||
| chainFactory | 90.6 | 71.6 | 357.0 | 46.2 | 79.6 |
|
||||
| register | 82.6 | 10.2 | 252.6 | 43.6 | 224.0 |
|
||||
| named | 18.4 | 9.4 | 12.2 | 10.2 | 10.8 |
|
||||
| override | 170.6 | 11.2 | 301.4 | 51.4 | 146.4 |
|
||||
| chainAsync | 493.8 | 34.0 | 5,039.0 | – | 87.2 |
|
||||
|
||||
## Максимальная нагрузка: chainCount=100, nestingDepth=100 (Mean, PeakRSS)
|
||||
|
||||
| Сценарий | cherrypick Mean (мкс) | cherrypick PeakRSS | get_it Mean (мкс) | get_it PeakRSS | riverpod Mean (мкс) | riverpod PeakRSS |
|
||||
|--------------------|----------------------:|-------------------:|------------------:|---------------:|--------------------:|-----------------:|
|
||||
| RegisterSingleton | 4.00 | 271072 | 1.00 | 262000 | 2.00 | 268688 |
|
||||
| ChainSingleton | 76.60 | 303312 | 2.00 | 297136 | 221.80 | 270784 |
|
||||
| ChainFactory | 80.00 | 293952 | 39.20 | 342720 | 195.80 | 308640 |
|
||||
| AsyncChain | 251.40 | 297008 | 18.20 | 450640 | 748.80 | 285968 |
|
||||
| Named | 2.20 | 297008 | 0.00 | 449824 | 1.00 | 281136 |
|
||||
| Override | 104.80 | 301632 | 2.20 | 477344 | 120.80 | 294752 |
|
||||
## Пиковое потребление памяти (Peak RSS, Кб)
|
||||
|
||||
| Сценарий | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|--------|----------|
|
||||
| chainSingleton | 338,224 | 326,752| 301,856 | 195,520| 320,928 |
|
||||
| chainFactory | 339,040 | 335,712| 304,832 | 319,952| 318,688 |
|
||||
| register | 333,760 | 334,208| 300,368 | 327,968| 326,736 |
|
||||
| named | 241,040 | 229,632| 280,144 | 271,872| 266,352 |
|
||||
| override | 356,912 | 331,456| 329,808 | 369,104| 304,416 |
|
||||
| chainAsync | 311,616 | 434,592| 301,168 | – | 328,912 |
|
||||
|
||||
---
|
||||
|
||||
## Краткий анализ и рекомендации
|
||||
|
||||
- **get_it** всегда лидер, особенно на глубине/асинхронных графах.
|
||||
- **cherrypick** заметно быстрее riverpod на сложных сценариях, опережая его в разы.
|
||||
- **riverpod** подходит только для простых/небольших графов — при росте глубины или async/override резко проигрывает по скорости.
|
||||
- **get_it** — абсолютный лидер по скорости и памяти на всех графах (минимальная задержка, небольшой peak RSS в любых цепочках).
|
||||
- **kiwi** — минимальное потребление памяти в chainSingleton/Factory, но не для асинхронности.
|
||||
- **yx_scope** — очень ровная производительность даже на сложных async/sync-цепях, иногда с пиком в памяти на override/register, но задержки всегда минимальны.
|
||||
- **cherrypick** — стабильнее riverpod, но ощутимо уступает top-3 по латентности на длинных/async-графах; по памяти лучше yx_scope для override/named.
|
||||
- **riverpod** — непригоден для глубоких/async-графов: память и время растут очень сильно.
|
||||
- **Пиковое потребление памяти**: большинство DI держится в районе 320–340 Мб (большие цепи), на мелких named/factory — крайне мало.
|
||||
- **Стабильность**: yx_scope и get_it показывают наименьшие скачки времени; у cherrypick иногда всплески на override/async, у riverpod — на async графе stddev почти равен mean!
|
||||
|
||||
### Рекомендации
|
||||
- Используйте **get_it** для критичных к скорости приложений/сложных графов зависимостей.
|
||||
- Выбирайте **cherrypick** для масштабируемых, тестируемых архитектур, если микросекундная разница не критична.
|
||||
- **riverpod** уместен только для реактивного UI или простых графов DI.
|
||||
- Используйте **get_it** (или **kiwi**, если не нужен async) для максимальной производительности и минимального пикового использования памяти.
|
||||
- **yx_scope** — идеально для production-графов с миксом sync/async.
|
||||
- **cherrypick** — хорошо для модульных и тестируемых приложений, если не требуется абсолютная “микросекундная” производительность.
|
||||
- **riverpod** — только если граф плоский или нужно DI только для UI во Flutter.
|
||||
|
||||
---
|
||||
|
||||
_Обновлено: 8 августа 2025_
|
||||
_Обновлено: 20 августа 2025._
|
||||
|
||||
@@ -2,4 +2,4 @@ import 'package:benchmark_di/cli/benchmark_cli.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
await BenchmarkCliRunner().run(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,8 @@ class UniversalChainBenchmark<TContainer> extends BenchmarkBase {
|
||||
_childDi!.resolve<UniversalService>();
|
||||
break;
|
||||
case UniversalScenario.asyncChain:
|
||||
throw UnsupportedError('asyncChain supported only in UniversalChainAsyncBenchmark');
|
||||
throw UnsupportedError(
|
||||
'asyncChain supported only in UniversalChainAsyncBenchmark');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:benchmark_di/cli/report/markdown_report.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_universal_container.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
@@ -16,6 +18,8 @@ import 'package:benchmark_di/benchmarks/universal_chain_async_benchmark.dart';
|
||||
import 'package:benchmark_di/di_adapters/cherrypick_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/get_it_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/riverpod_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/kiwi_adapter.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
|
||||
/// Command-line interface (CLI) runner for benchmarks.
|
||||
///
|
||||
@@ -36,8 +40,11 @@ class BenchmarkCliRunner {
|
||||
if (config.di == 'getit') {
|
||||
final di = GetItAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<GetIt>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<GetIt>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -45,8 +52,41 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<GetIt>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<GetIt>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
}
|
||||
} else if (config.di == 'kiwi') {
|
||||
final di = KiwiAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
// UnsupportedError будет выброшен адаптером, но если дойдёт — вызывать async benchmark
|
||||
final benchAsync = UniversalChainAsyncBenchmark<KiwiContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<KiwiContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -57,8 +97,12 @@ class BenchmarkCliRunner {
|
||||
} else if (config.di == 'riverpod') {
|
||||
final di = RiverpodAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Map<String, rp.ProviderBase<Object?>>>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<
|
||||
Map<String, rp.ProviderBase<Object?>>> (
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -66,8 +110,41 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<Map<String, rp.ProviderBase<Object?>>>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<
|
||||
Map<String, rp.ProviderBase<Object?>>> (
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
}
|
||||
} else if (config.di == 'yx_scope') {
|
||||
final di = YxScopeAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<UniversalYxScopeContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<UniversalYxScopeContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -78,8 +155,11 @@ class BenchmarkCliRunner {
|
||||
} else {
|
||||
final di = CherrypickDIAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Scope>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Scope>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -87,8 +167,12 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<Scope>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<Scope>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -103,7 +187,11 @@ class BenchmarkCliRunner {
|
||||
var median = timings[timings.length ~/ 2];
|
||||
var minVal = timings.first;
|
||||
var maxVal = timings.last;
|
||||
var stddev = timings.isEmpty ? 0 : sqrt(timings.map((x) => pow(x - mean, 2)).reduce((a, b) => a + b) / timings.length);
|
||||
var stddev = timings.isEmpty
|
||||
? 0
|
||||
: sqrt(
|
||||
timings.map((x) => pow(x - mean, 2)).reduce((a, b) => a + b) /
|
||||
timings.length);
|
||||
results.add({
|
||||
'benchmark': 'Universal_$bench',
|
||||
'chainCount': c,
|
||||
@@ -128,6 +216,7 @@ class BenchmarkCliRunner {
|
||||
'json': JsonReport(),
|
||||
'markdown': MarkdownReport(),
|
||||
};
|
||||
print(reportGenerators[config.format]?.render(results) ?? PrettyReport().render(results));
|
||||
print(reportGenerators[config.format]?.render(results) ??
|
||||
PrettyReport().render(results));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,19 @@ import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
enum UniversalBenchmark {
|
||||
/// Simple singleton registration benchmark
|
||||
registerSingleton,
|
||||
|
||||
/// Chain of singleton dependencies
|
||||
chainSingleton,
|
||||
|
||||
/// Chain using factories
|
||||
chainFactory,
|
||||
|
||||
/// Async chain resolution
|
||||
chainAsync,
|
||||
|
||||
/// Named registration benchmark
|
||||
named,
|
||||
|
||||
/// Override/child-scope benchmark
|
||||
override,
|
||||
}
|
||||
@@ -65,23 +70,32 @@ T parseEnum<T>(String value, List<T> values, T defaultValue) {
|
||||
}
|
||||
|
||||
/// Parses comma-separated integer list from [s].
|
||||
List<int> parseIntList(String s) =>
|
||||
s.split(',').map((e) => int.tryParse(e.trim()) ?? 0).where((x) => x > 0).toList();
|
||||
List<int> parseIntList(String s) => s
|
||||
.split(',')
|
||||
.map((e) => int.tryParse(e.trim()) ?? 0)
|
||||
.where((x) => x > 0)
|
||||
.toList();
|
||||
|
||||
/// CLI config describing what and how to benchmark.
|
||||
class BenchmarkCliConfig {
|
||||
/// Benchmarks enabled to run (scenarios).
|
||||
final List<UniversalBenchmark> benchesToRun;
|
||||
|
||||
/// List of chain counts (parallel, per test).
|
||||
final List<int> chainCounts;
|
||||
|
||||
/// List of nesting depths (max chain length, per test).
|
||||
final List<int> nestDepths;
|
||||
|
||||
/// How many times to repeat each trial.
|
||||
final int repeats;
|
||||
|
||||
/// How many times to warm-up before measuring.
|
||||
final int warmups;
|
||||
|
||||
/// Output report format.
|
||||
final String format;
|
||||
|
||||
/// Name of DI implementation ("cherrypick" or "getit")
|
||||
final String di;
|
||||
BenchmarkCliConfig({
|
||||
@@ -105,7 +119,9 @@ BenchmarkCliConfig parseBenchmarkCli(List<String> args) {
|
||||
..addOption('repeat', abbr: 'r', defaultsTo: '2')
|
||||
..addOption('warmup', abbr: 'w', defaultsTo: '1')
|
||||
..addOption('format', abbr: 'f', defaultsTo: 'pretty')
|
||||
..addOption('di', defaultsTo: 'cherrypick', help: 'DI implementation: cherrypick, getit or riverpod')
|
||||
..addOption('di',
|
||||
defaultsTo: 'cherrypick',
|
||||
help: 'DI implementation: cherrypick, getit or riverpod')
|
||||
..addFlag('help', abbr: 'h', negatable: false, help: 'Show help');
|
||||
final result = parser.parse(args);
|
||||
if (result['help'] == true) {
|
||||
@@ -127,4 +143,4 @@ BenchmarkCliConfig parseBenchmarkCli(List<String> args) {
|
||||
format: result['format'] as String,
|
||||
di: result['di'] as String? ?? 'cherrypick',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,20 +5,32 @@ class CsvReport extends ReportGenerator {
|
||||
/// List of all keys/columns to include in the CSV output.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','timings_us','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'timings_us',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Renders rows as a CSV table string.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final header = keys.join(',');
|
||||
final lines = rows.map((r) =>
|
||||
keys.map((k) {
|
||||
final v = r[k];
|
||||
if (v is List) return '"${v.join(';')}"';
|
||||
return (v ?? '').toString();
|
||||
}).join(',')
|
||||
).toList();
|
||||
final lines = rows
|
||||
.map((r) => keys.map((k) {
|
||||
final v = r[k];
|
||||
if (v is List) return '"${v.join(';')}"';
|
||||
return (v ?? '').toString();
|
||||
}).join(','))
|
||||
.toList();
|
||||
return ([header] + lines).join('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@ class JsonReport extends ReportGenerator {
|
||||
/// No specific keys; outputs all fields in raw map.
|
||||
@override
|
||||
List<String> get keys => [];
|
||||
|
||||
/// Renders all result rows as a pretty-printed JSON array.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
return '[\n${rows.map((r) => ' $r').join(',\n')}\n]';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,25 +7,46 @@ class MarkdownReport extends ReportGenerator {
|
||||
/// List of columns (keys) to show in the Markdown table.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Friendly display names for each benchmark type.
|
||||
static const nameMap = {
|
||||
'Universal_UniversalBenchmark.registerSingleton':'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton':'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory':'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync':'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named':'Named',
|
||||
'Universal_UniversalBenchmark.override':'Override',
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
};
|
||||
|
||||
/// Renders all results as a formatted Markdown table with aligned columns and a legend.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final headers = [
|
||||
'Benchmark', 'Chain Count', 'Depth', 'Mean (us)', 'Median', 'Stddev', 'Min', 'Max', 'N', 'ΔRSS(KB)', 'ΔPeak(KB)', 'PeakRSS(KB)'
|
||||
'Benchmark',
|
||||
'Chain Count',
|
||||
'Depth',
|
||||
'Mean (us)',
|
||||
'Median',
|
||||
'Stddev',
|
||||
'Min',
|
||||
'Max',
|
||||
'N',
|
||||
'ΔRSS(KB)',
|
||||
'ΔPeak(KB)',
|
||||
'PeakRSS(KB)'
|
||||
];
|
||||
final dataRows = rows.map((r) {
|
||||
final readableName = nameMap[r['benchmark']] ?? r['benchmark'];
|
||||
@@ -73,6 +94,6 @@ class MarkdownReport extends ReportGenerator {
|
||||
> `PeakRSS(KB)` – Max observed RSS memory (KB)
|
||||
''';
|
||||
|
||||
return '$legend\n\n${([headerLine, divider] + lines).join('\n')}' ;
|
||||
return '$legend\n\n${([headerLine, divider] + lines).join('\n')}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,25 +7,46 @@ class PrettyReport extends ReportGenerator {
|
||||
/// List of columns to output in the pretty report.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Mappings from internal benchmark IDs to display names.
|
||||
static const nameMap = {
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
};
|
||||
|
||||
/// Renders the results as a header + tab-separated value table.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final headers = [
|
||||
'Benchmark', 'Chain Count', 'Depth', 'Mean (us)', 'Median', 'Stddev', 'Min', 'Max', 'N', 'ΔRSS(KB)', 'ΔPeak(KB)', 'PeakRSS(KB)'
|
||||
'Benchmark',
|
||||
'Chain Count',
|
||||
'Depth',
|
||||
'Mean (us)',
|
||||
'Median',
|
||||
'Stddev',
|
||||
'Min',
|
||||
'Max',
|
||||
'N',
|
||||
'ΔRSS(KB)',
|
||||
'ΔPeak(KB)',
|
||||
'PeakRSS(KB)'
|
||||
];
|
||||
final header = headers.join('\t');
|
||||
final lines = rows.map((r) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
abstract class ReportGenerator {
|
||||
/// Renders the given [results] as a formatted string (table, markdown, csv, etc).
|
||||
String render(List<Map<String, dynamic>> results);
|
||||
|
||||
/// List of output columns/keys included in the export (or [] for auto/all).
|
||||
List<String> get keys;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,13 @@ import 'package:benchmark_di/benchmarks/universal_chain_async_benchmark.dart';
|
||||
class BenchmarkResult {
|
||||
/// List of timings for each run (in microseconds).
|
||||
final List<num> timings;
|
||||
|
||||
/// Difference in memory (RSS, in KB) after running.
|
||||
final int memoryDiffKb;
|
||||
|
||||
/// Difference between peak RSS and initial RSS (in KB).
|
||||
final int deltaPeakKb;
|
||||
|
||||
/// Peak RSS memory observed (in KB).
|
||||
final int peakRssKb;
|
||||
BenchmarkResult({
|
||||
@@ -19,6 +22,7 @@ class BenchmarkResult {
|
||||
required this.deltaPeakKb,
|
||||
required this.peakRssKb,
|
||||
});
|
||||
|
||||
/// Computes a BenchmarkResult instance from run timings and memory data.
|
||||
factory BenchmarkResult.collect({
|
||||
required List<num> timings,
|
||||
@@ -64,7 +68,8 @@ class BenchmarkRunner {
|
||||
rssValues.add(ProcessInfo.currentRss);
|
||||
benchmark.teardown();
|
||||
}
|
||||
return BenchmarkResult.collect(timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
return BenchmarkResult.collect(
|
||||
timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
}
|
||||
|
||||
/// Runs an asynchronous benchmark ([UniversalChainAsyncBenchmark]) for a given number of [warmups] and [repeats].
|
||||
@@ -91,6 +96,7 @@ class BenchmarkRunner {
|
||||
rssValues.add(ProcessInfo.currentRss);
|
||||
await benchmark.teardown();
|
||||
}
|
||||
return BenchmarkResult.collect(timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
return BenchmarkResult.collect(
|
||||
timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
|
||||
/// Test module that generates a chain of service bindings for benchmarking.
|
||||
///
|
||||
/// Configurable by chain count, nesting depth, binding mode, and scenario
|
||||
@@ -12,10 +11,13 @@ import 'di_adapter.dart';
|
||||
class UniversalChainModule extends Module {
|
||||
/// Number of chains to create.
|
||||
final int chainCount;
|
||||
|
||||
/// Depth of each chain.
|
||||
final int nestingDepth;
|
||||
|
||||
/// How modules are registered (factory/singleton/async).
|
||||
final UniversalBindingMode bindingMode;
|
||||
|
||||
/// Which di scenario to generate (chained, named, etc).
|
||||
final UniversalScenario scenario;
|
||||
|
||||
@@ -38,17 +40,18 @@ class UniversalChainModule extends Module {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
bind<UniversalService>()
|
||||
.toProvideAsync(() async {
|
||||
final prev = level > 1
|
||||
? await currentScope.resolveAsync<UniversalService>(named: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: prev,
|
||||
);
|
||||
})
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
.toProvideAsync(() async {
|
||||
final prev = level > 1
|
||||
? await currentScope.resolveAsync<UniversalService>(
|
||||
named: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: prev,
|
||||
);
|
||||
})
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -58,13 +61,18 @@ class UniversalChainModule extends Module {
|
||||
case UniversalScenario.register:
|
||||
// Simple singleton registration.
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'reg', dependency: null))
|
||||
.toProvide(
|
||||
() => UniversalServiceImpl(value: 'reg', dependency: null))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
// Named factory registration for two distinct objects.
|
||||
bind<UniversalService>().toProvide(() => UniversalServiceImpl(value: 'impl1')).withName('impl1');
|
||||
bind<UniversalService>().toProvide(() => UniversalServiceImpl(value: 'impl2')).withName('impl2');
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'impl1'))
|
||||
.withName('impl1');
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'impl2'))
|
||||
.withName('impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
// Chain of nested services, with dependency on previous level by name.
|
||||
@@ -79,7 +87,8 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: currentScope.tryResolve<UniversalService>(named: prevDepName),
|
||||
dependency: currentScope.tryResolve<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
@@ -88,7 +97,8 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: currentScope.tryResolve<UniversalService>(named: prevDepName),
|
||||
dependency: currentScope.tryResolve<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName);
|
||||
break;
|
||||
@@ -96,7 +106,9 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvideAsync(() async => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: await currentScope.resolveAsync<UniversalService>(named: prevDepName),
|
||||
dependency:
|
||||
await currentScope.resolveAsync<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
@@ -107,14 +119,16 @@ class UniversalChainModule extends Module {
|
||||
// Регистрация алиаса без имени (на последний элемент цепочки)
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.toProvide(
|
||||
() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level, но алиас нужен прямо в этом scope!
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.toProvide(
|
||||
() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.asyncChain:
|
||||
@@ -124,7 +138,6 @@ class UniversalChainModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
Scope? _scope;
|
||||
final bool _isSubScope;
|
||||
@@ -158,7 +171,8 @@ class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
]);
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by CherrypickDIAdapter');
|
||||
throw UnsupportedError(
|
||||
'Scenario $scenario not supported by CherrypickDIAdapter');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -170,9 +184,9 @@ class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
_scope!.resolveAsync<T>(named: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
Future<void> teardown() async {
|
||||
if (!_isSubScope) {
|
||||
CherryPick.closeRootScope();
|
||||
await CherryPick.closeRootScope();
|
||||
_scope = null;
|
||||
}
|
||||
// SubScope teardown не требуется
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
|
||||
/// Универсальная абстракция для DI-адаптера с унифицированной функцией регистрации.
|
||||
/// Теперь для каждого адаптера задаём строгий generic тип контейнера.
|
||||
typedef Registration<TContainer> = void Function(TContainer);
|
||||
|
||||
@@ -80,9 +80,11 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
getIt.registerSingletonAsync<UniversalService>(
|
||||
() async {
|
||||
final prev = level > 1
|
||||
? await getIt.getAsync<UniversalService>(instanceName: prevDepName)
|
||||
? await getIt.getAsync<UniversalService>(
|
||||
instanceName: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(value: depName, dependency: prev);
|
||||
return UniversalServiceImpl(
|
||||
value: depName, dependency: prev);
|
||||
},
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -90,11 +92,16 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
getIt.registerSingleton<UniversalService>(UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
getIt.registerSingleton<UniversalService>(
|
||||
UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
getIt.registerFactory<UniversalService>(() => UniversalServiceImpl(value: 'impl1'), instanceName: 'impl1');
|
||||
getIt.registerFactory<UniversalService>(() => UniversalServiceImpl(value: 'impl2'), instanceName: 'impl2');
|
||||
getIt.registerFactory<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl1'),
|
||||
instanceName: 'impl1');
|
||||
getIt.registerFactory<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl2'),
|
||||
instanceName: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
@@ -107,8 +114,8 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? getIt<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
? getIt<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
),
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -129,8 +136,9 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
() async => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? await getIt.getAsync<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
? await getIt.getAsync<UniversalService>(
|
||||
instanceName: prevDepName)
|
||||
: null,
|
||||
),
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -143,7 +151,8 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
// handled at benchmark level
|
||||
break;
|
||||
}
|
||||
if (scenario == UniversalScenario.chain || scenario == UniversalScenario.override) {
|
||||
if (scenario == UniversalScenario.chain ||
|
||||
scenario == UniversalScenario.override) {
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
getIt.registerSingleton<UniversalService>(
|
||||
getIt<UniversalService>(instanceName: depName),
|
||||
|
||||
129
benchmark_di/lib/di_adapters/kiwi_adapter.dart
Normal file
129
benchmark_di/lib/di_adapters/kiwi_adapter.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
/// DIAdapter-для KiwiContainer с поддержкой universal benchmark сценариев.
|
||||
class KiwiAdapter extends DIAdapter<KiwiContainer> {
|
||||
late KiwiContainer _container;
|
||||
// ignore: unused_field
|
||||
final bool _isSubScope;
|
||||
|
||||
KiwiAdapter({KiwiContainer? container, bool isSubScope = false})
|
||||
: _isSubScope = isSubScope {
|
||||
_container = container ?? KiwiContainer();
|
||||
}
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(KiwiContainer container) registration) {
|
||||
registration(_container);
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<KiwiContainer> universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
required UniversalBindingMode bindingMode,
|
||||
}) {
|
||||
if (scenario is UniversalScenario) {
|
||||
if (scenario == UniversalScenario.asyncChain ||
|
||||
bindingMode == UniversalBindingMode.asyncStrategy) {
|
||||
throw UnsupportedError('Kiwi does not support async dependencies or async binding scenarios.');
|
||||
}
|
||||
return (container) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.asyncChain:
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'reg', dependency: null),
|
||||
);
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'impl1'), name: 'impl1');
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'impl2'), name: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
switch (bindingMode) {
|
||||
case UniversalBindingMode.singletonStrategy:
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? c.resolve<UniversalService>(prevDepName)
|
||||
: null),
|
||||
name: depName);
|
||||
break;
|
||||
case UniversalBindingMode.factoryStrategy:
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? c.resolve<UniversalService>(prevDepName)
|
||||
: null),
|
||||
name: depName);
|
||||
break;
|
||||
case UniversalBindingMode.asyncStrategy:
|
||||
// Не поддерживается
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => c.resolve<UniversalService>(depName));
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => c.resolve<UniversalService>(depName));
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by KiwiAdapter');
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) {
|
||||
// Для asyncChain нужен resolve<Future<T>>
|
||||
if (T.toString().startsWith('Future<')) {
|
||||
return _container.resolve<T>(named);
|
||||
} else {
|
||||
return _container.resolve<T>(named);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async {
|
||||
if (T.toString().startsWith('Future<')) {
|
||||
// resolve<Future<T>>, unwrap result
|
||||
return Future.value(_container.resolve<T>(named));
|
||||
} else {
|
||||
// Для совместимости с chain/override
|
||||
return Future.value(_container.resolve<T>(named));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
_container.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
KiwiAdapter openSubScope(String name) {
|
||||
// Возвращаем новый scoped контейнер (отдельный). Наследование не реализовано.
|
||||
return KiwiAdapter(container: KiwiContainer.scoped(), isSubScope: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {}
|
||||
}
|
||||
@@ -20,7 +20,9 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
_parent = parent;
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(Map<String, rp.ProviderBase<Object?>> container) registration) {
|
||||
void setupDependencies(
|
||||
void Function(Map<String, rp.ProviderBase<Object?>> container)
|
||||
registration) {
|
||||
_container ??= _parent == null
|
||||
? rp.ProviderContainer()
|
||||
: rp.ProviderContainer(parent: _parent);
|
||||
@@ -76,7 +78,8 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<Map<String, rp.ProviderBase<Object?>>> universalRegistration<S extends Enum>({
|
||||
Registration<Map<String, rp.ProviderBase<Object?>>>
|
||||
universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
@@ -86,25 +89,34 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
return (providers) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.register:
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
providers['impl1'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'impl1'));
|
||||
providers['impl2'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'impl2'));
|
||||
providers['impl1'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'impl1'));
|
||||
providers['impl2'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'impl2'));
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
providers[depName] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1 ? ref.watch(providers[prevDepName] as rp.ProviderBase<UniversalService>) : null,
|
||||
));
|
||||
providers[depName] =
|
||||
rp.Provider<UniversalService>((ref) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? ref.watch(providers[prevDepName]
|
||||
as rp.ProviderBase<UniversalService>)
|
||||
: null,
|
||||
));
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>((ref) => ref.watch(providers[depName] as rp.ProviderBase<UniversalService>));
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>(
|
||||
(ref) => ref.watch(
|
||||
providers[depName] as rp.ProviderBase<UniversalService>));
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level
|
||||
@@ -114,24 +126,31 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
providers[depName] = rp.FutureProvider<UniversalService>((ref) async {
|
||||
providers[depName] =
|
||||
rp.FutureProvider<UniversalService>((ref) async {
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? await ref.watch((providers[prevDepName] as rp.FutureProvider<UniversalService>).future) as UniversalService?
|
||||
? await ref.watch((providers[prevDepName]
|
||||
as rp.FutureProvider<UniversalService>)
|
||||
.future) as UniversalService?
|
||||
: null,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
providers['UniversalService'] = rp.FutureProvider<UniversalService>((ref) async {
|
||||
return await ref.watch((providers[depName] as rp.FutureProvider<UniversalService>).future);
|
||||
providers['UniversalService'] =
|
||||
rp.FutureProvider<UniversalService>((ref) async {
|
||||
return await ref.watch(
|
||||
(providers[depName] as rp.FutureProvider<UniversalService>)
|
||||
.future);
|
||||
});
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by RiverpodAdapter');
|
||||
throw UnsupportedError(
|
||||
'Scenario $scenario not supported by RiverpodAdapter');
|
||||
}
|
||||
}
|
||||
|
||||
126
benchmark_di/lib/di_adapters/yx_scope_adapter.dart
Normal file
126
benchmark_di/lib/di_adapters/yx_scope_adapter.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
// ignore_for_file: invalid_use_of_protected_member
|
||||
|
||||
import 'package:benchmark_di/di_adapters/di_adapter.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_universal_container.dart';
|
||||
|
||||
/// DIAdapter для yx_scope UniversalYxScopeContainer
|
||||
class YxScopeAdapter extends DIAdapter<UniversalYxScopeContainer> {
|
||||
late UniversalYxScopeContainer _scope;
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(UniversalYxScopeContainer container) registration) {
|
||||
_scope = UniversalYxScopeContainer();
|
||||
registration(_scope);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) {
|
||||
return _scope.depFor<T>(name: named).get;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async {
|
||||
return resolve<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
// У yx_scope нет явного dispose на ScopeContainer, но можно добавить очистку Map/Deps если потребуется
|
||||
// Ничего не делаем
|
||||
}
|
||||
|
||||
@override
|
||||
YxScopeAdapter openSubScope(String name) {
|
||||
// Для простоты всегда возвращаем новый контейнер, сабскоупы не реализованы явно
|
||||
return YxScopeAdapter();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {
|
||||
// Все зависимости синхронны
|
||||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<UniversalYxScopeContainer> universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
required UniversalBindingMode bindingMode,
|
||||
}) {
|
||||
if (scenario is UniversalScenario) {
|
||||
return (scope) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.asyncChain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? scope.depFor<UniversalService>(name: prevDepName).get
|
||||
: null,
|
||||
),
|
||||
name: depName,
|
||||
);
|
||||
scope.register<UniversalService>(dep, name: depName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'reg', dependency: null),
|
||||
);
|
||||
scope.register<UniversalService>(dep);
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
final dep1 = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl1'),
|
||||
name: 'impl1',
|
||||
);
|
||||
final dep2 = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl2'),
|
||||
name: 'impl2',
|
||||
);
|
||||
scope.register<UniversalService>(dep1, name: 'impl1');
|
||||
scope.register<UniversalService>(dep2, name: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? scope.depFor<UniversalService>(name: prevDepName).get
|
||||
: null,
|
||||
),
|
||||
name: depName,
|
||||
);
|
||||
scope.register<UniversalService>(dep, name: depName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level
|
||||
break;
|
||||
}
|
||||
if (scenario == UniversalScenario.chain || scenario == UniversalScenario.override) {
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
final lastDep = scope.dep<UniversalService>(
|
||||
() => scope.depFor<UniversalService>(name: depName).get,
|
||||
);
|
||||
scope.register<UniversalService>(lastDep);
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by YxScopeAdapter');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:yx_scope/yx_scope.dart';
|
||||
|
||||
/// Universal container for dynamic DI registration in yx_scope (for benchmarks).
|
||||
/// Allows to register and resolve deps by name/type at runtime.
|
||||
class UniversalYxScopeContainer extends ScopeContainer {
|
||||
final Map<String, Dep<dynamic>> _namedDeps = {};
|
||||
final Map<Type, Dep<dynamic>> _typedDeps = {};
|
||||
|
||||
void register<T>(Dep<T> dep, {String? name}) {
|
||||
if (name != null) {
|
||||
_namedDeps[_depKey<T>(name)] = dep;
|
||||
} else {
|
||||
_typedDeps[T] = dep;
|
||||
}
|
||||
}
|
||||
|
||||
Dep<T> depFor<T>({String? name}) {
|
||||
if (name != null) {
|
||||
final dep = _namedDeps[_depKey<T>(name)];
|
||||
if (dep is Dep<T>) return dep;
|
||||
throw Exception('No dep for type $T/$name');
|
||||
} else {
|
||||
final dep = _typedDeps[T];
|
||||
if (dep is Dep<T>) return dep;
|
||||
throw Exception('No dep for type $T');
|
||||
}
|
||||
}
|
||||
|
||||
static String _depKey<T>(String name) => '$T@$name';
|
||||
}
|
||||
@@ -2,12 +2,16 @@
|
||||
enum UniversalScenario {
|
||||
/// Single registration.
|
||||
register,
|
||||
|
||||
/// Chain of dependencies.
|
||||
chain,
|
||||
|
||||
/// Named registrations.
|
||||
named,
|
||||
|
||||
/// Child-scope override scenario.
|
||||
override,
|
||||
|
||||
/// Asynchronous chain scenario.
|
||||
asyncChain,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
/// Base interface for any universal service in the benchmarks.
|
||||
///
|
||||
/// Represents an object in the dependency chain with an identifiable value
|
||||
@@ -6,6 +5,7 @@
|
||||
abstract class UniversalService {
|
||||
/// String ID for this service instance (e.g. chain/level info).
|
||||
final String value;
|
||||
|
||||
/// Optional reference to dependency service in the chain.
|
||||
final UniversalService? dependency;
|
||||
UniversalService({required this.value, this.dependency});
|
||||
@@ -14,4 +14,4 @@ abstract class UniversalService {
|
||||
/// Default implementation for [UniversalService] used in service chains.
|
||||
class UniversalServiceImpl extends UniversalService {
|
||||
UniversalServiceImpl({required super.value, super.dependency});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ packages:
|
||||
path: "../cherrypick"
|
||||
relative: true
|
||||
source: path
|
||||
version: "3.0.0-dev.5"
|
||||
version: "3.0.0-dev.12"
|
||||
collection:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -72,6 +72,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.2.0"
|
||||
kiwi:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: kiwi
|
||||
sha256: d078364a90fb1b93852bb74468efdf4aaae35c036c538c1cf4f9c74a19df9a61
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.1"
|
||||
lazy_memo:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -128,5 +136,13 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
yx_scope:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: yx_scope
|
||||
sha256: "9ba98b442261596311363bf7361622e5ccc67189705b8d042ca23c9de366f8bf"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.2"
|
||||
sdks:
|
||||
dart: ">=3.6.0 <4.0.0"
|
||||
|
||||
@@ -12,6 +12,8 @@ dependencies:
|
||||
args: ^2.7.0
|
||||
get_it: ^8.2.0
|
||||
riverpod: ^2.6.1
|
||||
kiwi: ^5.0.1
|
||||
yx_scope: ^1.1.2
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^5.0.0
|
||||
|
||||
@@ -1,3 +1,33 @@
|
||||
## 3.0.0-dev.12
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
## 3.0.0-dev.11
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
## 3.0.0-dev.10
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
## 3.0.0-dev.9
|
||||
|
||||
- **DOCS**(readme): add talker_cherrypick_logger to Additional Modules section.
|
||||
- **DOCS**(api): improve all DI core code documentation with English dartdoc and examples.
|
||||
|
||||
## 3.0.0-dev.8
|
||||
|
||||
- **REFACTOR**(tests): replace MockLogger with MockObserver in scope tests to align with updated observer API.
|
||||
- **FIX**(doc): remove hide symbol.
|
||||
- **FEAT**(core): add full DI lifecycle observability via onInstanceDisposed.
|
||||
- **DOCS**(logging): update Logging section in README with modern Observer usage and Talker integration examples.
|
||||
- **DOCS**(observer): improve documentation, translate all comments to English, add usage examples.
|
||||
- **DOCS**(README): add section with overview table for additional modules.
|
||||
- **DOCS**(README): refactor structure and improve clarity of advanced features.
|
||||
- **DOCS**(README): add 'Hierarchical Subscopes' section and update structure for advanced features clarity.
|
||||
|
||||
## 3.0.0-dev.7
|
||||
|
||||
> Note: This release has breaking changes.
|
||||
|
||||
@@ -13,7 +13,7 @@ It provides an easy-to-use system for registering, scoping, and resolving depend
|
||||
- [Binding](#binding)
|
||||
- [Module](#module)
|
||||
- [Scope](#scope)
|
||||
- [Automatic Resource Cleanup with Disposable](#automatic-resource-cleanup-with-disposable)
|
||||
- [Disposable](#disposable)
|
||||
- [Dependency Resolution API](#dependency-resolution-api)
|
||||
- [Using Annotations & Code Generation](#using-annotations--code-generation)
|
||||
- [Advanced Features](#advanced-features)
|
||||
@@ -24,6 +24,7 @@ It provides an easy-to-use system for registering, scoping, and resolving depend
|
||||
- [Example Application](#example-application)
|
||||
- [FAQ](#faq)
|
||||
- [Documentation Links](#documentation-links)
|
||||
- [Additional Modules](#additional-modules)
|
||||
- [Contributing](#contributing)
|
||||
- [License](#license)
|
||||
|
||||
@@ -50,13 +51,14 @@ Add to your `pubspec.yaml`:
|
||||
```yaml
|
||||
dependencies:
|
||||
cherrypick: ^<latest_version>
|
||||
```
|
||||
````
|
||||
|
||||
Then run:
|
||||
|
||||
```shell
|
||||
dart pub get
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Getting Started
|
||||
@@ -66,7 +68,6 @@ Here is a minimal example that registers and resolves a dependency:
|
||||
```dart
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
@@ -92,40 +93,107 @@ await CherryPick.closeRootScope();
|
||||
|
||||
A **Binding** acts as a configuration for how to create or provide a particular dependency. Bindings support:
|
||||
|
||||
- Direct instance assignment (`toInstance()`, `toInstanceAsync()`)
|
||||
- Lazy providers (sync/async functions)
|
||||
- Provider functions supporting dynamic parameters
|
||||
- Named instances for resolving by string key
|
||||
- Optional singleton lifecycle
|
||||
* Direct instance assignment (`toInstance()`, `toInstanceAsync()`)
|
||||
* Lazy providers (sync/async functions)
|
||||
* Provider functions supporting dynamic parameters
|
||||
* Named instances for resolving by string key
|
||||
* Optional singleton lifecycle
|
||||
|
||||
#### Example
|
||||
|
||||
```dart
|
||||
// Provide a direct instance
|
||||
Binding<String>().toInstance("Hello world");
|
||||
void builder(Scope scope) {
|
||||
// Provide a direct instance
|
||||
bind<String>().toInstance("Hello world");
|
||||
|
||||
// Provide an async direct instance
|
||||
Binding<String>().toInstanceAsync(Future.value("Hello world"));
|
||||
// Provide an async direct instance
|
||||
bind<String>().toInstanceAsync(Future.value("Hello world"));
|
||||
|
||||
// Provide a lazy sync instance using a factory
|
||||
Binding<String>().toProvide(() => "Hello world");
|
||||
// Provide a lazy sync instance using a factory
|
||||
bind<String>().toProvide(() => "Hello world");
|
||||
|
||||
// Provide a lazy async instance using a factory
|
||||
Binding<String>().toProvideAsync(() async => "Hello async world");
|
||||
// Provide a lazy async instance using a factory
|
||||
bind<String>().toProvideAsync(() async => "Hello async world");
|
||||
|
||||
// Provide an instance with dynamic parameters (sync)
|
||||
Binding<String>().toProvideWithParams((params) => "Hello $params");
|
||||
// Provide an instance with dynamic parameters (sync)
|
||||
bind<String>().toProvideWithParams((params) => "Hello $params");
|
||||
|
||||
// Provide an instance with dynamic parameters (async)
|
||||
Binding<String>().toProvideAsyncWithParams((params) async => "Hello $params");
|
||||
// Provide an instance with dynamic parameters (async)
|
||||
bind<String>().toProvideAsyncWithParams((params) async => "Hello $params");
|
||||
|
||||
// Named instance for retrieval by name
|
||||
Binding<String>().toProvide(() => "Hello world").withName("my_string");
|
||||
// Named instance for retrieval by name
|
||||
bind<String>().toProvide(() => "Hello world").withName("my_string");
|
||||
|
||||
// Mark as singleton (only one instance within the scope)
|
||||
Binding<String>().toProvide(() => "Hello world").singleton();
|
||||
// Mark as singleton (only one instance within the scope)
|
||||
bind<String>().toProvide(() => "Hello world").singleton();
|
||||
}
|
||||
```
|
||||
|
||||
> ⚠️ **Important note about using `toInstance` in Module `builder`:**
|
||||
>
|
||||
> If you register a chain of dependencies via `toInstance` inside a Module's `builder`, **do not** call `scope.resolve<T>()` for types that are also being registered in the same builder — at the moment they are registered.
|
||||
>
|
||||
> CherryPick initializes all bindings in the builder sequentially. Dependencies registered earlier are not yet available to `resolve` within the same builder execution. Trying to resolve just-registered types will result in an error (`Can't resolve dependency ...`).
|
||||
>
|
||||
> **How to do it right:**
|
||||
> Manually construct the full dependency chain before calling `toInstance`:
|
||||
>
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> final a = A();
|
||||
> final b = B(a);
|
||||
> final c = C(b);
|
||||
> bind<A>().toInstance(a);
|
||||
> bind<B>().toInstance(b);
|
||||
> bind<C>().toInstance(c);
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Wrong:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toInstance(A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Wrong:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toProvide(() => A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Note:** This limitation applies **only** to `toInstance`. With `toProvide`/`toProvideAsync` and similar providers, you can safely use `scope.resolve<T>()` inside the builder.
|
||||
|
||||
|
||||
> ⚠️ **Special note regarding `.singleton()` with `toProvideWithParams()` / `toProvideAsyncWithParams()`:**
|
||||
>
|
||||
> If you declare a binding using `.toProvideWithParams(...)` (or its async variant) and then chain `.singleton()`, only the **very first** `resolve<T>(params: ...)` will use its parameters; every subsequent call (regardless of params) will return the same (cached) instance.
|
||||
>
|
||||
> **Example:**
|
||||
> ```dart
|
||||
> bind<Service>().toProvideWithParams((params) => Service(params)).singleton();
|
||||
> final a = scope.resolve<Service>(params: 1); // creates Service(1)
|
||||
> final b = scope.resolve<Service>(params: 2); // returns Service(1)
|
||||
> print(identical(a, b)); // true
|
||||
> ```
|
||||
>
|
||||
> Use this pattern only when you want a “master” singleton. If you expect a new instance per params, **do not** use `.singleton()` on parameterized providers.
|
||||
|
||||
|
||||
> ℹ️ **Note about `.singleton()` and `.toInstance()`:**
|
||||
>
|
||||
> Calling `.singleton()` after `.toInstance()` does **not** change the binding’s behavior: the object passed with `toInstance()` is already a single, constant instance that will be always returned for every resolve.
|
||||
>
|
||||
> It is not necessary to use `.singleton()` with an existing object—this call has no effect.
|
||||
>
|
||||
> `.singleton()` is only meaningful with providers (such as `toProvide`/`toProvideAsync`), to ensure only one instance is created by the factory.
|
||||
|
||||
|
||||
### Module
|
||||
|
||||
A **Module** is a logical collection point for bindings, designed for grouping and initializing related dependencies. Implement the `builder` method to define how dependencies should be bound within the scope.
|
||||
@@ -172,7 +240,7 @@ await CherryPick.closeRootScope();
|
||||
|
||||
---
|
||||
|
||||
### Automatic Resource Cleanup with Disposable
|
||||
### Disposable
|
||||
|
||||
CherryPick can automatically clean up any dependency that implements the `Disposable` interface. This makes resource management (for controllers, streams, sockets, files, etc.) easy and reliable—especially when scopes or the app are shut down.
|
||||
|
||||
@@ -223,67 +291,17 @@ await CherryPick.closeRootScope(); // awaits async disposal
|
||||
|
||||
---
|
||||
|
||||
### Automatic resource management (`Disposable`, `dispose`)
|
||||
## Dependency Resolution API
|
||||
|
||||
CherryPick automatically manages the lifecycle of any object registered via DI that implements the `Disposable` interface.
|
||||
- `resolve<T>()` — Locates a dependency instance or throws if missing.
|
||||
- `resolveAsync<T>()` — Async variant for dependencies requiring async binding.
|
||||
- `tryResolve<T>()` — Returns `null` if not found (sync).
|
||||
- `tryResolveAsync<T>()` — Returns `null` async if not found.
|
||||
|
||||
**Best practice:**
|
||||
Always finish your work with `await CherryPick.closeRootScope()` (for the root scope) or `await scope.closeSubScope('key')` (for subscopes).
|
||||
These methods will automatically await `dispose()` on all resolved objects (e.g., singletons) that implement `Disposable`, ensuring proper and complete resource cleanup—sync or async.
|
||||
|
||||
Manual `await scope.dispose()` may be useful if you manually manage custom scopes.
|
||||
|
||||
#### Example
|
||||
|
||||
```dart
|
||||
class MyService implements Disposable {
|
||||
@override
|
||||
FutureOr<void> dispose() async {
|
||||
// release resources, close streams, perform async shutdown, etc.
|
||||
print('MyService disposed!');
|
||||
}
|
||||
}
|
||||
|
||||
final scope = openRootScope();
|
||||
scope.installModules([
|
||||
ModuleImpl(),
|
||||
]);
|
||||
|
||||
final service = scope.resolve<MyService>();
|
||||
|
||||
// ... use service
|
||||
|
||||
// Recommended completion:
|
||||
await CherryPick.closeRootScope(); // will print: MyService disposed!
|
||||
|
||||
// Or, to close and clean up a subscope and its resources:
|
||||
await scope.closeSubScope('feature');
|
||||
|
||||
class ModuleImpl extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<MyService>().toProvide(() => MyService()).singleton();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Working with Subscopes
|
||||
|
||||
```dart
|
||||
// Open a named child scope (e.g., for a feature/module)
|
||||
final subScope = rootScope.openSubScope('featureScope')
|
||||
..installModules([FeatureModule()]);
|
||||
|
||||
// Resolve from subScope, with fallback to parents if missing
|
||||
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
||||
```
|
||||
|
||||
### Fast Dependency Lookup (Performance Improvement)
|
||||
|
||||
> **Performance Note:**
|
||||
> **Starting from version 3.0.0**, CherryPick uses a Map-based resolver index for dependency lookup. This means calls to `resolve<T>()` and related methods are now O(1) operations, regardless of the number of modules or bindings in your scope. Previously, the library had to iterate over all modules and bindings to locate the requested dependency, which could impact performance as your project grew.
|
||||
>
|
||||
> This optimization is internal and does not change any library APIs or usage patterns, but it significantly improves resolution speed in larger applications.
|
||||
Supports:
|
||||
- Synchronous and asynchronous dependencies
|
||||
- Named dependencies
|
||||
- Provider functions with and without runtime parameters
|
||||
|
||||
---
|
||||
|
||||
@@ -337,7 +355,7 @@ class ProfilePage with _\$ProfilePage {
|
||||
}
|
||||
```
|
||||
|
||||
- After running build_runner, the mixin `_ | ||||