Skip to content

Cache Query Fields in core requests #13248

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

Draft
wants to merge 1 commit into
base: jetty-12.1.x
Choose a base branch
from

Conversation

gregw
Copy link
Contributor

@gregw gregw commented Jun 16, 2025

Fix #13242 by caching query fields in core requests

Fix #13242 by caching query fields in core requests
Comment on lines +932 to +964
@Override
public Object setAttribute(String name, Object attribute)
{
// Short cut for known common implementation attributes to avoid the creation of a lazy attribute map.
// These attributes are hidden from name set
return switch (name)
{
case CachedQueryFields.KEY ->
{
CachedQueryFields cqf = _cachedQueryFields;
_cachedQueryFields = attribute instanceof CachedQueryFields fields ? fields : null;
yield cqf;
}
case COOKIE_ATTRIBUTE ->
{
CookieCache old = _cookieCache;
_cookieCache = attribute instanceof CookieCache cookieCache ? cookieCache : null;
yield old;
}
default -> super.setAttribute(name, attribute);
};
}

@Override
public Object getAttribute(String name)
{
return switch (name)
{
case CachedQueryFields.KEY -> _cachedQueryFields;
case COOKIE_ATTRIBUTE -> _cookieCache;
default -> super.getAttribute(name);
};
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lorban @sbordet This is probably a second order optimisation that need not be in this PR, but I thought I'd raise the question.

We now have a few request attributes used for caching derived data. This means that very few requests will actually not have attributes. So we probably should either just remove the Lazy attribute implementation or make an effort like this so that common attributes will not force an attribute creation.

I don't think this is so much about speed of lookup, but more about allocation of a map of attributes per request. But looking at ConcurrentHashMap, it looks very cheap to create an empty one, so I'm not sure lazy attributes ios worthwhile at all.

Perhaps, this PR should just be about the CachedQueryFields and we open another PR/Issue to examine if Lazy attributes are needed and if this kind of "optimization" is worthwhile

@gregw
Copy link
Contributor Author

gregw commented Jun 16, 2025

@lachlan-roberts @sbordet @lorban This is an almost trivial PR... except it does raise the question about do we need Lazy attributes anymore and/or if optimizations like this PR attempts are worthwhile to avoid creating the lazy attribute map.
This is a 12.1.1 thing, but would be nice to remove a bit of complexity if just using a non-lazy ConcurrentHashMap is OK.

@gregw gregw linked an issue Jun 16, 2025 that may be closed by this pull request
@joakime
Copy link
Contributor

joakime commented Jun 16, 2025

Wouldn't it be better to cache them in the HttpURI object itself?
That way if the HttpURI is changed/replaced, the cache goes away properly.
Besides, the query belongs to the HttpURI object anyway.

@gregw
Copy link
Contributor Author

gregw commented Jun 17, 2025

Wouldn't it be better to cache them in the HttpURI object itself? That way if the HttpURI is changed/replaced, the cache goes away properly. Besides, the query belongs to the HttpURI object anyway.

Interesting idea.... investigating....

@gregw
Copy link
Contributor Author

gregw commented Jun 17, 2025

Wouldn't it be better to cache them in the HttpURI object itself? That way if the HttpURI is changed/replaced, the cache goes away properly. Besides, the query belongs to the HttpURI object anyway.

Interesting idea.... investigating....

@joakime it kind of works, but kind of doesn't. The cached Fields are not just related to the HttpURI, but to also the the Charset, UriCompliance and ComplianceViolation.Listener as well. So it really is a cache of the value resulting from that tuple. I could put a fat method on HttpURI that takes all those args.... so hmmmm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Keep extracted query params as attribute for jetty-core
2 participants