Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Fixing Multiple Iterations Over A Single Collection  RSS feed

 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP that iterates a total of 5 times over a single collection to create a report. Each iteration (using <c:forEach>) creates a section of the report.

This seems to be very wasteful to me, especially if I could somehow do this all in one sweep. However, the only idea that comes to mind is to break the report sections into HTML <div> areas and somehow populate each section during one loop. I really don't know that I can do that, especially without Javascript.

Does anyone have any ideas?

(Oh, the joys of software maintenance...)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Ferguson:
This seems to be very wasteful to me
But is it? Does it create discernible performance issues? Does it introduce unnecessary complexity or fragility to the page code that another solution might alleviate? Are you perhaps trying to solve a problem that may not exist?
 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
But is it? Does it create discernible performance issues? Does it introduce unnecessary complexity or fragility to the page code that another solution might alleviate? Are you perhaps trying to solve a problem that may not exist?


During peak hours, the report can take up to an hour to run. This tends to happen on Mondays, as everyone tries to complete their weekly reports. It's even worse on the first of the month.

The app is a resource hog and has OOM'd out of its 512MB allocated heap space in the past (I can't give it more).

I'm wondering if the collection isn't getting garbage collected (or collected fast enough) at the end of the <c:forEach>.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you are fetching the data as part of the loops, rather than getting the data prior to forwarding to the JSP, then the loops are unlikely to have any impact whatsoever on performance. If you are fetching the data five times, once for each loop, I'd suggest that a serious refactoring is in order.
 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Thanks for the response. This tree structure is composed of persisted Hibernate that have multiple many-to-one relationships. THAT part of this discussion isn't appropriate for this forum, though.

One of the ideas we had was to use Velocity or Freemarker (or another templating engine) to lay out the page as a template, get the collection once, and fill in the blanks there.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Bear's point is that, unless you're doing an expensive lookup with each iteration, it really shouldn't matter if you're looping through the same collection multiple times.

If this were my issue and I did have to perform expensive lookups with each iteration, I would factor some of the functionality back to the model tier and create multiple, smaller, beans to pass to the JSP.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!