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

Replace cfgCommandTotal()/cfgOptionTotal() functions with constants.

The constants are applicable in more cases and allow the compiler to optimize certain loops more efficiently.
This commit is contained in:
David Steele 2017-12-05 09:45:51 -05:00
parent 59fd92fdd5
commit 4f1725dc36
9 changed files with 85 additions and 57 deletions

View File

@ -67,6 +67,25 @@ sub buildAll
"#ifndef ${strHeaderDefine}\n" .
"#define ${strHeaderDefine}\n";
# Iterate constant groups
foreach my $strConstantGroup (sort(keys(%{$rhFileConstant})))
{
my $rhConstantGroup = $rhFileConstant->{$strConstantGroup};
$strHeader .= "\n" . bldBanner($rhConstantGroup->{&BLD_SUMMARY} . ' constants');
# Iterate constants
foreach my $strConstant (sort(keys(%{$rhConstantGroup->{&BLD_CONSTANT}})))
{
my $rhConstant = $rhConstantGroup->{&BLD_CONSTANT}{$strConstant};
$strHeader .=
"#define ${strConstant} " . (' ' x (69 - length($strConstant) - 10)) .
$rhConstant->{&BLD_CONSTANT_VALUE} . "\n";
}
}
# Iterate enum groups
foreach my $strEnum (sort(keys(%{$rhFileEnum})))
{
my $rhEnum = $rhFileEnum->{$strEnum};
@ -100,24 +119,6 @@ sub buildAll
"} " . $rhEnum->{&BLD_NAME} . ";\n";
}
# Iterate constant groups
foreach my $strConstantGroup (sort(keys(%{$rhFileConstant})))
{
my $rhConstantGroup = $rhFileConstant->{$strConstantGroup};
$strHeader .= "\n" . bldBanner($rhConstantGroup->{&BLD_SUMMARY} . ' constants');
# Iterate constants
foreach my $strConstant (sort(keys(%{$rhConstantGroup->{&BLD_CONSTANT}})))
{
my $rhConstant = $rhConstantGroup->{&BLD_CONSTANT}{$strConstant};
$strHeader .=
"#define ${strConstant} " . (' ' x (69 - length($strConstant) - 10)) .
$rhConstant->{&BLD_CONSTANT_VALUE} . "\n";
}
}
$strHeader .=
"\n#endif";

View File

@ -27,12 +27,18 @@ use pgBackRestBuild::Config::Data;
####################################################################################################################################
use constant BLDLCL_FILE_CONFIG => 'config';
use constant BLDLCL_CONSTANT_COMMAND => '01-constantCommand';
use constant BLDLCL_CONSTANT_COMMAND_TOTAL => 'CFG_COMMAND_TOTAL';
use constant BLDLCL_CONSTANT_OPTION => '02-constantOption';
use constant BLDLCL_CONSTANT_OPTION_TOTAL => 'CFG_OPTION_TOTAL';
use constant BLDLCL_DATA_COMMAND => '01-command';
use constant BLDLCL_DATA_OPTION => '02-option';
use constant BLDLCL_ENUM_COMMAND => '01-enumCommand';
use constant BLDLCL_ENUM_OPTION => '02-enumOption';
####################################################################################################################################
# Definitions for constants and data to build
####################################################################################################################################
@ -45,6 +51,18 @@ my $rhBuild =
{
&BLD_SUMMARY => 'Command and Option Configuration',
&BLD_CONSTANT_GROUP =>
{
&BLDLCL_CONSTANT_COMMAND =>
{
&BLD_SUMMARY => 'Command',
},
&BLDLCL_CONSTANT_OPTION =>
{
&BLD_SUMMARY => 'Option',
},
},
&BLD_ENUM =>
{
&BLDLCL_ENUM_COMMAND =>
@ -103,9 +121,10 @@ sub buildConfig
# Build command constants and data
#-------------------------------------------------------------------------------------------------------------------------------
my $rhEnum = $rhBuild->{&BLD_FILE}{&BLDLCL_FILE_CONFIG}{&BLD_ENUM}{&BLDLCL_ENUM_COMMAND};
my $iCommandTotal = 0;
my $strBuildSource =
"ConfigCommandData configCommandData[] = CONFIG_COMMAND_LIST\n" .
'ConfigCommandData configCommandData[' . BLDLCL_CONSTANT_COMMAND_TOTAL . "] = CONFIG_COMMAND_LIST\n" .
"(";
foreach my $strCommand (cfgDefineCommandList())
@ -121,6 +140,8 @@ sub buildConfig
" (\n" .
" CONFIG_COMMAND_NAME(\"${strCommand}\")\n" .
" )\n";
$iCommandTotal++;
}
# Add "none" command that is used to initialize the current command before anything is parsed
@ -131,6 +152,10 @@ sub buildConfig
$rhBuild->{&BLD_FILE}{&BLDLCL_FILE_CONFIG}{&BLD_DATA}{&BLDLCL_DATA_COMMAND}{&BLD_SOURCE} = $strBuildSource;
# Set option total constant
$rhBuild->{&BLD_FILE}{&BLDLCL_FILE_CONFIG}{&BLD_CONSTANT_GROUP}{&BLDLCL_CONSTANT_COMMAND}{&BLD_CONSTANT}
{&BLDLCL_CONSTANT_COMMAND_TOTAL}{&BLD_CONSTANT_VALUE} = $iCommandTotal;
# Build option constants and data
#-------------------------------------------------------------------------------------------------------------------------------
my $rhConfigDefine = cfgDefine();
@ -138,7 +163,7 @@ sub buildConfig
my $iOptionTotal = 0;
$strBuildSource =
"ConfigOptionData configOptionData[] = CONFIG_OPTION_LIST\n" .
'ConfigOptionData configOptionData[' . BLDLCL_CONSTANT_OPTION_TOTAL . "] = CONFIG_OPTION_LIST\n" .
"(";
foreach my $strOption (sort(keys(%{$rhConfigDefine})))
@ -178,6 +203,10 @@ sub buildConfig
$rhBuild->{&BLD_FILE}{&BLDLCL_FILE_CONFIG}{&BLD_DATA}{&BLDLCL_DATA_OPTION}{&BLD_SOURCE} = $strBuildSource;
# Set option total constant
$rhBuild->{&BLD_FILE}{&BLDLCL_FILE_CONFIG}{&BLD_CONSTANT_GROUP}{&BLDLCL_CONSTANT_OPTION}{&BLD_CONSTANT}
{&BLDLCL_CONSTANT_OPTION_TOTAL}{&BLD_CONSTANT_VALUE} = $iOptionTotal;
return $rhBuild;
}

View File

@ -50,6 +50,10 @@
<p>Minor changes to <code>Manifest</code> module, mostly for test reproducibility.</p>
</release-item>
<release-item>
<p>Replace <code>cfgCommandTotal()</code>/<code>cfgOptionTotal()</code> functions with constants. The constants are applicable in more cases and allow the compiler to optimize certain loops more efficiently.</p>
</release-item>
</release-development-list>
</release-core-list>

View File

@ -368,3 +368,7 @@ OUTPUT:
U32
cfgOptionTotal()
CODE:
RETVAL = CFG_OPTION_TOTAL;
OUTPUT:
RETVAL

View File

@ -7,7 +7,7 @@ Automatically generated by Build.pm -- do not modify directly.
/***********************************************************************************************************************************
Command data
***********************************************************************************************************************************/
ConfigCommandData configCommandData[] = CONFIG_COMMAND_LIST
ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_LIST
(
CONFIG_COMMAND
(
@ -88,7 +88,7 @@ ConfigCommandData configCommandData[] = CONFIG_COMMAND_LIST
/***********************************************************************************************************************************
Option data
***********************************************************************************************************************************/
ConfigOptionData configOptionData[] = CONFIG_OPTION_LIST
ConfigOptionData configOptionData[CFG_OPTION_TOTAL] = CONFIG_OPTION_LIST
(
//------------------------------------------------------------------------------------------------------------------------------
CONFIG_OPTION

View File

@ -6,6 +6,16 @@ Automatically generated by Build.pm -- do not modify directly.
#ifndef CONFIG_CONFIG_AUTO_H
#define CONFIG_CONFIG_AUTO_H
/***********************************************************************************************************************************
Command constants
***********************************************************************************************************************************/
#define CFG_COMMAND_TOTAL 15
/***********************************************************************************************************************************
Option constants
***********************************************************************************************************************************/
#define CFG_OPTION_TOTAL 138
/***********************************************************************************************************************************
Command enum
***********************************************************************************************************************************/

View File

@ -83,8 +83,8 @@ Ensure that command id is valid
void
cfgCommandCheck(ConfigCommand commandId)
{
if (commandId >= cfgCommandTotal())
THROW(AssertError, "command id %d invalid - must be >= 0 and < %d", commandId, cfgCommandTotal());
if (commandId >= CFG_COMMAND_TOTAL)
THROW(AssertError, "command id %d invalid - must be >= 0 and < %d", commandId, CFG_COMMAND_TOTAL);
}
/***********************************************************************************************************************************
@ -108,11 +108,11 @@ cfgCommandId(const char *commandName)
{
ConfigCommand commandId;
for (commandId = 0; commandId < cfgCommandTotal(); commandId++)
for (commandId = 0; commandId < CFG_COMMAND_TOTAL; commandId++)
if (strcmp(commandName, configCommandData[commandId].name) == 0)
break;
if (commandId == cfgCommandTotal())
if (commandId == CFG_COMMAND_TOTAL)
THROW(AssertError, "invalid command '%s'", commandName);
return commandId;
@ -128,23 +128,14 @@ cfgCommandName(ConfigCommand commandId)
return configCommandData[commandId].name;
}
/***********************************************************************************************************************************
cfgCommandTotal - total number of commands
***********************************************************************************************************************************/
unsigned int
cfgCommandTotal()
{
return sizeof(configCommandData) / sizeof(ConfigCommandData);
}
/***********************************************************************************************************************************
Ensure that option id is valid
***********************************************************************************************************************************/
void
cfgOptionCheck(ConfigOption optionId)
{
if (optionId >= cfgOptionTotal())
THROW(AssertError, "option id %d invalid - must be >= 0 and < %d", optionId, cfgOptionTotal());
if (optionId >= CFG_OPTION_TOTAL)
THROW(AssertError, "option id %d invalid - must be >= 0 and < %d", optionId, CFG_OPTION_TOTAL);
}
/***********************************************************************************************************************************
@ -173,7 +164,7 @@ Get option id by name
int
cfgOptionId(const char *optionName)
{
for (ConfigOption optionId = 0; optionId < cfgOptionTotal(); optionId++)
for (ConfigOption optionId = 0; optionId < CFG_OPTION_TOTAL; optionId++)
if (strcmp(optionName, configOptionData[optionId].name) == 0)
return optionId;
@ -199,12 +190,12 @@ cfgOptionIdFromDefId(ConfigDefineOption optionDefId, int index)
// Search for the option
ConfigOption optionId;
for (optionId = 0; optionId < cfgOptionTotal(); optionId++)
for (optionId = 0; optionId < CFG_OPTION_TOTAL; optionId++)
if (configOptionData[optionId].defineId == optionDefId)
break;
// Error when not found
if (optionId == cfgOptionTotal())
if (optionId == CFG_OPTION_TOTAL)
cfgDefOptionCheck(optionDefId);
// Return with original index
@ -220,12 +211,3 @@ cfgOptionName(ConfigOption optionId)
cfgOptionCheck(optionId);
return configOptionData[optionId].name;
}
/***********************************************************************************************************************************
cfgOptionTotal - total number of configuration options
***********************************************************************************************************************************/
unsigned int
cfgOptionTotal()
{
return sizeof(configOptionData) / sizeof(ConfigOptionData);
}

View File

@ -17,7 +17,6 @@ int cfgCommandId(const char *commandName);
const char *cfgCommandName(ConfigCommand commandId);
ConfigDefineCommand cfgCommandDefIdFromId(ConfigCommand commandId);
void cfgCommandSet(ConfigCommand commandParam);
unsigned int cfgCommandTotal();
int cfgOptionId(const char *optionName);
ConfigOption cfgOptionIdFromDefId(ConfigDefineOption optionDefId, int index);
@ -25,6 +24,5 @@ int cfgOptionIndex(ConfigOption optionId);
int cfgOptionIndexTotal(ConfigOption optionDefId);
const char *cfgOptionName(ConfigOption optionId);
ConfigDefineOption cfgOptionDefIdFromId(ConfigOption optionId);
unsigned int cfgOptionTotal();
#endif

View File

@ -22,28 +22,28 @@ void testRun()
char optionIdInvalidHighError[256];
snprintf(
optionIdInvalidHighError, sizeof(optionIdInvalidHighError), "option id %d invalid - must be >= 0 and < %d",
cfgOptionTotal(), cfgOptionTotal());
CFG_OPTION_TOTAL, CFG_OPTION_TOTAL);
char optionIdInvalidLowError[256];
snprintf(
optionIdInvalidLowError, sizeof(optionIdInvalidLowError), "option id -1 invalid - must be >= 0 and < %d",
cfgOptionTotal());
CFG_OPTION_TOTAL);
char commandIdInvalidHighError[256];
snprintf(
commandIdInvalidHighError, sizeof(commandIdInvalidHighError), "command id %d invalid - must be >= 0 and < %d",
cfgCommandTotal(), cfgCommandTotal());
CFG_COMMAND_TOTAL, CFG_COMMAND_TOTAL);
char commandIdInvalidLowError[256];
snprintf(
commandIdInvalidLowError, sizeof(commandIdInvalidLowError), "command id -1 invalid - must be >= 0 and < %d",
cfgCommandTotal());
CFG_COMMAND_TOTAL);
// -------------------------------------------------------------------------------------------------------------------------
TEST_ERROR(cfgCommandId(BOGUS_STR), AssertError, "invalid command 'BOGUS'");
TEST_RESULT_INT(cfgCommandId("archive-push"), cfgCmdArchivePush, "command id from name");
TEST_ERROR(cfgCommandDefIdFromId(cfgCommandTotal()), AssertError, commandIdInvalidHighError);
TEST_ERROR(cfgCommandDefIdFromId(CFG_COMMAND_TOTAL), AssertError, commandIdInvalidHighError);
TEST_RESULT_INT(cfgCommandDefIdFromId(cfgCmdBackup), cfgDefCmdBackup, "command id to def id");
TEST_ERROR(cfgCommandName(-1), AssertError, commandIdInvalidLowError);
@ -57,7 +57,7 @@ void testRun()
TEST_ERROR(cfgOptionIdFromDefId(cfgDefOptionTotal(), 6), AssertError, optionDefIdInvalidHighError);
TEST_RESULT_INT(cfgOptionIdFromDefId(cfgDefOptDbHost, 6), cfgOptDbHost + 6, "option def id to id");
TEST_ERROR(cfgOptionIndex(cfgOptionTotal()), AssertError, optionIdInvalidHighError);
TEST_ERROR(cfgOptionIndex(CFG_OPTION_TOTAL), AssertError, optionIdInvalidHighError);
TEST_RESULT_INT(cfgOptionIndex(cfgOptDbCmd + 6), 6, "option index");
TEST_RESULT_INT(cfgOptionIndex(cfgOptCompressLevel), 0, "option index");