RFR: 8283346: Optimize observable ArrayList creation in FXCollections

Marius Hanl mhanl at openjdk.java.net
Thu Mar 17 21:16:05 UTC 2022


This simple PR optimizes the observable `ArrayList` creation by using the ArrayList constructor/array size so that the underlying array will be initialized at the correct size which will speed up the creation as the array does not need to grow as a result of the `addAll` call.

I also added tests which will succeed before and after to verify that nothing got broken by this change.
Also I made a benchmark test. Results:

Old: `ListBenchmark.init  thrpt   25  722,842 ± 26,930  ops/s`
New: `ListBenchmark.init  thrpt   25  29262,274 ± 2088,712  ops/s`

<details><summary>Benchmark code</summary>


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;

import java.util.ArrayList;
import java.util.List;

@State(Scope.Benchmark)
public class ListBenchmark {

    List<String> strings;

    @Setup
    public void setup() {
        strings = new ArrayList<>();
        for(int i = 0; i< 100000;i++) {
            strings.add("abc: " + i);
        }
    }

    @TearDown
    public void tearDown() {
        strings = null;
    }

    @Benchmark
    public ObservableList<String> init() {
        return FXCollections.observableArrayList(strings);
    }
}


</details>

-------------

Commit messages:
 - 8283346: Optimized observable array list creation

Changes: https://git.openjdk.java.net/jfx/pull/758/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jfx&pr=758&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8283346
  Stats: 47 lines in 2 files changed: 41 ins; 2 del; 4 mod
  Patch: https://git.openjdk.java.net/jfx/pull/758.diff
  Fetch: git fetch https://git.openjdk.java.net/jfx pull/758/head:pull/758

PR: https://git.openjdk.java.net/jfx/pull/758


More information about the openjfx-dev mailing list