From 8ac60a9705e6b62d8f883fa3c594ca7ce9a572c2 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 2 Apr 2024 20:21:19 -0400 Subject: [PATCH] Optimized conversion to bitmask New method is about 60% faster than before --- src/Map.php | 15 ++++++++++----- test/Tests/MapTest.php | 4 ++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Map.php b/src/Map.php index c959b98..ca85dcb 100644 --- a/src/Map.php +++ b/src/Map.php @@ -221,12 +221,17 @@ protected function reduceToBitmask($list) { $this->logger->debug('Swivel - reducing to bitmask.', compact('list')); - return !is_array($list) ? $list : array_reduce($list, function ($mask, $index) { - if ((int)$index == 0) { - return $mask; + if (!is_array($list)) { + return $list; + } + + $mask = 0; + foreach ($list as $value) { + if ($value > 0) { + $mask |= (1 << ((int)$value - 1)); } - return $mask | (1 << ($index - 1)); - }, 0); + } + return $mask; } /** diff --git a/test/Tests/MapTest.php b/test/Tests/MapTest.php index 50529f8..d483f12 100644 --- a/test/Tests/MapTest.php +++ b/test/Tests/MapTest.php @@ -134,6 +134,10 @@ public function parseProvider() ['a' => [6, 7], 'a.b' => [7]], ['a' => Bucket::SIXTH | Bucket::SEVENTH, 'a.b' => Bucket::SEVENTH], ], + [ + ['a' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], + ['a' => Bucket::ALL], + ], [ ['a' => Bucket::FIRST], ['a' => Bucket::FIRST],