From 5457d9818a169a92e9fd676d489aa82ae0a90b9f Mon Sep 17 00:00:00 2001 From: Simon Roesler Date: Sat, 13 Jan 2018 16:15:55 +0100 Subject: [PATCH 1/4] Reimplementation of Collection::mapToDictionary to be much faster - old one had problems when eager loading multiple thousands of rows --- src/Illuminate/Support/Collection.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index d95271d35e15..18832ad61d1f 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -880,13 +880,21 @@ public function mapSpread(callable $callback) * @param callable $callback * @return static */ + public function mapToDictionary(callable $callback) { - $dictionary = $this->map($callback)->reduce(function ($groups, $pair) { - $groups[key($pair)][] = reset($pair); + $dictionary = []; - return $groups; - }, []); + $this->each(function($item) use(&$dictionary, $callback) { + $pair = $callback($item); + $key = key($pair); + $value = reset($pair); + + if (!isset($dictionary[$key])) { + $dictionary[$key] = []; + } + $dictionary[$key][] = $value; + }); return new static($dictionary); } From 149934e13f6c10055609ce8bddf2bf26aa48ab0c Mon Sep 17 00:00:00 2001 From: Simon Roesler Date: Sat, 13 Jan 2018 16:40:14 +0100 Subject: [PATCH 2/4] Added key to mapToDictionary callback --- src/Illuminate/Support/Collection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 18832ad61d1f..c07aa351b7b7 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -885,8 +885,8 @@ public function mapToDictionary(callable $callback) { $dictionary = []; - $this->each(function($item) use(&$dictionary, $callback) { - $pair = $callback($item); + $this->each(function($item, $key) use(&$dictionary, $callback) { + $pair = $callback($item, $key); $key = key($pair); $value = reset($pair); From 5857ecf82679f0750b9bd6e974142113fe537447 Mon Sep 17 00:00:00 2001 From: Simon Roesler Date: Sat, 13 Jan 2018 16:41:20 +0100 Subject: [PATCH 3/4] Fixed style --- src/Illuminate/Support/Collection.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index c07aa351b7b7..0ff887004c78 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -880,17 +880,16 @@ public function mapSpread(callable $callback) * @param callable $callback * @return static */ - public function mapToDictionary(callable $callback) { $dictionary = []; - $this->each(function($item, $key) use(&$dictionary, $callback) { + $this->each(function ($item, $key) use(&$dictionary, $callback) { $pair = $callback($item, $key); $key = key($pair); $value = reset($pair); - if (!isset($dictionary[$key])) { + if (! isset($dictionary[$key])) { $dictionary[$key] = []; } $dictionary[$key][] = $value; From d8b2eb1dae66a45ca20034b04bc193ff6ef43b85 Mon Sep 17 00:00:00 2001 From: Simon Roesler Date: Sat, 13 Jan 2018 16:41:56 +0100 Subject: [PATCH 4/4] Fixed style --- src/Illuminate/Support/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 0ff887004c78..ce60a66f26a9 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -884,7 +884,7 @@ public function mapToDictionary(callable $callback) { $dictionary = []; - $this->each(function ($item, $key) use(&$dictionary, $callback) { + $this->each(function ($item, $key) use (&$dictionary, $callback) { $pair = $callback($item, $key); $key = key($pair); $value = reset($pair);