Skip to content

Commit 72d979e

Browse files
committed
Fix some duplicated SField codes, speed up SField by name lookup
1 parent 8de7849 commit 72d979e

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

include/xrpl/protocol/SField.h

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class SField
306306
private:
307307
static int num;
308308
static std::map<int, SField const*> knownCodeToField;
309+
static std::map<std::string, SField const*> knownNameToField;
309310
};
310311

311312
/** A field with a type known at compile time. */

include/xrpl/protocol/detail/sfields.macro

+6-6
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ TYPED_SFIELD(sfHookNamespace, UINT256, 32)
208208
TYPED_SFIELD(sfHookSetTxnID, UINT256, 33)
209209
TYPED_SFIELD(sfDomainID, UINT256, 34)
210210
TYPED_SFIELD(sfVaultID, UINT256, 35)
211-
TYPED_SFIELD(sfLoanBrokerID, UINT256, 35)
211+
TYPED_SFIELD(sfLoanBrokerID, UINT256, 36)
212212

213213
// number (common)
214214
TYPED_SFIELD(sfNumber, NUMBER, 1)
@@ -220,11 +220,11 @@ TYPED_SFIELD(sfDebtTotal, NUMBER, 6)
220220
TYPED_SFIELD(sfDebtMaximum, NUMBER, 7)
221221
TYPED_SFIELD(sfCoverAvailable, NUMBER, 8)
222222
TYPED_SFIELD(sfLoanOriginationFee, NUMBER, 9)
223-
TYPED_SFIELD(sfLoanServiceFee, NUMBER, 9)
224-
TYPED_SFIELD(sfLatePaymentFee, NUMBER, 10)
225-
TYPED_SFIELD(sfClosePaymentFee, NUMBER, 11)
226-
TYPED_SFIELD(sfOverpaymentFee, NUMBER, 12)
227-
TYPED_SFIELD(sfPrincipalOutstanding, NUMBER, 13)
223+
TYPED_SFIELD(sfLoanServiceFee, NUMBER, 10)
224+
TYPED_SFIELD(sfLatePaymentFee, NUMBER, 11)
225+
TYPED_SFIELD(sfClosePaymentFee, NUMBER, 12)
226+
TYPED_SFIELD(sfOverpaymentFee, NUMBER, 13)
227+
TYPED_SFIELD(sfPrincipalOutstanding, NUMBER, 14)
228228

229229
// currency amount (common)
230230
TYPED_SFIELD(sfAmount, AMOUNT, 1)

src/libxrpl/protocol/SField.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
//==============================================================================
1919

20+
#include <xrpl/beast/utility/instrumentation.h>
2021
#include <xrpl/protocol/SField.h>
2122

2223
#include <map>
@@ -28,6 +29,7 @@ namespace ripple {
2829
SField::IsSigning const SField::notSigning;
2930
int SField::num = 0;
3031
std::map<int, SField const*> SField::knownCodeToField;
32+
std::map<std::string, SField const*> SField::knownNameToField;
3133

3234
// Give only this translation unit permission to construct SFields
3335
struct SField::private_access_tag_t
@@ -45,7 +47,7 @@ TypedField<T>::TypedField(private_access_tag_t pat, Args&&... args)
4547
}
4648

4749
// Construct all compile-time SFields, and register them in the knownCodeToField
48-
// database:
50+
// and knownNameToField databases:
4951

5052
// Use macros for most SField construction to enforce naming conventions.
5153
#pragma push_macro("UNTYPED_SFIELD")
@@ -99,7 +101,14 @@ SField::SField(
99101
, signingField(signing)
100102
, jsonName(fieldName.c_str())
101103
{
104+
XRPL_ASSERT(
105+
!knownCodeToField.contains(fieldCode),
106+
"ripple::SField::SField(tid,fv,fn,meta,signing) : fieldCode is unique");
107+
XRPL_ASSERT(
108+
!knownNameToField.contains(fieldName),
109+
"ripple::SField::SField(tid,fv,fn,meta,signing) : fieldName is unique");
102110
knownCodeToField[fieldCode] = this;
111+
knownNameToField[fieldName] = this;
103112
}
104113

105114
SField::SField(private_access_tag_t, int fc)
@@ -111,6 +120,9 @@ SField::SField(private_access_tag_t, int fc)
111120
, signingField(IsSigning::yes)
112121
, jsonName(fieldName.c_str())
113122
{
123+
XRPL_ASSERT(
124+
!knownCodeToField.contains(fieldCode),
125+
"ripple::SField::SField(fc) : fieldCode is unique");
114126
knownCodeToField[fieldCode] = this;
115127
}
116128

@@ -145,11 +157,11 @@ SField::compare(SField const& f1, SField const& f2)
145157
SField const&
146158
SField::getField(std::string const& fieldName)
147159
{
148-
for (auto const& [_, f] : knownCodeToField)
160+
auto it = knownNameToField.find(fieldName);
161+
162+
if (it != knownNameToField.end())
149163
{
150-
(void)_;
151-
if (f->fieldName == fieldName)
152-
return *f;
164+
return *(it->second);
153165
}
154166
return sfInvalid;
155167
}

0 commit comments

Comments
 (0)