Optimize loading speed of xstream

耗尽温柔 提交于 2020-01-02 06:16:07

问题


I felt xstream loading speed doesn't up to my requirement when I try to perform loading from the XML file. For an "database" with 10k ++ entries, it will take several minutes.

The following is the entire data structure I use to serialize. Size of List (symbols and codes) will be roughly 10k ++ entries.

http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/engine/StockCodeAndSymbolDatabase.java?revision=1.11&view=markup

Is there any approach I can try out, to see whether it will speed up my loading time? Able to still load back previous saved file is important as well.

The following are the code used to de-serialization. Thanks.

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    InputStream inputStream = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        Object object = xStream.fromXML(inputStream);
        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(inputStream)) {
        return null;
        }
        inputStream = null;
    }

    return null;
} 

回答1:


Avoid using slow DomDriver.

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    // Don't ever try to use DomDriver. They are VERY slow.
    XStream xStream = new XStream();
    InputStream inputStream = null;
    Reader reader = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        Object object = xStream.fromXML(reader);

        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(reader)) {
            return null;
        }
        if (false == close(inputStream)) {
            return null;
        }
        reader = null;
        inputStream = null;
    }

    return null;
}


来源:https://stackoverflow.com/questions/3623546/optimize-loading-speed-of-xstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!