Skip to content

Commit cb489c7

Browse files
committed
simplify map implementation transitions
1 parent 9ef70b1 commit cb489c7

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66

77
- Reduced the number of intermediate objects created on some `Str` methods
8+
- Reduced the number of intermediate objects created when switching from differents `Map` implementations
89

910
## 6.4.0 - 2026-11-08
1011

‎src/Map/ObjectKeys.php‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(?\SplObjectStorage $values = null)
3737
public function __invoke($key, $value): Implementation
3838
{
3939
if (!\is_object($key)) {
40-
return (new DoubleIndex)->merge($this)($key, $value);
40+
return new DoubleIndex($this->toSequence()(new Pair($key, $value)));
4141
}
4242

4343
/** @var \SplObjectStorage<object, mixed> */
@@ -446,11 +446,22 @@ public function find(callable $predicate): Maybe
446446
#[\Override]
447447
public function toSequence(): Sequence
448448
{
449-
/** @var Sequence<Pair<T, S>> */
450-
return $this->reduce(
451-
Sequence::of(),
452-
static fn(Sequence $pairs, $key, $value) => ($pairs)(new Pair($key, $value)),
453-
);
449+
/** @var list<Pair<T, S>> */
450+
$pairs = [];
451+
452+
/** @psalm-suppress ImpureMethodCall */
453+
foreach ($this->values as $k) {
454+
/** @var T $key */
455+
$key = $k;
456+
/**
457+
* @psalm-suppress ImpureMethodCall
458+
* @var S $v
459+
*/
460+
$v = $this->values[$k];
461+
$pairs[] = new Pair($key, $v);
462+
}
463+
464+
return Sequence::of(...$pairs);
454465
}
455466

456467
/**

‎src/Map/Primitive.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public function __invoke($key, $value): Implementation
4242
// numeric-string keys are casted to ints by php, so when iterating
4343
// over the array afterward the type is not conserved so we switch
4444
// the implementation to DoubleIndex so keep the type
45-
return (new DoubleIndex)->merge($this)($key, $value);
45+
return new DoubleIndex($this->toSequence()(new Pair($key, $value)));
4646
}
4747

4848
if (!\is_string($key) && !\is_int($key)) {
49-
return (new DoubleIndex)->merge($this)($key, $value);
49+
return new DoubleIndex($this->toSequence()(new Pair($key, $value)));
5050
}
5151

5252
$values = $this->values;
@@ -367,11 +367,14 @@ public function find(callable $predicate): Maybe
367367
#[\Override]
368368
public function toSequence(): Sequence
369369
{
370-
/** @var Sequence<Pair<T, S>> */
371-
return $this->reduce(
372-
Sequence::of(),
373-
static fn(Sequence $pairs, $key, $value) => ($pairs)(new Pair($key, $value)),
374-
);
370+
/** @var list<Pair<T, S>> */
371+
$pairs = [];
372+
373+
foreach ($this->values as $k => $v) {
374+
$pairs[] = new Pair($k, $v);
375+
}
376+
377+
return Sequence::of(...$pairs);
375378
}
376379

377380
/**

0 commit comments

Comments
 (0)