Skip to content

feat(#4218): avoid executing the injection logic multiple times #5201

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 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -4,6 +4,7 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.annotation.JacksonInject;
Expand Down Expand Up @@ -70,6 +71,12 @@ public abstract class DeserializationContext
*/
protected final DeserializerCache _cache;

/**
* Object that caches the injectable values already resolved,
* to avoid executing the injection logic multiple times
*/
protected final Map<Object, Object> _injectablesCache;

/*
/**********************************************************
/* Configuration, changeable via fluent factories
Expand Down Expand Up @@ -169,6 +176,7 @@ protected DeserializationContext(DeserializerFactory df,
cache = new DeserializerCache();
}
_cache = cache;
_injectablesCache = new ConcurrentHashMap<>();
_featureFlags = 0;
_readCapabilities = null;
_config = null;
Expand All @@ -181,6 +189,7 @@ protected DeserializationContext(DeserializationContext src,
DeserializerFactory factory)
{
_cache = src._cache;
_injectablesCache = src._injectablesCache;
_factory = factory;

_config = src._config;
Expand All @@ -199,6 +208,7 @@ protected DeserializationContext(DeserializationContext src,
DeserializerCache cache)
{
_cache = cache;
_injectablesCache = src._injectablesCache;
_factory = src._factory;

_config = src._config;
Expand All @@ -218,6 +228,7 @@ protected DeserializationContext(DeserializationContext src,
InjectableValues injectableValues)
{
_cache = src._cache;
_injectablesCache = src._injectablesCache;
_factory = src._factory;
// 08-Jun-2020. tatu: Called only for `ObjectMapper.canDeserialize()`
// (see [databind#2749]), not sure what's the best work-around but
Expand All @@ -242,6 +253,7 @@ protected DeserializationContext(DeserializationContext src,
DeserializationConfig config)
{
_cache = src._cache;
_injectablesCache = src._injectablesCache;
_factory = src._factory;
_readCapabilities = null;

Expand All @@ -259,6 +271,7 @@ protected DeserializationContext(DeserializationContext src,
*/
protected DeserializationContext(DeserializationContext src) {
_cache = src._cache.emptyCopy();
_injectablesCache = src._injectablesCache;
_factory = src._factory;

_config = src._config;
Expand Down Expand Up @@ -479,7 +492,19 @@ public final Object findInjectableValue(Object valueId,
"No 'injectableValues' configured, cannot inject value with id '%s'", valueId),
valueId, forProperty, beanInstance);
}
return _injectableValues.findInjectableValue(this, valueId, forProperty, beanInstance, optional);

Object value = _injectablesCache.get(valueId);

if (value == null) {
value = _injectableValues.findInjectableValue(this, valueId, forProperty, beanInstance,
optional);

if (value != null) {
_injectablesCache.put(valueId, value);
}
}

return value;
Comment on lines +495 to +507
Copy link
Member

Choose a reason for hiding this comment

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

Would this make injectino logic to run exactly once? Or less than before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly once, as proven by this test, which doesn't fail anymore with the change above

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.databind.tofix;
package com.fasterxml.jackson.databind.deser.inject;

import org.junit.jupiter.api.Test;

Expand All @@ -8,7 +8,6 @@

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -51,7 +50,6 @@ public Object findInjectableValue(
}

// [databind#4218]
@JacksonTestFailureExpected
@Test
void injectFail4218() throws Exception
{
Expand Down