Skip to content

Commit 08e4b57

Browse files
committed
use singleton pattern for some amount of memory saving
1 parent bb7dffe commit 08e4b57

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/Codegen/Constraints/UntypedBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function generateOneOfChecks(vec<TSchema> $schemas, HackBuilder $hb): vo
170170
->addAssignment('$passed_any', false, HackBuilderValues::export())
171171
->addAssignment('$passed_multi', false, HackBuilderValues::export())
172172
// Use `sentinal` rather than `null` so that it's obvious when we failed to match any constraint.
173-
->addAssignment('$output', Str\format('new \%s()', Sentinal::class), HackBuilderValues::literal())
173+
->addAssignment('$output', Str\format('\%s::get()', Sentinal::class), HackBuilderValues::literal())
174174
->startForeachLoop('$constraints', null, '$constraint')
175175
->startTryBlock()
176176
->addMultilineCall('$output = $constraint', vec['$input', '$pointer'])

src/Sentinal.hack

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@ namespace Slack\Hack\JsonSchema;
33
/**
44
* Represents an unset value.
55
*/
6-
final class Sentinal {}
6+
final class Sentinal {
7+
8+
/**
9+
* The singleton instance of the sentinal value.
10+
*/
11+
public static function get(): this {
12+
return new self();
13+
}
14+
15+
private function __construct () {}
16+
}

tests/examples/codegen/AddressSchemaValidator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* To re-generate this file run `make test`
66
*
77
*
8-
* @generated SignedSource<<884da88463b70fac29468fab117c4e1b>>
8+
* @generated SignedSource<<7c546e3edf3430be1044abfffdf398cd>>
99
*/
1010
namespace Slack\Hack\JsonSchema\Tests\Generated;
1111
use namespace Slack\Hack\JsonSchema;
@@ -425,7 +425,7 @@ public static function check(
425425

426426
$passed_any = false;
427427
$passed_multi = false;
428-
$output = new \Slack\Hack\JsonSchema\Sentinal();
428+
$output = \Slack\Hack\JsonSchema\Sentinal::get();
429429
foreach ($constraints as $constraint) {
430430
try {
431431
$output = $constraint($input, $pointer);

tests/examples/codegen/UntypedSchemaValidator.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* To re-generate this file run `make test`
66
*
77
*
8-
* @generated SignedSource<<90a3ca75ea51b3a416f721e6eb1828de>>
8+
* @generated SignedSource<<8553bd28dc52954a5c87a8c2cb2f3b01>>
99
*/
1010
namespace Slack\Hack\JsonSchema\Tests\Generated;
1111
use namespace Slack\Hack\JsonSchema;
@@ -1036,7 +1036,7 @@ public static function check(
10361036

10371037
$passed_any = false;
10381038
$passed_multi = false;
1039-
$output = new \Slack\Hack\JsonSchema\Sentinal();
1039+
$output = \Slack\Hack\JsonSchema\Sentinal::get();
10401040
foreach ($constraints as $constraint) {
10411041
try {
10421042
$output = $constraint($input, $pointer);
@@ -1099,7 +1099,7 @@ public static function check(
10991099

11001100
$passed_any = false;
11011101
$passed_multi = false;
1102-
$output = new \Slack\Hack\JsonSchema\Sentinal();
1102+
$output = \Slack\Hack\JsonSchema\Sentinal::get();
11031103
foreach ($constraints as $constraint) {
11041104
try {
11051105
$output = $constraint($input, $pointer);
@@ -1191,7 +1191,7 @@ public static function check(
11911191

11921192
$passed_any = false;
11931193
$passed_multi = false;
1194-
$output = new \Slack\Hack\JsonSchema\Sentinal();
1194+
$output = \Slack\Hack\JsonSchema\Sentinal::get();
11951195
foreach ($constraints as $constraint) {
11961196
try {
11971197
$output = $constraint($input, $pointer);

0 commit comments

Comments
 (0)