Skip to content

Commit

Permalink
Merge pull request #5317 from amousset/function-name-cache
Browse files Browse the repository at this point in the history
CFE-4244: Add the function name to the result cache key
  • Loading branch information
larsewi authored Sep 15, 2023
2 parents d1df034 + eec9776 commit 4eb6a9d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
20 changes: 18 additions & 2 deletions libpromises/eval_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -2832,12 +2832,20 @@ bool EvalContextFunctionCacheGet(const EvalContext *ctx,
const FnCall *fp ARG_UNUSED,
const Rlist *args, Rval *rval_out)
{
assert(fp != NULL);
assert(fp->name != NULL);
assert(ctx != NULL);

if (!(ctx->eval_options & EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS))
{
return false;
}

Rval *rval = FuncCacheMapGet(ctx->function_cache, args);
// The cache key is made of the function name and all args values
Rlist *args_copy = RlistCopy(args);
Rlist *key = RlistPrepend(&args_copy, fp->name, RVAL_TYPE_SCALAR);
Rval *rval = FuncCacheMapGet(ctx->function_cache, key);
RlistDestroy(key);
if (rval)
{
if (rval_out)
Expand All @@ -2856,14 +2864,22 @@ void EvalContextFunctionCachePut(EvalContext *ctx,
const FnCall *fp ARG_UNUSED,
const Rlist *args, const Rval *rval)
{
assert(fp != NULL);
assert(fp->name != NULL);
assert(ctx != NULL);

if (!(ctx->eval_options & EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS))
{
return;
}

Rval *rval_copy = xmalloc(sizeof(Rval));
*rval_copy = RvalCopy(*rval);
FuncCacheMapInsert(ctx->function_cache, RlistCopy(args), rval_copy);

Rlist *args_copy = RlistCopy(args);
Rlist *key = RlistPrepend(&args_copy, fp->name, RVAL_TYPE_SCALAR);

FuncCacheMapInsert(ctx->function_cache, key, rval_copy);
}

/* cfPS and associated machinery */
Expand Down
51 changes: 51 additions & 0 deletions tests/acceptance/01_vars/02_functions/cache_name.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#######################################################
#
# Test that the function result cache checks function name
#
#######################################################

body common control
{
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

#######################################################


bundle agent init
{
vars:
"agent_regex" string => ".*cf-agent.*";
}

#######################################################

bundle common test
{
meta:
"description" -> { "CFE-4244" }
string => "Test that the function result cache checks function name";

vars:
"res1" data => findprocesses("${init.agent_regex}");

classes:
# must not reuse result from previous line
# is reused, produces a type error
"_pass" expression => processexists("${init.agent_regex}");
}


#######################################################

bundle agent check
{
methods:
_pass::
"pass" usebundle => dcs_pass("$(this.promise_filename)");

!_pass::
"pass" usebundle => dcs_fail("$(this.promise_filename)");
}

0 comments on commit 4eb6a9d

Please sign in to comment.