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

Closed
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<>();
Copy link
Member

@cowtowncoder cowtowncoder Jul 10, 2025

Choose a reason for hiding this comment

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

Need not be thread-safe, single deserialization calls are never concurrent. So simple HashMap would do.
Single DeserializationContext is only ever used for single readValue() call.

Also: should not eagerly construct Map since this imposes cost for all usage, even when (like in most cases I'd guess) no injection is required.
So should instead lazily construct when needed.

All of this assuming we actually want caching. Not sold on that yet either.

_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

Copy link
Member

Choose a reason for hiding this comment

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

I don't think that is true: while locating the value only occurs just once, wouldn't actual injection occur multiple times still? While this results in same value being used, it still calls injection (pass via constructor, set field) more than once. Ideally I'd rather neither would happen more than once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. The value is either found in cache or actually resolved, but the injection happens anyway.

}

/**
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