1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Improve implementation of cfgOptionIdxName().

Cache option names after they are generated rather than regenerating them each time.
This commit is contained in:
David Steele 2022-01-10 14:47:29 -05:00
parent aced5d47ed
commit aeecb500f5
4 changed files with 27 additions and 7 deletions

View File

@ -46,6 +46,7 @@
<commit subject="Fix inconsistent group display names in messages."/>
<commit subject="Dynamically allocate index to key index map."/>
<commit subject="Replace cfgOptionGroupIdxToKey() with cfgOptionGroupName()."/>
<commit subject="Improve implementation of cfgOptionIdxName()."/>
<release-item-contributor-list>
<release-item-contributor id="david.steele"/>

View File

@ -664,18 +664,34 @@ cfgOptionIdxName(ConfigOption optionId, unsigned int optionIdx)
(configLocal->option[optionId].group && optionIdx <
configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal));
// If an indexed option
if (configLocal->option[optionId].group)
{
// This is somewhat less than ideal since memory is being allocated with each call, rather than caching prior results. In
// practice the number of allocations should be quite small so we'll ignore this for now.
String *name = strNewFmt(
// Generate indexed names for the option the first time one is requested
if (configLocal->option[optionId].indexName == NULL)
{
MEM_CONTEXT_BEGIN(configLocal->memContext)
{
const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal;
configLocal->option[optionId].indexName = memNew(sizeof(String *) * indexTotal);
for (unsigned int optionIdx = 0; optionIdx < indexTotal; optionIdx++)
{
configLocal->option[optionId].indexName[optionIdx] = strNewFmt(
"%s%u%s", configLocal->optionGroup[configLocal->option[optionId].groupId].name,
configLocal->optionGroup[configLocal->option[optionId].groupId].indexMap[optionIdx] + 1,
configLocal->option[optionId].name + strlen(configLocal->optionGroup[configLocal->option[optionId].groupId].name));
FUNCTION_TEST_RETURN(strZ(name));
configLocal->option[optionId].name +
strlen(configLocal->optionGroup[configLocal->option[optionId].groupId].name));
}
}
MEM_CONTEXT_END();
}
FUNCTION_TEST_RETURN(strZ(configLocal->option[optionId].indexName[optionIdx]));
}
// Else not indexed
FUNCTION_TEST_RETURN(configLocal->option[optionId].name);
}

View File

@ -75,6 +75,7 @@ typedef struct Config
ConfigOptionDataType dataType; // Underlying data type
const String *defaultValue; // Default value
ConfigOptionValue *index; // List of indexed values (only 1 unless the option is indexed)
const String **indexName; // Index names (e.g. repo1-path, repo2-path)
} option[CFG_OPTION_TOTAL];
} Config;

View File

@ -1687,6 +1687,8 @@ testRun(void)
TEST_RESULT_BOOL(cfgOptionIdxReset(cfgOptPgHost, 0), true, "pg1-host was reset");
TEST_RESULT_UINT(cfgOptionGroupIdxDefault(cfgOptGrpPg), 0, "pg1 is default");
TEST_RESULT_UINT(cfgOptionGroupIdxToKey(cfgOptGrpPg, 1), 2, "pg2 is index 2");
TEST_RESULT_Z(cfgOptionIdxName(cfgOptPgPath, 0), "pg1-path", "pg1-path option name");
TEST_RESULT_Z(cfgOptionIdxName(cfgOptPgPath, 1), "pg2-path", "pg2-path option name");
TEST_RESULT_Z(cfgOptionGroupName(cfgOptGrpPg, 1), "pg2", "pg2 group display");
TEST_RESULT_Z(cfgOptionGroupName(cfgOptGrpPg, 0), "pg1", "pg1 group display (cached)");
TEST_RESULT_STR_Z(cfgOptionStr(cfgOptPgPath), "/path/to/db", "default pg-path");