Efficient representation for large numeric arrays in GWT

怎甘沉沦 提交于 2019-12-22 14:38:14

问题


I have a timeseries class that, over the course of a day will hold 100K-200K values (basically market ticks, uniformly sampled). On the java side the most performant representation is to use double[] (as opposed to say List). I am doubtful that this approach maps well into javasctipt.

On the Java side, the double[] array must grow periodically (ie allocate a new array and copy the old into the new). So for instance, the class has a method like:

public void add (long time, double price)
{
    ...
    if (_len == _prices.length)
        expand ();

    _prices[_len++] = price;
    ...
}

private void expand ()
{
    final double[] newprices = new double [_prices.length + 1024];
    System.arraycopy (_prices, 0, newprices, 0, _len);
    _prices = newprices;
}

Now javascript has a different array model, and allows incremental expansion via indexing beyond the last index, adjusting allocation implicitly.

GWT has a JsArrayNumeric which allows one to see and manipulate JS numerical arrays. JsArrayNative is meant to be a return signature from JS though and cannot be instantiated (yes could whip up some conditional use of JSNI on the JS side, but ugly).

Question is, in GWT, what datastructures can I use in the java class to get the best mapping to the JS representation? I'm looking for the most performant approach for:

  • adding a price to the timeseries (growing the timeseries)
  • accessing the timeseries (will often scan a part of it by index)/li>

Does ArrayList<Double>, for instance, map to something tight in javascript? Might this have a better mapping that using raw double[].


回答1:


JSArrayNumber is going to give you the most efficient mapping. ArrayList will box values for you, which will mean a object wrapper for every value. Even double[] will honor the Java initial value semantics (ensuring that every element is initialized to 0.0). JSArrayNumber will map directly onto JavaScript array holding primitive numbers.

There are some trappings here since JavaScript arrays can return undefined for uninitialized indexes and assigning to an index can actually change the length. You will have to take these into account. I usually create subclass of JsArrayNumber and use assertions to validate use.

Also, you don't have to use JSNI to instantiate a JsArrayNumber:

JSArrayNumber a = JavaSriptObject.createArray().cast();



回答2:


I would be concerned about shipping that much data to the browser and processing it there. If I had hundreds of thousands of data points to process in a GWT app, I'd try to do most of the processing server side and just ship simplified views of that data to the browser to be displayed to the user.




回答3:


I have not looked at the compiled output of the ArrayList class, but I don't think you're going to see any advantage in terms of raw performance of ArrayList vs double[]. That said, I'd suggest going with ArrayList unless you have profiled and know that the add or get methods are actually the bottlenecks of the application. If it's not the bottleneck, then you have less code to write as you can use ArrayList.

Spend some time profiling your application before attempting to optimize the data structures. Firebug works great, but plain old method timing calls work fine as well for getting a rough idea for where an application is spending it's time. Make sure you compile in pretty or detailed if you use firebug so you can make some sense of the output.

You've hinted at it, but I will emphasize not to profile in hosted mode as the JVM has all sorts of optimizations that differ wildly from the Javascript engines in the various browsers. It's also worth pointing out this stackoverflow question as it discusses one of the more common GWT performance issues I've seen in my own applications that do rather heavy data processing on the client. Anecdotal evidence, I know, but we haven't yet hit any problems that had ArrayList as a bottleneck.

Finally, the Google IO conference had a couple of talks that had some interesting performance information.

  • http://www.youtube.com/watch?v=q9hhENmVTWg
  • http://www.youtube.com/watch?v=hNEvr2eLad0

    来源:https://stackoverflow.com/questions/1053256/efficient-representation-for-large-numeric-arrays-in-gwt

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