Skip to content

Commit 8b30cfa

Browse files
authored
Merge pull request #92 from Innmind/optimisations
Reduce the number of intermediate objects on some methods
2 parents 64490ea + 67c075e commit 8b30cfa

5 files changed

Lines changed: 57 additions & 53 deletions

File tree

‎CHANGELOG.md‎

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

3+
## [Unreleased]
4+
5+
### Changed
6+
7+
- 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
9+
310
## 6.4.0 - 2026-11-08
411

512
### Added

‎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
/**

‎src/RegExp.php‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,15 @@ public function capture(Str $string): Map
6969
throw new InvalidRegex(\preg_last_error_msg(), \preg_last_error());
7070
}
7171

72-
/** @var Map<int|string, Str> */
73-
$map = Map::of();
72+
/** @var list<array{int|string, Str}> */
73+
$map = [];
7474

7575
foreach ($matches as $key => $match) {
7676
/** @psalm-suppress RedundantCast Don't trust the types of preg_match */
77-
$map = ($map)(
78-
$key,
79-
Str::of(
80-
(string) $match,
81-
$string->encoding(),
82-
),
83-
);
77+
$map[] = [$key, Str::of((string) $match, $string->encoding())];
8478
}
8579

86-
return $map;
80+
return Map::of(...$map);
8781
}
8882

8983
#[\NoDiscard]

‎src/Str.php‎

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,11 @@ public function split(?string $delimiter = null): Sequence
8787
return $this->chunk();
8888
}
8989

90-
$parts = \explode($delimiter, $this->value);
91-
/** @var Sequence<self> */
92-
$sequence = Sequence::of();
93-
94-
foreach ($parts as $part) {
95-
$sequence = ($sequence)(new self($part, $this->encoding));
96-
}
97-
98-
return $sequence;
90+
/** @psalm-suppress ImpureFunctionCall */
91+
return Sequence::of(...\array_map(
92+
fn($value) => new self($value, $this->encoding),
93+
\explode($delimiter, $this->value),
94+
));
9995
}
10096

10197
/**
@@ -108,15 +104,11 @@ public function split(?string $delimiter = null): Sequence
108104
#[\NoDiscard]
109105
public function chunk(int $size = 1): Sequence
110106
{
111-
/** @var Sequence<self> */
112-
$sequence = Sequence::of();
113-
$parts = \mb_str_split($this->value, $size, $this->encoding->toString());
114-
115-
foreach ($parts as $value) {
116-
$sequence = ($sequence)(new self($value, $this->encoding));
117-
}
118-
119-
return $sequence;
107+
/** @psalm-suppress ImpureFunctionCall */
108+
return Sequence::of(...\array_map(
109+
fn($value) => new self($value, $this->encoding),
110+
\mb_str_split($this->value, $size, $this->encoding->toString()),
111+
));
120112
}
121113

122114
/**
@@ -287,14 +279,14 @@ public function words(string|\Stringable $charlist = ''): Map
287279
{
288280
/** @var list<string> */
289281
$words = \str_word_count($this->value, 2, (string) $charlist);
290-
/** @var Map<int, self> */
291-
$map = Map::of();
282+
/** @var list<array{int, self}> */
283+
$pairs = [];
292284

293285
foreach ($words as $position => $word) {
294-
$map = ($map)($position, new self($word, $this->encoding));
286+
$pairs[] = [$position, new self($word, $this->encoding)];
295287
}
296288

297-
return $map;
289+
return Map::of(...$pairs);
298290
}
299291

300292
/**
@@ -315,14 +307,11 @@ public function pregSplit(string|\Stringable $regex, int $limit = -1): Sequence
315307
throw new InvalidRegex('', \preg_last_error());
316308
}
317309

318-
/** @var Sequence<self> */
319-
$sequence = Sequence::of();
320-
321-
foreach ($strings as $string) {
322-
$sequence = ($sequence)(new self($string, $this->encoding));
323-
}
324-
325-
return $sequence;
310+
/** @psalm-suppress ImpureFunctionCall */
311+
return Sequence::of(...\array_map(
312+
fn($value) => new self($value, $this->encoding),
313+
$strings,
314+
));
326315
}
327316

328317
/**

0 commit comments

Comments
 (0)