Skip to content

Commit

Permalink
cgi/Address: cache the GetChildId() value
Browse files Browse the repository at this point in the history
This again eliminates a good amount of per-request overhead.
  • Loading branch information
MaxKellermann committed Jan 23, 2025
1 parent 7ed603c commit a67dfa9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/ResourceAddress.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ ResourceAddress::SaveBase(AllocatorPtr alloc,
gcc_unreachable();
}

inline void
ResourceAddress::PostCacheStore(AllocatorPtr alloc) noexcept
{
switch (type) {
case Type::NONE:
case Type::PIPE:
break;

case Type::CGI:
case Type::FASTCGI:
case Type::WAS:
GetCgi().PostCacheStore(alloc);
break;

case Type::LOCAL:
case Type::HTTP:
case Type::LHTTP:
break;
}
}

void
ResourceAddress::CacheStore(AllocatorPtr alloc,
const ResourceAddress &src,
Expand All @@ -246,6 +267,7 @@ ResourceAddress::CacheStore(AllocatorPtr alloc,
{
if (base == nullptr) {
CopyFrom(alloc, src);
PostCacheStore(alloc);
return;
} else if (const char *tail = base_tail(uri, base)) {
/* we received a valid BASE packet - store only the base
Expand All @@ -255,6 +277,7 @@ ResourceAddress::CacheStore(AllocatorPtr alloc,
/* when the response is expandable, skip appending the
tail URI, don't call SaveBase() */
CopyFrom(alloc, src);
PostCacheStore(alloc);
return;
}

Expand All @@ -267,8 +290,10 @@ ResourceAddress::CacheStore(AllocatorPtr alloc,
}

*this = src.SaveBase(alloc, tail);
if (IsDefined())
if (IsDefined()) {
PostCacheStore(alloc);
return;
}

/* the tail could not be applied to the address, so this is a
base mismatch */
Expand Down
3 changes: 3 additions & 0 deletions src/ResourceAddress.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,7 @@ public:
* Throws std::runtime_error on error.
*/
void Expand(AllocatorPtr alloc, const MatchData &match_data);

private:
void PostCacheStore(AllocatorPtr alloc) noexcept;
};
14 changes: 14 additions & 0 deletions src/cgi/Address.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CgiAddress::CgiAddress(AllocatorPtr alloc, const CgiAddress &src) noexcept
query_string(alloc.CheckDup(src.query_string)),
document_root(alloc.CheckDup(src.document_root)),
address_list(alloc, src.address_list),
cached_child_id(alloc.Dup(src.cached_child_id)),
parallelism(src.parallelism),
concurrency(src.concurrency),
disposable(src.disposable),
Expand All @@ -50,6 +51,16 @@ CgiAddress::CgiAddress(AllocatorPtr alloc, const CgiAddress &src) noexcept
{
}

void
CgiAddress::PostCacheStore(AllocatorPtr alloc) noexcept
{
/* cache the GetChildId() call only if we expect future calls
to have the same result, i.e. none of the relevant fields
are "expandable" */
if ((action != nullptr || !expand_path) && !IsChildExpandable())
cached_child_id = GetChildId(alloc);
}

[[gnu::pure]]
static bool
HasTrailingSlash(const char *p) noexcept
Expand Down Expand Up @@ -93,6 +104,9 @@ CgiAddress::GetURI(AllocatorPtr alloc) const noexcept
StringWithHash
CgiAddress::GetChildId(AllocatorPtr alloc) const noexcept
{
if (!cached_child_id.IsNull())
return cached_child_id;

std::size_t hash = options.GetHash();

PoolStringBuilder<256> b;
Expand Down
12 changes: 12 additions & 0 deletions src/cgi/Address.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "spawn/ChildOptions.hxx"
#include "cluster/AddressList.hxx"
#include "adata/ExpandableStringList.hxx"
#include "util/StringWithHash.hxx"

#include <string_view>

Expand Down Expand Up @@ -48,6 +49,8 @@ struct CgiAddress {
*/
AddressList address_list;

StringWithHash cached_child_id{nullptr};

/**
* The maximum number of parallel child processes of this
* kind.
Expand Down Expand Up @@ -93,6 +96,7 @@ struct CgiAddress {
uri(src.uri), script_name(src.script_name), path_info(src.path_info),
query_string(src.query_string), document_root(src.document_root),
address_list(shallow_copy, src.address_list),
cached_child_id(src.cached_child_id),
concurrency(src.concurrency),
disposable(src.disposable),
request_uri_verbatim(src.request_uri_verbatim),
Expand All @@ -111,6 +115,8 @@ struct CgiAddress {

CgiAddress &operator=(const CgiAddress &) = delete;

void PostCacheStore(AllocatorPtr alloc) noexcept;

[[gnu::pure]]
const char *GetURI(AllocatorPtr alloc) const noexcept;

Expand Down Expand Up @@ -215,6 +221,12 @@ struct CgiAddress {
params.IsExpandable();
}

[[gnu::pure]]
bool IsChildExpandable() const noexcept {
return options.IsExpandable() ||
args.IsExpandable();
}

/**
* Throws std::runtime_error on error.
*/
Expand Down

0 comments on commit a67dfa9

Please sign in to comment.