1

I have a method, which connects to a HTTP server and requests via XMLRPC, a list of data structures and then for each data structure gets a list of attributes and the values of those attributes. It's implemented using nested for each loops.

The problem is that it's loading a lot of data all at once, and consuming a massive amount of CPU (over 100%) reading responses from the server and parsing the XML.

If I were writing the program in C, I'd insert a usleep() at the end of the loop, to wait before trying to load more data and reduce CPU usage. What would the equivalent be in Flex?

j b
  • 5,147
  • 5
  • 41
  • 60
  • 1
    Instead of trying to stagger processing, why not look to optimize it. For each loops are the slowest for loops, as one example. You could also switch to an AMF format with automatic object conversion and I suspect that may help address the problems all together b/c the binary AMF format leads to much quicker processing than XML. – JeffryHouser Aug 17 '11 at 20:35
  • AMF will definitely give you the biggest profit. I cranked the Census app up to 100.000 rows and parsing time was 0.15s. I'll add though that serialization speeds on the server side can vary with the chosen technologies. I guess it was most optimized for Java (BlazeDS). – RIAstar Aug 17 '11 at 22:05
  • Take a look at this post, a similar question was asked and answered. http://stackoverflow.com/questions/560474/flex-equivalent-of-processmessages-and-unresponsive-ui-during-long-loops – Jacob Eggers Aug 18 '11 at 00:06
  • @Jacob, thanks I didn't spot that question in my search. I don't mind the UI being unresponsive, since this is a 'project load' operation from a local XMLRPC service. It sounds like switching to AMF and pulling the data on a Timer is the way to go. – j b Aug 18 '11 at 07:30

2 Answers2

3

One of the biggest drawbacks of flash/flex is that the generated application is single threaded, so running CPU intensive tasks like parsing large responses will make the application freeze.

Some of the solutions I use to work around those problems are:

  • If possible do not load everything from the server at once but load it through multiple calls (i.e read your data using 10 pages of 50 results instead of 500 at once).

  • Make sure that the data you are loading is not directly binded on some UI elements (changes in the data will trigger change on the UI that will consume more CPU)

  • Try to simulate a thread-like model by using a Timer object, and work on a subset of your data at each timer tick.

Also returning XML results is not the most efficient way (using RemoteObjects through BlazeDs is more efficient as it uses binary streams instead of strings).

Voo
  • 29,040
  • 11
  • 82
  • 156
Davz
  • 1,530
  • 11
  • 24
  • +! from me. As an extension to this answer, you can compare load times of various data sets with James Wards Census Application: http://www.jamesward.com/census2/ . – JeffryHouser Aug 17 '11 at 21:04
  • 1
    +1 from me. These are all things I do when working with remote data. I'll add that using AMF instead of XML can be quite beneficial as well. – NoobsArePeople2 Aug 17 '11 at 21:33
0

In some scenarios, it's better to stick with what you have access to if you're not in control of your server-side data source. Another alternative would be to write getter/setter wrapper to your XML object. XML is first class citizen in AS3. Easy for programs to read (e4x) but annoying for us devs to write and modify. If you can, get a page of data that once received will kick off another request while the user can work with the data from pages that have already been loaded. Create the illusion of parallelism using serial techniques.

Mark Lapasa
  • 1,644
  • 4
  • 19
  • 37