XML用SimpleXML库 - Android电子性能性能、电子、XML、SimpleXML

2023-09-06 07:59:08 作者:Infatuation(痴心)

我使用的是简单的XML库来处理我的Andr​​oid应用程序的XML文件。这些文件可以得到相当大的 - 各地1MB,并且可以深度嵌套pretty的,所以他们是相当复杂的。

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.

当应用程序加载这些文件中的一个,通过简单的API,它可能需要长达30秒才能完成。目前,我路过一个FileInputStream成简单的持留类的[阅读(类,InputStream的)] [2]的方法。有效它只是读取XML节点和数据映射到我的模型对象的情况下,在存储器复制XML树结构

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.

我的问题就是如何提高在Android上的表现?我现在的想法是读取文件的内容到一个字节数组,和一个ByteArrayInputStream传递给持留的read方法代替。我想处理文件将是更快的时间,但我不知道,如果节省的时间会被采取先阅读整个文件的时间来抵消。同时内存的限制可能是一个问题。

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.

一些注意事项:

1)我不能改变我使用的XML库 - 在code的问题是用于跨桌面,移动和网络应用的引擎的一部分。开销改变它会吃不消timebeing。

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)的数据文件是由用户创建的,所以我并没有对嵌套在其中的大小/深度控制。

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

推荐答案

嗯,有很多事情可以做,以改善这一点。在这里,他们是。

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

1)在Android上,你应该使用版本至少为2.5.2,但理想的2.5.3,因为它使用KXML这是更快,更高效的内存在Android上。

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)简单的将动态地建立自己的对象图,这意味着它需要加载尚未被加载的类,并构建基于使用反射的注释每个类的模式。所以第一次使用将永远是迄今为止最昂贵的。重复使用相同的持留实例会快许多倍。因此要尽量避免多次持留情况下,只使用一个,如果可能的。

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)尽量采取措施,直接读取文件的时间,而无需使用简单的XML库。多久时间?如果它永远那么你知道在这里的表现打是由于文件系统。尝试使用的BufferedInputStream来提高性能。

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)如果仍然发现任何问题,提高它在邮件列表上。

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

编辑: Android有与注释处理https://$c$c.google.com/p/android/issues/detail?id=7811,简单的2.6.6补丁已经对这些问题实行变通。 10倍的性能提升可以观察到。

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.