Skip to content

Commit a1885ee

Browse files
perf: refresh faker instance to clear memory (#2)
`fake()` is bound as a singleton to the container so refreshing by calling `fake()` again doesn't actually use a new instance. This PR fixes that behavior by making a new instance. Co-authored-by: patrickhoogkamer <patrickhoogkamer@users.noreply.github.com>
1 parent 4404caf commit a1885ee

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/Console/Commands/ObfuscateDatabaseCommand.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,26 @@ private function obfuscateTable(string $tableName, array $primaryColumns, int $c
191191

192192
// Process in smaller batches to reduce memory usage
193193
if (count($updates) >= $batchSize) {
194-
DB::table($tableName)->upsert($updates, $primaryColumns, array_keys($fields));
194+
DB::table($tableName)->upsert($updates, $primaryColumns);
195195
$progress->advance($processedCount);
196196

197197
$updates = [];
198198
$processedCount = 0;
199199
unset($update);
200200
}
201-
202-
unset($record);
203201
}
204202

205203
// Process any remaining updates
206204
if (count($updates) > 0) {
207-
DB::table($tableName)->upsert($updates, $primaryColumns, array_keys($fields));
205+
DB::table($tableName)->upsert($updates, $primaryColumns);
208206
$progress->advance($processedCount);
209207
unset($update);
210208
}
211-
212-
unset($records);
213-
});
209+
}, $primaryColumns[0]);
214210

215211
$progress->finish();
216212

217213
$this->obfuscatorInstances = [];
214+
FakerObfuscator::refreshFakerInstance();
218215
}
219216
}

src/Obfuscators/FakerObfuscator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55
namespace Intermax\Blur\Obfuscators;
66

7+
use Faker;
78
use Faker\Generator;
89
use Intermax\Blur\Contracts\Obfuscator;
910
use InvalidArgumentException;
1011

1112
class FakerObfuscator implements Obfuscator
1213
{
13-
private Generator $faker;
14+
private static ?Generator $faker = null;
1415

1516
public function __construct()
1617
{
17-
// Create a single Faker instance to reuse
18-
$this->faker = fake();
18+
if (self::$faker === null) {
19+
self::refreshFakerInstance();
20+
}
1921
}
2022

2123
/**
@@ -27,6 +29,11 @@ public function generate(?array $parameters = null): mixed
2729
throw new InvalidArgumentException('No faker method provided');
2830
}
2931

30-
return $this->faker->{$parameters[0]}();
32+
return self::$faker->{$parameters[0]}();
33+
}
34+
35+
public static function refreshFakerInstance(): void
36+
{
37+
self::$faker = Faker\Factory::create();
3138
}
3239
}

0 commit comments

Comments
 (0)