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

Add a way to configure TypeFactory Jackson uses #4115

Merged
merged 6 commits into from
Sep 26, 2023

Conversation

JooHyukKim
Copy link
Member

@JooHyukKim JooHyukKim commented Sep 12, 2023

(part of #2502)

Blocked by #4111, which has implementations in cleaner form that can be retrofitted to this PR.

@JooHyukKim JooHyukKim marked this pull request as ready for review September 23, 2023 14:43
@@ -205,10 +249,12 @@ public void testBuilderValueValidation() throws Exception
DefaultCacheProvider.builder()
.maxDeserializerCacheSize(0)
.maxSerializerCacheSize(0)
.maxTypeFactoryCacheSize(0)
Copy link
Member

Choose a reason for hiding this comment

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

One thing I wonder: I think that most cache implementations impose a minimum size, so 0 (for example) won't do what user might want -- that is, disabling of caching.

Then again, it is now possibly to add custom cache instances too which can be "no-op" (do nothing) so maybe this doesn't matter.

I also haven't tested out if backing LRUMap honors 0 size or not: JDK Maps do have minimum size but since 2.14 LRUMap uses integrated version of a more optimized caching library (ConcurrentLinkedHashMap).

Copy link
Contributor

Choose a reason for hiding this comment

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

LRUMap does honor max size of 0 (a write occurs, but is immediately evicted). LRUMap is based off ConcurrentLinkedHashMap, which is a predecessor to Caffeine

Caffeine, on the other hand, batches evictions, so the zero maximum size constraint can be temporarily violated.

In addition, Cache2k, Android LruCache, and Ehcache all throw IllegalArgumentException when trying to create a cache with a maximum size of zero.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you @iProdigy . Sounds like we are good then. So 0 size for 2.14+ Jackson implementation will not retain anything in cache, and while there is write overhead that should be negligible.

It'd be nice to test this invariant, but no further work should be necessary it sounds.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm, I am trying to come up with JavaDoc like...

 * Note that setting value of {@code ...} to zero will not .... unlike some Cache providers.

....but idk what to exactly say 🥲 any help?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added JavaDoc and tagged you there.

* and {@link #_buildCache(int)}
* <p>
* Note that value zero is not considered a magic number, unlike some other libraries where it means "disabling cache".
* Instead setting value to zero only means setting cache size to zero.
Copy link
Member Author

Choose a reason for hiding this comment

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

Added JavaDoc as per comment #4115 (comment). WDYT?

/cc @iProdigy @cowtowncoder

Copy link
Contributor

@iProdigy iProdigy Sep 24, 2023

Choose a reason for hiding this comment

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

I would rephrase... For DefaultCacheProvider, zero max size effectively does disable caching since entries are not retained in the underlying LRUMap. Yes, it's technically true that a write & removal occurs under the hood, but users don't care about this detail (other than for optimizing performance, as a special no-op implementation could've been used instead)

Perhaps: Specifying a maximum size of zero prevents values from being retained in the cache

Copy link
Member Author

Choose a reason for hiding this comment

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

but users don't care about this detail

True, they don't need to. Thank you for the suggestion! 🙏🏼🙏🏼 Changed accordingly

@cowtowncoder cowtowncoder merged commit 245fe1c into FasterXML:2.16 Sep 26, 2023
3 checks passed
*
* @since 2.16
*/
public TypeFactory withCaches(CacheProvider cacheProvider) {
Copy link
Member

Choose a reason for hiding this comment

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

I think I'll change this so we won't need a new method -- caller can just construct LookupCache needed and pass that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! Ill check your commit later👍🏼👍🏼

cowtowncoder added a commit that referenced this pull request Sep 26, 2023
@JooHyukKim JooHyukKim deleted the 2502-Add-TypeFactory-Cache branch September 27, 2023 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants