XML 与 SimpleXML 库 - Android 上的性能
我正在使用 简单 XML 库 来处理 Android 应用程序中的 XML 文件。这些文件可能会变得相当大 - 大约 1Mb,并且可以嵌套得很深,因此它们相当复杂。
当应用程序通过 Simple API 加载这些文件之一时,最多可能需要 30 秒才能完成。目前,我正在将 FileInputStream 传递到 Simple 的 Persister 类的 [read(Class, InputStream)][2] 方法中。实际上,它只是读取 XML 节点并将数据映射到模型对象的实例,从而在内存中复制 XML 树结构。
那么我的问题是如何提高 Android 上的性能?我当前的想法是将文件的内容读入字节数组,然后将 ByteArrayInputStream 传递给 Persister 的 read 方法。我想处理文件的时间会更快,但我不确定节省的时间是否会被首先读取整个文件所需的时间抵消。内存限制也可能是一个问题。
这是傻子的差事吗?在这种情况下我还能做些什么来提高性能吗?如果没有,我将不得不改进对用户加载文件进度的反馈。
一些警告:
1) 我无法更改我正在使用的 XML 库 - 相关代码是跨桌面、移动和 Web 应用程序使用的“引擎”的一部分。暂时改变它的开销太大了。
2)数据文件是由用户创建的,因此我无法控制其中嵌套的大小/深度。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,您可以采取很多措施来改善这一点。他们在这里。
1) 在 Android 上,您应该至少使用版本 2.5.2,但最好使用 2.5.3,因为它使用 KXML,这在 Android 上速度更快,内存效率更高。
2) Simple 将动态构建对象图,这意味着它将需要加载尚未加载的类,并使用反射根据其注释为每个类构建模式。因此,首次使用始终是迄今为止最昂贵的。重复使用同一个持久化实例将会快很多倍。因此,尽量避免使用多个持久化实例,如果可能,只使用一个。
3) 尝试测量直接读取文件所花费的时间,而不使用简单 XML 库。多久时间?如果需要很长时间,那么您就知道这里的性能影响是由文件系统造成的。尝试使用 BufferedInputStream 来提高性能。
4) 如果您仍然发现任何问题,请在邮件列表中提出。
编辑:
Android 在注释处理方面存在一些问题 https://code.google.com/ p/android/issues/detail?id=7811,Simple 2.6.6 修复已针对这些问题实现了解决方法。可以观察到性能提高了 10 倍。
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.