Fix some duplicated SField codes, speed up SField by name lookup

This commit is contained in:
Ed Hennis
2025-03-13 19:55:28 -04:00
parent 687b7944fb
commit 9d1b98a5a4
3 changed files with 24 additions and 11 deletions

View File

@@ -306,6 +306,7 @@ public:
private:
static int num;
static std::map<int, SField const*> knownCodeToField;
static std::map<std::string, SField const*> knownNameToField;
};
/** A field with a type known at compile time. */

View File

@@ -209,7 +209,7 @@ TYPED_SFIELD(sfHookNamespace, UINT256, 32)
TYPED_SFIELD(sfHookSetTxnID, UINT256, 33)
TYPED_SFIELD(sfDomainID, UINT256, 34)
TYPED_SFIELD(sfVaultID, UINT256, 35)
TYPED_SFIELD(sfLoanBrokerID, UINT256, 35)
TYPED_SFIELD(sfLoanBrokerID, UINT256, 36)
// number (common)
TYPED_SFIELD(sfNumber, NUMBER, 1)
@@ -221,11 +221,11 @@ TYPED_SFIELD(sfDebtTotal, NUMBER, 6)
TYPED_SFIELD(sfDebtMaximum, NUMBER, 7)
TYPED_SFIELD(sfCoverAvailable, NUMBER, 8)
TYPED_SFIELD(sfLoanOriginationFee, NUMBER, 9)
TYPED_SFIELD(sfLoanServiceFee, NUMBER, 9)
TYPED_SFIELD(sfLatePaymentFee, NUMBER, 10)
TYPED_SFIELD(sfClosePaymentFee, NUMBER, 11)
TYPED_SFIELD(sfOverpaymentFee, NUMBER, 12)
TYPED_SFIELD(sfPrincipalOutstanding, NUMBER, 13)
TYPED_SFIELD(sfLoanServiceFee, NUMBER, 10)
TYPED_SFIELD(sfLatePaymentFee, NUMBER, 11)
TYPED_SFIELD(sfClosePaymentFee, NUMBER, 12)
TYPED_SFIELD(sfOverpaymentFee, NUMBER, 13)
TYPED_SFIELD(sfPrincipalOutstanding, NUMBER, 14)
// currency amount (common)
TYPED_SFIELD(sfAmount, AMOUNT, 1)

View File

@@ -17,6 +17,7 @@
*/
//==============================================================================
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/protocol/SField.h>
#include <map>
@@ -28,6 +29,7 @@ namespace ripple {
SField::IsSigning const SField::notSigning;
int SField::num = 0;
std::map<int, SField const*> SField::knownCodeToField;
std::map<std::string, SField const*> SField::knownNameToField;
// Give only this translation unit permission to construct SFields
struct SField::private_access_tag_t
@@ -45,7 +47,7 @@ TypedField<T>::TypedField(private_access_tag_t pat, Args&&... args)
}
// Construct all compile-time SFields, and register them in the knownCodeToField
// database:
// and knownNameToField databases:
// Use macros for most SField construction to enforce naming conventions.
#pragma push_macro("UNTYPED_SFIELD")
@@ -99,7 +101,14 @@ SField::SField(
, signingField(signing)
, jsonName(fieldName.c_str())
{
XRPL_ASSERT(
!knownCodeToField.contains(fieldCode),
"ripple::SField::SField(tid,fv,fn,meta,signing) : fieldCode is unique");
XRPL_ASSERT(
!knownNameToField.contains(fieldName),
"ripple::SField::SField(tid,fv,fn,meta,signing) : fieldName is unique");
knownCodeToField[fieldCode] = this;
knownNameToField[fieldName] = this;
}
SField::SField(private_access_tag_t, int fc)
@@ -111,6 +120,9 @@ SField::SField(private_access_tag_t, int fc)
, signingField(IsSigning::yes)
, jsonName(fieldName.c_str())
{
XRPL_ASSERT(
!knownCodeToField.contains(fieldCode),
"ripple::SField::SField(fc) : fieldCode is unique");
knownCodeToField[fieldCode] = this;
}
@@ -145,11 +157,11 @@ SField::compare(SField const& f1, SField const& f2)
SField const&
SField::getField(std::string const& fieldName)
{
for (auto const& [_, f] : knownCodeToField)
auto it = knownNameToField.find(fieldName);
if (it != knownNameToField.end())
{
(void)_;
if (f->fieldName == fieldName)
return *f;
return *(it->second);
}
return sfInvalid;
}