13

I'm using the Simple XML library to process XML files in my Android application. These file can get quite big - around 1Mb, and can be nested pretty deeply, so they are fairly complex.

When the app loads one of these files, via the Simple API, it can take up to 30sec to complete. Currently I am passing a FileInputStream into the [read(Class, InputStream)][2] method of Simple's Persister class. Effectively it just reads the XML nodes and maps the data to instances of my model objects, replicating the XML tree structure in memory.

My question then is how do I improve the performance on Android? My current thought would be to read the contents of the file into a byte array, and pass a ByteArrayInputStream to the Persister's read method instead. I imagine the time to process the file would be quicker, but I'm not sure if the time saved would be counteracted by the time taken to read the whole file first. Also memory constraints might be an issue.

Is this a fools errand? Is there anything else I could do to increase the performance in this situation? If not I will just have to resort to improving the feedback to the user on the progress of loading the file.

Some caveats:

1) I can't change the XML library I'm using - the code in question is part of an "engine" which is used across desktop, mobile and web applications. The overhead to change it would be too much for the timebeing.

2) The data files are created by users so I don't have control over the size/depth of nesting in them.

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
Jonskichov
  • 299
  • 4
  • 15
  • 1
    Wow, that sounds like a long time. I use the Simple XML Library and I have not experienced such a large slowdown. Point two that you made is odd but I must say that 1MB is a large XML file indeed. The entire export of my whole Wordpress blog was less than that so I am not surprised that it takes so long. If I were you I would make it a bug in the simple sourceforge page: http://sourceforge.net/tracker/?group_id=112203&atid=661526 – Robert Massaioli May 21 '11 at 22:37

1 Answers1

17

Well, there are many things you can do to improve this. Here they are.

1) On Android you should be using at least version 2.5.2, but ideally 2.5.3 as it uses KXML which is much faster and more memory efficient on Android.

2) Simple will dynamically build your object graph, this means that it will need to load classes that have not already been loaded, and build a schema for each class based on its annotations using reflection. So first use will always be by far the most expensive. Repeated use of the same persister instance will be many times faster. So try to avoid multiple persister instances, just use one if possible.

3) Try measure the time taken to read the file directly, without using the Simple XML library. How long does it take? If it takes forever then you know the performance hit here is due to the file system. Try use a BufferedInputStream to improve performance.

4) If you still notice any issues, raise it on the mailing list.

EDIT: Android has some issues with annotation processing https://code.google.com/p/android/issues/detail?id=7811, Simple 2.6.6 fixes has implemented work arounds for these issues. Performance increases of 10 times can be observed.

ng.
  • 7,099
  • 1
  • 38
  • 42
  • 2
    Your comment "Repeated use of the same persister instance will be many times faster. So try to avoid multiple persister instances, just use one if possible." saved us a lot of time in our API calling. Before I optimized this, it used to take 15seconds now it takes 3-5seconds. Thanks. – vipsy Aug 07 '12 at 17:29
  • I am parsing a large number of XML streams with version 2.7.1, each around 1k in size. Reusing the persister instead of recreating it decreased the parsing time for ~350 streams on a low-end smartphone from 3 minutes to 14 seconds. – user149408 Jul 05 '20 at 21:35