val235 opened a new issue, #15100:
URL: https://github.com/apache/grails-core/issues/15100

   ### Expected Behavior
   
   I 've been writing some code that does tight loop based iteration as well as 
random access in lists and array. Think a machine learning data pipeline. My 
test scripts have been as fast as expected, but i noticed that if the same 
iteration loops were triggered through a grails controller, they suffered from 
severe performance degradation.  I would have expected simple looping and 
array, no matter how its triggered, be blazingly fast given that the JVM 
optimizes these down at a very low level.
   
   
   
   ### Actual Behaviour
   
   I've simplified the looping to a very basic example (attached), its just a 
tight outer loop that iterates 500000 reps, and an inner loop that iterates 
over a short array of strings, and extracts the element from the array, using 
basic index based accessor (array[i]) What I found is that exact same groovy 
code triggered from the controller is magnitudes of order slower than then if 
the exact same code is triggered from a groovy script. As a second test point, 
I converted the same loop into a pure java class, and that one runs as fast as 
expected in all situations.
   
    For the slow down we are talking 
    - 6 msec for pure java (doesnt matter how its triggered)
    - 10-18 msec for groovy triggered via a script
    -400-600 mse for groovy triggered via Grails Controller
   
   I've tried various combination of @CompileStatic and @GrailsCompileStatic 
none make a difference. After profiling I noticed that when running from a 
controller the hotspot is around various java reflective array accessors. So 
the Grails compilation or tooling is leaving some invoke-dynamics around arrays 
that end up using reflection which is much slower and would account for this 
slow down.
   
   this happens for very basic array operations, instead of using the highly 
optimized low level operators, they default to reflection based classes
   
   array[i] 
   array.length
   
   <img width="1456" height="150" alt="Image" 
src="https://github.com/user-attachments/assets/94575ff2-ead7-4e86-bb7d-05f768898f20";
 />
   
   <img width="1684" height="94" alt="Image" 
src="https://github.com/user-attachments/assets/39513f0e-079b-4944-badf-b1a14671e26a";
 />
   
   <img width="1587" height="216" alt="Image" 
src="https://github.com/user-attachments/assets/f80daa81-4ebb-4fec-887b-8807106acf4f";
 />
   
   ### Steps To Reproduce
   
   Im attaching a very basic grails app, generated via Grails App Forge. 
   
   I added 4 files, 
   - slow.loop.MainController  -controller that triggers  slow groovy looper 
and fast java looper and reports the timing
   - slow.loop.TinyLoopGroovy.groovy - the groovy based loop that suffers from 
slowdown when run from controller
   - slow.loop.TinyLoopJava.java - the java based version for the groovy looper 
above, doesnt suffer from any slowdown
   -scritps/SlowLoopTest.groovy - the groovy script that calls the same java 
and groovy looper, prinlns the timing, doesnt show any slowdowns
   
   To view the controller based slow down
   1) start the grails app  `./gradlew bootRun`  (assume it ll start on port 
8080
   2) hit localhost:8080/test  to trigger the controller and view the timing 
difference between java and groovy (there are 10 loops of each) finishes 
quickly in about a minute but the difference in the timings is stark
   
   <img width="1040" height="1169" alt="Image" 
src="https://github.com/user-attachments/assets/14ef2222-f277-47de-bcb7-83219f3832c5";
 />
   
   To run the same loopers as a scirpt I added a gradle task (runTestScript) 
   `./gradlew runTestScript`
   
   <img width="828" height="788" alt="Image" 
src="https://github.com/user-attachments/assets/8275ae39-67ac-4cb1-95e9-183ae43808a7";
 />
   
   
   ### Environment Information
   
   Windows 11
   Java 21 
   Grails 7.0.0.RC2
   
   
[slowloop.zip](https://github.com/user-attachments/files/22550430/slowloop.zip)
   
   Also happened with the previous Grails 6x
   
   ### Example Application
   
   _No response_
   
   ### Version
   
   7.0.0.RC2 (also 6x)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to