Skip to content

Commit 57405cb

Browse files
committed
Simplify BSON structure creation
1 parent af7be6d commit 57405cb

File tree

2 files changed

+21
-69
lines changed

2 files changed

+21
-69
lines changed

benchmark/BSON/DocumentBench.php

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
<?php
2-
/*
3-
* Copyright 2023-present MongoDB, Inc.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* https://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
172

183
namespace MongoDB\Benchmark\BSON;
194

@@ -23,75 +8,67 @@
238

249
use function file_get_contents;
2510
use function iterator_to_array;
26-
use function MongoDB\BSON\fromJSON;
2711

2812
#[BeforeMethods('prepareData')]
2913
final class DocumentBench extends BaseBench
3014
{
31-
private static string $rawBSON;
3215
private static Document $document;
3316

3417
public function prepareData(): void
3518
{
36-
self::$rawBSON = fromJSON(file_get_contents(self::LARGE_FILE_PATH));
37-
}
38-
39-
/** Create a document from the BSON string */
40-
public static function getBSONDocument(): Document
41-
{
42-
return self::$document ??= Document::fromBSON(self::$rawBSON);
19+
self::$document = Document::fromJSON(file_get_contents(self::LARGE_FILE_PATH));
4320
}
4421

4522
public function benchCheckFirst(): void
4623
{
47-
self::getBSONDocument()->has('qx3MigjubFSm');
24+
self::$document->has('qx3MigjubFSm');
4825
}
4926

5027
public function benchCheckLast(): void
5128
{
52-
self::getBSONDocument()->has('Zz2MOlCxDhLl');
29+
self::$document->has('Zz2MOlCxDhLl');
5330
}
5431

5532
public function benchAccessFirst(): void
5633
{
57-
self::getBSONDocument()->get('qx3MigjubFSm');
34+
self::$document->get('qx3MigjubFSm');
5835
}
5936

6037
public function benchAccessLast(): void
6138
{
62-
self::getBSONDocument()->get('Zz2MOlCxDhLl');
39+
self::$document->get('Zz2MOlCxDhLl');
6340
}
6441

6542
public function benchIteratorToArray(): void
6643
{
67-
iterator_to_array(self::getBSONDocument());
44+
iterator_to_array(self::$document);
6845
}
6946

7047
public function benchToPHPObject(): void
7148
{
72-
self::getBSONDocument()->toPHP();
49+
self::$document->toPHP();
7350
}
7451

7552
public function benchToPHPArray(): void
7653
{
77-
self::getBSONDocument()->toPHP(['root' => 'array']);
54+
self::$document->toPHP(['root' => 'array']);
7855
}
7956

8057
public function benchIteration(): void
8158
{
8259
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
83-
foreach (self::getBSONDocument() as $key => $value);
60+
foreach (self::$document as $key => $value);
8461
}
8562

8663
public function benchIterationAsArray(): void
8764
{
8865
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
89-
foreach (self::getBSONDocument()->toPHP(['root' => 'array']) as $key => $value);
66+
foreach (self::$document->toPHP(['root' => 'array']) as $key => $value);
9067
}
9168

9269
public function benchIterationAsObject(): void
9370
{
9471
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
95-
foreach (self::getBSONDocument()->toPHP() as $key => $value);
72+
foreach (self::$document->toPHP() as $key => $value);
9673
}
9774
}

benchmark/BSON/PackedArrayBench.php

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,75 @@
11
<?php
2-
/*
3-
* Copyright 2023-present MongoDB, Inc.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* https://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
172

183
namespace MongoDB\Benchmark\BSON;
194

205
use MongoDB\Benchmark\BaseBench;
21-
use MongoDB\BSON\Document;
226
use MongoDB\BSON\PackedArray;
237
use PhpBench\Attributes\BeforeMethods;
248

259
use function array_values;
2610
use function file_get_contents;
2711
use function iterator_to_array;
2812
use function json_decode;
29-
use function MongoDB\BSON\fromPHP;
3013

3114
use const JSON_THROW_ON_ERROR;
3215

3316
#[BeforeMethods('prepareData')]
3417
final class PackedArrayBench extends BaseBench
3518
{
36-
private static string $rawBSONDocument;
3719
private static PackedArray $array;
3820

3921
public function prepareData(): void
4022
{
4123
$array = array_values(json_decode(file_get_contents(self::LARGE_FILE_PATH), true, 512, JSON_THROW_ON_ERROR));
4224

43-
// Store BSON string for a document since we can't create packed arrays from BSON strings
44-
self::$rawBSONDocument = fromPHP(['array' => $array]);
45-
}
46-
47-
/** Create a document from the BSON string */
48-
public static function getBSONArray(): PackedArray
49-
{
50-
return self::$array ??= Document::fromBSON(self::$rawBSONDocument)->get('array');
25+
self::$array = PackedArray::fromPHP($array);
5126
}
5227

5328
public function benchCheckFirst(): void
5429
{
55-
self::getBSONArray()->has(0);
30+
self::$array->has(0);
5631
}
5732

5833
public function benchCheckLast(): void
5934
{
60-
self::getBSONArray()->has(94354);
35+
self::$array->has(94354);
6136
}
6237

6338
public function benchAccessFirst(): void
6439
{
65-
self::getBSONArray()->get(0);
40+
self::$array->get(0);
6641
}
6742

6843
public function benchAccessLast(): void
6944
{
70-
self::getBSONArray()->get(94354);
45+
self::$array->get(94354);
7146
}
7247

7348
public function benchIteratorToArray(): void
7449
{
75-
iterator_to_array(self::getBSONArray());
50+
iterator_to_array(self::$array);
7651
}
7752

7853
public function benchToPHPArray(): void
7954
{
80-
self::getBSONArray()->toPHP();
55+
self::$array->toPHP();
8156
}
8257

8358
public function benchIteration(): void
8459
{
8560
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
86-
foreach (self::getBSONArray() as $key => $value);
61+
foreach (self::$array as $key => $value);
8762
}
8863

8964
public function benchIterationAfterIteratorToArray(): void
9065
{
9166
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
92-
foreach (iterator_to_array(self::getBSONArray()) as $key => $value);
67+
foreach (iterator_to_array(self::$array) as $key => $value);
9368
}
9469

9570
public function benchIterationAsArray(): void
9671
{
9772
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
98-
foreach (self::getBSONArray()->toPHP() as $key => $value);
73+
foreach (self::$array->toPHP() as $key => $value);
9974
}
10075
}

0 commit comments

Comments
 (0)