Skip to content

Commit 82cea88

Browse files
authored
Merge pull request #102 from php-java/saving-memories
Saving memories in operation processing
2 parents 339f68e + d418be5 commit 82cea88

File tree

12 files changed

+104
-17
lines changed

12 files changed

+104
-17
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "JVM emulator by PHP",
44
"type": "library",
55
"license": "MIT",
6-
"version": "0.0.6.4-dev",
6+
"version": "0.0.6.5-dev",
77
"authors": [
88
{
99
"name": "memory"
@@ -17,7 +17,8 @@
1717
"php-java/java-util-package": "*",
1818
"php-java/java-io-package": "*",
1919
"php-java/java-net-package": "*",
20-
"php-java/java-nio-package": "*"
20+
"php-java/java-nio-package": "*",
21+
"gabrielelana/byte-units": "dev-master"
2122
},
2223
"autoload": {
2324
"psr-4": {

src/Core/JVM/Cache/Cache.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace PHPJava\Core\JVM\Cache;
3+
4+
class Cache
5+
{
6+
private $items = [];
7+
8+
public function fetchOrPush(string $key, callable $pushFunction, ...$parameters)
9+
{
10+
if (isset($this->items[$key])) {
11+
return $this->items[$key];
12+
}
13+
return $this->items[$key] = $pushFunction(...$parameters);
14+
}
15+
}

src/Core/JVM/Cache/HeapCache.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Core\JVM\Cache;
3+
4+
class HeapCache extends Cache
5+
{
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace PHPJava\Core\JVM\Cache;
3+
4+
class OperationCache extends Cache
5+
{
6+
7+
}

src/Core/JVM/Invoker/Invokable.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22
namespace PHPJava\Core\JVM\Invoker;
33

4+
use ByteUnits\Metric;
45
use Monolog\Handler\StreamHandler;
56
use Monolog\Logger;
67
use PHPJava\Core\JavaClass;
78
use PHPJava\Core\JavaClassInvoker;
9+
use PHPJava\Core\JVM\Cache\OperationCache;
810
use PHPJava\Core\JVM\FlexibleMethod;
911
use PHPJava\Core\JVM\Parameters\GlobalOptions;
1012
use PHPJava\Core\JVM\Stream\BinaryReader;
@@ -218,6 +220,17 @@ function ($item) {
218220
$localStorage
219221
);
220222

223+
$this->debugTool->getLogger()->debug(
224+
vsprintf(
225+
'Used Memory: %s, Used Memory Peak: %s',
226+
[
227+
Metric::bytes(memory_get_usage())->format(),
228+
Metric::bytes(memory_get_peak_usage())->format(),
229+
]
230+
)
231+
);
232+
233+
$operationCache = new OperationCache();
221234
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
222235
if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) {
223236
throw new RuntimeException(
@@ -248,6 +261,16 @@ function ($item) {
248261
$debugTraces['mnemonic_indexes'][] = $pointer;
249262
}
250263

264+
$this->debugTool->getLogger()->debug(
265+
vsprintf(
266+
'Used Memory: %s, Used Memory Peak: %s',
267+
[
268+
Metric::bytes(memory_get_usage())->format(),
269+
Metric::bytes(memory_get_peak_usage())->format(),
270+
]
271+
)
272+
);
273+
251274
$this->debugTool->getLogger()->debug(
252275
vsprintf(
253276
'OpCode: 0x%02X, Mnemonic: %s, Stacks: %d, PC: %d',
@@ -274,17 +297,23 @@ function ($item) {
274297
/**
275298
* @var OperationInterface|Accumulator|ConstantPool $executor
276299
*/
277-
$executor = new $fullName();
278-
$executor->setConstantPool($currentConstantPool);
279-
$executor->setParameters(
280-
$method->getAttributes(),
281-
$this->javaClassInvoker,
282-
$reader,
283-
$localStorage,
284-
$stacks,
285-
$pointer
300+
$executor = $operationCache->fetchOrPush(
301+
$fullName,
302+
function () use ($fullName) {
303+
return new $fullName();
304+
}
286305
);
287-
$returnValue = $executor->execute();
306+
$returnValue = $executor
307+
->setConstantPool($currentConstantPool)
308+
->setParameters(
309+
$method->getAttributes(),
310+
$this->javaClassInvoker,
311+
$reader,
312+
$localStorage,
313+
$stacks,
314+
$pointer
315+
)
316+
->execute();
288317

289318
$afterTrigger = $this->options['operations']['injections']['after'] ?? GlobalOptions::get('operations.injections.after');
290319
if (is_callable($afterTrigger)) {
@@ -298,13 +327,17 @@ function ($item) {
298327
}
299328

300329
if ($returnValue !== null) {
301-
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
330+
if ($isEnabledTrace) {
331+
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
332+
}
302333
$this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified);
303334
return $returnValue;
304335
}
305336
}
306337

307-
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
338+
if ($isEnabledTrace) {
339+
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
340+
}
308341
$this->debugTool->getLogger()->info('Finish operations: ' . $methodBeautified);
309342
return null;
310343
}

src/Core/JVM/Parameters/GlobalOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function get($name)
2525
return $ref;
2626
}
2727

28-
public function reset()
28+
public static function reset()
2929
{
3030
static::$settings = [];
3131
}

src/Core/JVM/Parameters/Runtime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class Runtime
1313
const PRELOAD = false;
1414
const DRY_RUN_ATTRIBUTE = false;
1515

16-
const OPERATIONS_ENABLE_TRACE = true;
16+
const OPERATIONS_ENABLE_TRACE = false;
1717
const OPERATIONS_TEMPORARY_CODE_STREAM = 'php://memory';
1818

1919
const VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY = false;

src/Core/JavaClass.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
use PHPJava\Core\JVM\ActiveInterface;
77
use PHPJava\Core\JVM\ActiveMethods;
88
use PHPJava\Core\JVM\ConstantPool;
9+
use PHPJava\Core\JVM\Parameters\GlobalOptions;
10+
use PHPJava\Core\JVM\Parameters\Runtime;
911
use PHPJava\Core\JVM\Validations\MagicByte;
1012
use PHPJava\Core\Stream\Reader\ReaderInterface;
13+
use PHPJava\Exceptions\DebugTraceIsDisabledException;
1114
use PHPJava\Exceptions\ValidatorException;
1215
use PHPJava\Kernel\Attributes\AttributeInterface;
1316
use PHPJava\Kernel\Attributes\InnerClassesAttribute;
@@ -302,6 +305,12 @@ public function getSuperClass()
302305

303306
public function debug(): void
304307
{
308+
$isEnabledTrace = $this->options['operations']['enable_trace'] ?? GlobalOptions::get('operations.enable_trace') ?? Runtime::OPERATIONS_ENABLE_TRACE;
309+
if (!$isEnabledTrace) {
310+
throw new DebugTraceIsDisabledException(
311+
"Debug trace is disabled. If you want to show debug trace then enable to `enable_trace` option."
312+
);
313+
}
305314
$cpInfo = $this->getConstantPool();
306315
foreach ($this->debugTraces as $debugTraces) {
307316
printf("[method]\n");

src/Core/PHPJava.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ final class PHPJava
1212
/**
1313
* As same as composer version.
1414
*/
15-
const VERSION = 0x000064;
15+
const VERSION = 0x000065;
1616
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class DebugTraceIsDisabledException extends \Exception
5+
{
6+
}

0 commit comments

Comments
 (0)