private @NotNull StubList createOptimizedCopy() {
    MaterialStubList copy = new MaterialStubList(size());
    new Object() {
      void visitStub(StubBase<?> stub, int parentId) {
        int idInCopy = copy.size();
        copy.addStub(idInCopy, parentId, getStubTypeIndex(stub.id));
        copy.myPlainList.add(stub);

        List<StubBase<?>> children = getChildrenStubs(stub.id);
        copy.prepareForChildren(idInCopy, children.size());

        for (StubBase<?> child : children) {
          visitStub(child, idInCopy);
        }
      }
    }.visitStub(get(0), -1);

    assert copy.isChildrenLayoutOptimal();

    for (int i = 0; i < copy.size(); i++) {
      StubBase<?> stub = copy.get(i);
      stub.myStubList = copy;
      stub.id = i;
    }

    return copy.finalizeLoadingStage();
  }
private @NotNull StubList createOptimizedCopy()