11<?php
22namespace PHPJava \Core \JVM \Invoker ;
33
4+ use ByteUnits \Metric ;
45use Monolog \Handler \StreamHandler ;
56use Monolog \Logger ;
67use PHPJava \Core \JavaClass ;
78use PHPJava \Core \JavaClassInvoker ;
9+ use PHPJava \Core \JVM \Cache \OperationCache ;
810use PHPJava \Core \JVM \FlexibleMethod ;
911use PHPJava \Core \JVM \Parameters \GlobalOptions ;
1012use 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 }
0 commit comments