Skip to content

Replace the list used internally by the ListItemReader (ArrayList -> LinkedList) #3782

Closed
@jojoldu

Description

@jojoldu

Expected Behavior

I expect the arrayList to be changed to LinkedList to improve the performance of the read() method.

public class ListItemReader<T> implements ItemReader<T> 
	private List<T> list;

	public ListItemReader(List<T> list) {
		// If it is a proxy we assume it knows how to deal with its own state.
		// (It's probably transaction aware.)
		if (AopUtils.isAopProxy(list)) {
			this.list = list;
		}
		else {
			this.list = new LinkedList<>(list); // ArrayList -> LinkedList
		}
	}

        @Nullable
	@Override
	public T read() {
		if (!list.isEmpty()) {
			return list.remove(0);
		}
		return null;
	}
}

Current Behavior

The list used inside the ListItemReader is ArrayList.

public class ListItemReader<T> implements ItemReader<T> 
	private List<T> list;

	public ListItemReader(List<T> list) {
		// If it is a proxy we assume it knows how to deal with its own state.
		// (It's probably transaction aware.)
		if (AopUtils.isAopProxy(list)) {
			this.list = list;
		}
		else {
			this.list = new ArrayList<>(list);
		}
	}

        @Nullable
	@Override
	public T read() {
		if (!list.isEmpty()) {
			return list.remove(0);
		}
		return null;
	}
}

So when a lot of data is inserted, the read() method has a very poor performance.

Context

ArrayList should be used where more search operations are required, and LinkedList should be used where more insert and delete operation is required.

ListItemReader thinks LinkedList is more appropriate for the implementation because the read() method returns data through list.remove() method.

This is the benchmark result that I compared directly.

https://github.com/jojoldu/spring-batch-benchmark/tree/master/list-item-reader-performance/posts

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions