Skip to content
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

Makes EnvName mutable to avoid allocating it for lookups #1295

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
147 changes: 105 additions & 42 deletions implementation/src/main/java/io/smallrye/config/EnvConfigSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class EnvConfigSource extends AbstractConfigSource {
public static final int ORDINAL = 300;

private final EnvVars envVars;
private EnvName reusableEnvName;

protected EnvConfigSource() {
this(ORDINAL);
Expand All @@ -74,6 +75,7 @@ protected EnvConfigSource(final int ordinal) {
public EnvConfigSource(final Map<String, String> properties, final int ordinal) {
super(NAME, getEnvOrdinal(properties, ordinal));
this.envVars = new EnvVars(properties);
this.reusableEnvName = null;
}

@Override
Expand Down Expand Up @@ -103,7 +105,18 @@ public String getValue(final String propertyName) {
}

boolean hasPropertyName(final String propertyName) {
return envVars.getEnv().containsKey(new EnvName(propertyName));
EnvName envName = reusableEnvName;
if (envName == null) {
envName = new EnvName(propertyName);
this.reusableEnvName = envName;
} else {
envName.setName(propertyName);
}
try {
return envVars.getEnv().containsKey(envName);
} finally {
envName.reset();
}
Comment on lines +108 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can cause issues if not thread-safe. While we use a simple HashMap, we never modify it after creation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make #1295 (comment) once you apply your changes :P

}

/**
Expand Down Expand Up @@ -148,48 +161,75 @@ static final class EnvVars implements Serializable {

private final Map<EnvName, EnvEntry> env;
private final Set<String> names;
private EnvName reusableEnvName;

public EnvVars(final Map<String, String> properties) {
this.env = new HashMap<>(properties.size());
this.names = new HashSet<>(properties.size() * 2);
properties.forEach(new BiConsumer<>() {
@Override
public void accept(String key, String value) {
EnvName envName = new EnvName(key);
EnvEntry envEntry = env.get(envName);
if (envEntry == null) {
env.put(envName, new EnvEntry(key, value));
} else {
envEntry.add(key, value);
EnvName envName = tmpEnvNameWith(key);
try {
EnvEntry envEntry = env.get(envName);
if (envEntry == null) {
env.put(envName, new EnvEntry(key, value));
// it means envName cannot be reused
envName = null;
} else {
envEntry.add(key, value);
}
EnvVars.this.names.add(key);
EnvVars.this.names.add(toLowerCaseAndDotted(key));
} finally {
if (envName != null) {
envName.reset();
} else {
reusableEnvName = null;
}
}
EnvVars.this.names.add(key);
EnvVars.this.names.add(toLowerCaseAndDotted(key));
}
});
}

public String get(final String propertyName) {
EnvEntry envEntry = env.get(new EnvName(propertyName));
if (envEntry != null) {
String value = envEntry.get();
if (value != null) {
return value;
}
EnvName tmpEnvName = tmpEnvNameWith(propertyName);
try {
EnvEntry envEntry = env.get(tmpEnvName);
if (envEntry != null) {
String value = envEntry.get();
if (value != null) {
return value;
}

value = envEntry.getEntries().get(propertyName);
if (value != null) {
return value;
}
value = envEntry.getEntries().get(propertyName);
if (value != null) {
return value;
}

String envName = replaceNonAlphanumericByUnderscores(propertyName);
value = envEntry.getEntries().get(envName);
if (value != null) {
return value;
}

String envName = replaceNonAlphanumericByUnderscores(propertyName);
value = envEntry.getEntries().get(envName);
if (value != null) {
return value;
return envEntry.envEntries.get(envName.toUpperCase());
}
return null;
} finally {
tmpEnvName.reset();
}
}

return envEntry.envEntries.get(envName.toUpperCase());
private EnvName tmpEnvNameWith(String propertyName) {
EnvName envName = reusableEnvName;
if (envName == null) {
envName = new EnvName(propertyName);
this.reusableEnvName = envName;
} else {
envName.setName(propertyName);
}
return null;
return envName;
}

public Map<EnvName, EnvEntry> getEnv() {
Expand All @@ -204,11 +244,27 @@ public Set<String> getNames() {
static final class EnvName implements Serializable {
private static final long serialVersionUID = -2679716955093904512L;

private final String name;
private String name;
private int hash;
private boolean hashIsZero;

public EnvName(final String name) {
assert name != null;
this.name = name;
this.hashIsZero = false;
this.hash = 0;
}

public void reset() {
this.name = null;
this.hashIsZero = false;
this.hash = 0;
}

public void setName(final String name) {
this.name = name;
this.hashIsZero = false;
this.hash = 0;
}

public String getName() {
Expand All @@ -229,26 +285,33 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {
int h = 0;
int length = name.length();
if (length >= 2) {
if (name.charAt(length - 1) == '_' && name.charAt(length - 2) == '_') {
length = length - 1;
int h = hash;
if (h == 0 && !hashIsZero) {
int length = name.length();
if (length >= 2) {
if (name.charAt(length - 1) == '_' && name.charAt(length - 2) == '_') {
length = length - 1;
}
}
}

for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (i == 0 && length > 1) {
// The first '%' or '_' is meaninful because it represents a profiled property name
if ((c == '%' || c == '_') && isAsciiLetterOrDigit(name.charAt(i + 1))) {
h = 31 * h + 31;
continue;
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (i == 0 && length > 1) {
// The first '%' or '_' is meaninful because it represents a profiled property name
if ((c == '%' || c == '_') && isAsciiLetterOrDigit(name.charAt(i + 1))) {
h = 31 * h + 31;
continue;
}
}
}

if (isAsciiLetterOrDigit(c)) {
h = 31 * h + toLowerCase(c);
if (isAsciiLetterOrDigit(c)) {
h = 31 * h + toLowerCase(c);
}
}
if (h == 0) {
hashIsZero = true;
} else {
hash = h;
}
}
return h;
Expand Down
Loading