Skip to content

Improve indexOf() performance in DefaultFieldSet #4931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.springframework.lang.Nullable;
Expand All @@ -40,6 +42,7 @@
* @author Rob Harrop
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @author Choi Wang Gyu
*/
public class DefaultFieldSet implements FieldSet {

Expand All @@ -60,6 +63,8 @@ public class DefaultFieldSet implements FieldSet {

private List<String> names;

private Map<String, Integer> nameIndexMap;

/**
* The {@link NumberFormat} to use for parsing numbers. If unset the {@link Locale#US}
* will be used ('.' as decimal place).
Expand Down Expand Up @@ -137,6 +142,10 @@ public DefaultFieldSet(String[] tokens, String[] names, @Nullable DateFormat dat
}
this.tokens = tokens.clone();
this.names = Arrays.asList(names);
this.nameIndexMap = new HashMap<>(names.length);
for (int i = 0; i < names.length; i++) {
this.nameIndexMap.put(names[i], i);
}
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
}
Expand Down Expand Up @@ -450,11 +459,11 @@ protected String readAndTrim(int index) {
* @throws IllegalArgumentException if a column with given name is not defined.
*/
protected int indexOf(String name) {
if (names == null) {
if (nameIndexMap == null) {
throw new IllegalArgumentException("Cannot access columns by name without meta data");
}
int index = names.indexOf(name);
if (index >= 0) {
Integer index = nameIndexMap.get(name);
if (index != null) {
return index;
}
throw new IllegalArgumentException("Cannot access column [" + name + "] from " + names);
Expand Down