Skip to content

Commit e7b242c

Browse files
authored
Merge pull request #93 from Innmind/improve-performance
Improve performance
2 parents 3fddc26 + 5cf5051 commit e7b242c

21 files changed

Lines changed: 343 additions & 351 deletions

‎CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Changed
6+
7+
- `Sequence::toList()` no longer use a quadratic `reduce` to build the list
8+
- `Sequence::distinct()` no longer use a quadratic `reduce` to build the new in memory sequence
9+
- `Map::equals()` no longer apply redundant keys comparison
10+
- Lazy and deferred `Sequence::takeEnd()`/`::dropEnd()` no longer re-index the sliding window on each new value
11+
- `Map` with objects as keys no longer have to lookup the storage to access associated values
12+
- Reduced the number of intermediate objects for `Map::parition()`
13+
- Reduce the number of variables for `Sequence::flatMap()`/`::aggregate()`
14+
- Reduce the number of variables to handle deferred `Attempt`s
15+
- Reduce the number of variables to handle `Validation` methods composition
16+
- Remove useless calls to `SideEffect::identity()`
17+
318
## 6.5.0 - 2026-11-13
419

520
### Changed

‎src/Attempt.php‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public static function result(mixed $value): self
6363
#[\NoDiscard]
6464
public static function defer(callable $deferred): self
6565
{
66-
return new self(new Defer($deferred));
66+
return new self(new Defer(
67+
static fn() => $deferred()->implementation,
68+
));
6769
}
6870

6971
/**
@@ -109,8 +111,7 @@ public function map(callable $map): self
109111
public function flatMap(callable $map): self
110112
{
111113
return new self($this->implementation->flatMap(
112-
$map,
113-
static fn(self $self) => $self->implementation,
114+
static fn($value) => /** @var T $value */ $map($value)->implementation,
114115
));
115116
}
116117

@@ -125,8 +126,7 @@ public function flatMap(callable $map): self
125126
public function guard(callable $map): self
126127
{
127128
return new self($this->implementation->guard(
128-
$map,
129-
static fn(self $self) => $self->implementation,
129+
static fn($value) => /** @var T $value */ $map($value)->implementation,
130130
));
131131
}
132132

@@ -200,8 +200,7 @@ public function mapErrorCase(string $case, callable $map): self
200200
public function recover(callable $recover): self
201201
{
202202
return new self($this->implementation->recover(
203-
$recover,
204-
static fn(self $self) => $self->implementation,
203+
static fn($value) => $recover($value)->implementation,
205204
));
206205
}
207206

@@ -236,8 +235,7 @@ public function recoverCase(string $case, callable $recover): self
236235
public function xrecover(callable $recover): self
237236
{
238237
return new self($this->implementation->xrecover(
239-
$recover,
240-
static fn(self $self) => $self->implementation,
238+
static fn($value) => $recover($value)->implementation,
241239
));
242240
}
243241

@@ -267,9 +265,7 @@ public function either(): Either
267265
#[\NoDiscard]
268266
public function memoize(): self
269267
{
270-
return new self($this->implementation->memoize(
271-
static fn(self $self) => $self->implementation,
272-
));
268+
return new self($this->implementation->memoize());
273269
}
274270

275271
/**
@@ -284,9 +280,8 @@ public function memoize(): self
284280
public function eitherWay(callable $result, callable $error): self
285281
{
286282
return new self($this->implementation->eitherWay(
287-
$result,
288-
$error,
289-
static fn(self $self) => $self->implementation,
283+
static fn($value) => /** @var T $value */ $result($value)->implementation,
284+
static fn($value) => $error($value)->implementation,
290285
));
291286
}
292287
}

‎src/Attempt/Defer.php‎

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Innmind\Immutable\Attempt;
55

66
use Innmind\Immutable\{
7-
Attempt,
87
Maybe,
98
Either
109
};
@@ -17,17 +16,14 @@
1716
*/
1817
final class Defer implements Implementation
1918
{
20-
/** @var callable(): Attempt<R1> */
21-
private $deferred;
22-
/** @var ?Attempt<R1> */
23-
private ?Attempt $value = null;
19+
/** @var ?Implementation<R1> */
20+
private ?Implementation $value = null;
2421

2522
/**
26-
* @param callable(): Attempt<R1> $deferred
23+
* @param \Closure(): Implementation<R1> $deferred
2724
*/
28-
public function __construct(callable $deferred)
25+
public function __construct(private \Closure $deferred)
2926
{
30-
$this->deferred = $deferred;
3127
}
3228

3329
#[\Override]
@@ -39,20 +35,16 @@ public function map(callable $map): self
3935
}
4036

4137
#[\Override]
42-
public function flatMap(
43-
callable $map,
44-
callable $exfiltrate,
45-
): self {
38+
public function flatMap(callable $map): self
39+
{
4640
$captured = $this->capture();
4741

4842
return new self(static fn() => self::detonate($captured)->flatMap($map));
4943
}
5044

5145
#[\Override]
52-
public function guard(
53-
callable $map,
54-
callable $exfiltrate,
55-
): self {
46+
public function guard(callable $map): self
47+
{
5648
$captured = $this->capture();
5749

5850
return new self(static fn() => self::detonate($captured)->guard($map));
@@ -79,20 +71,16 @@ public function mapError(callable $map): self
7971
}
8072

8173
#[\Override]
82-
public function recover(
83-
callable $recover,
84-
callable $exfiltrate,
85-
): self {
74+
public function recover(callable $recover): self
75+
{
8676
$captured = $this->capture();
8777

8878
return new self(static fn() => self::detonate($captured)->recover($recover));
8979
}
9080

9181
#[\Override]
92-
public function xrecover(
93-
callable $recover,
94-
callable $exfiltrate,
95-
): self {
82+
public function xrecover(callable $recover): self
83+
{
9684
$captured = $this->capture();
9785

9886
return new self(static fn() => self::detonate($captured)->xrecover($recover));
@@ -115,16 +103,15 @@ public function either(): Either
115103
}
116104

117105
#[\Override]
118-
public function memoize(callable $exfiltrate): Implementation
106+
public function memoize(): Implementation
119107
{
120-
return $exfiltrate($this->unwrap());
108+
return $this->unwrap();
121109
}
122110

123111
#[\Override]
124112
public function eitherWay(
125113
callable $result,
126114
callable $error,
127-
callable $exfiltrate,
128115
): self {
129116
$captured = $this->capture();
130117

@@ -134,19 +121,19 @@ public function eitherWay(
134121
}
135122

136123
/**
137-
* @return Attempt<R1>
124+
* @return Implementation<R1>
138125
*/
139-
private function unwrap(): Attempt
126+
private function unwrap(): Implementation
140127
{
141128
/**
142129
* @psalm-suppress InaccessibleProperty
143130
* @psalm-suppress ImpureFunctionCall
144131
*/
145-
return $this->value ??= ($this->deferred)()->memoize();
132+
return $this->value ??= ($this->deferred)();
146133
}
147134

148135
/**
149-
* @return array{\WeakReference<self<R1>>, callable(): Attempt<R1>}
136+
* @return array{\WeakReference<self<R1>>, callable(): Implementation<R1>}
150137
*/
151138
private function capture(): array
152139
{
@@ -160,11 +147,11 @@ private function capture(): array
160147
/**
161148
* @template A
162149
*
163-
* @param array{\WeakReference<self<A>>, callable(): Attempt<A>} $captured
150+
* @param array{\WeakReference<self<A>>, callable(): Implementation<A>} $captured
164151
*
165-
* @return Attempt<A>
152+
* @return Implementation<A>
166153
*/
167-
private static function detonate(array $captured): Attempt
154+
private static function detonate(array $captured): Implementation
168155
{
169156
[$ref, $deferred] = $captured;
170157
$self = $ref->get();

‎src/Attempt/Error.php‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@ public function map(callable $map): self
3636
}
3737

3838
#[\Override]
39-
public function flatMap(
40-
callable $map,
41-
callable $exfiltrate,
42-
): self {
39+
public function flatMap(callable $map): self
40+
{
4341
return $this;
4442
}
4543

4644
#[\Override]
47-
public function guard(
48-
callable $map,
49-
callable $exfiltrate,
50-
): self {
45+
public function guard(callable $map): self
46+
{
5147
return $this;
5248
}
5349

@@ -88,30 +84,26 @@ public function mapError(callable $map): self
8884
}
8985

9086
#[\Override]
91-
public function recover(
92-
callable $recover,
93-
callable $exfiltrate,
94-
): Implementation {
87+
public function recover(callable $recover): Implementation
88+
{
9589
if ($this->value instanceof Guard) {
9690
/** @psalm-suppress ImpureFunctionCall */
97-
return $exfiltrate($recover($this->value->unwrap()));
91+
return $recover($this->value->unwrap());
9892
}
9993

10094
/** @psalm-suppress ImpureFunctionCall */
101-
return $exfiltrate($recover($this->value));
95+
return $recover($this->value);
10296
}
10397

10498
#[\Override]
105-
public function xrecover(
106-
callable $recover,
107-
callable $exfiltrate,
108-
): Implementation {
99+
public function xrecover(callable $recover): Implementation
100+
{
109101
if ($this->value instanceof Guard) {
110102
return $this;
111103
}
112104

113105
/** @psalm-suppress ImpureFunctionCall */
114-
return $exfiltrate($recover($this->value));
106+
return $recover($this->value);
115107
}
116108

117109
#[\Override]
@@ -131,7 +123,7 @@ public function either(): Either
131123
}
132124

133125
#[\Override]
134-
public function memoize(callable $exfiltrate): self
126+
public function memoize(): self
135127
{
136128
return $this;
137129
}
@@ -140,14 +132,13 @@ public function memoize(callable $exfiltrate): self
140132
public function eitherWay(
141133
callable $result,
142134
callable $error,
143-
callable $exfiltrate,
144135
): Implementation {
145136
if ($this->value instanceof Guard) {
146137
/** @psalm-suppress ImpureFunctionCall */
147-
return $exfiltrate($error($this->value->unwrap()));
138+
return $error($this->value->unwrap());
148139
}
149140

150141
/** @psalm-suppress ImpureFunctionCall */
151-
return $exfiltrate($error($this->value));
142+
return $error($this->value);
152143
}
153144
}

0 commit comments

Comments
 (0)