@@ -91,22 +91,59 @@ void TYsonStructBase::Save(IYsonConsumer* consumer) const
91
91
consumer->OnEndMap ();
92
92
}
93
93
94
- void TYsonStructBase::SaveAsMapFragment (NYson::IYsonConsumer* consumer) const
94
+ void TYsonStructBase::SaveRecognizedAsMapFragment (NYson::IYsonConsumer* consumer) const
95
95
{
96
96
for (const auto & [name, parameter] : Meta_->GetParameterSortedList ()) {
97
97
if (!parameter->CanOmitValue (this )) {
98
98
consumer->OnKeyedItem (name);
99
99
parameter->Save (this , consumer);
100
100
}
101
101
}
102
+ }
103
+
104
+ void TYsonStructBase::SaveAsMapFragment (NYson::IYsonConsumer* consumer) const
105
+ {
106
+ if (!LocalUnrecognized_) {
107
+ // Fast path.
108
+ return SaveRecognizedAsMapFragment (consumer);
109
+ }
110
+
111
+ const auto & recognizedList = Meta_->GetParameterSortedList ();
112
+ auto recognizedIt = recognizedList.begin ();
113
+
114
+ auto unrecognizedList = LocalUnrecognized_->GetChildren ();
115
+ SortBy (unrecognizedList, [] (const auto & item) { return item.first ; });
116
+ auto unrecognizedIt = unrecognizedList.begin ();
102
117
103
- if (LocalUnrecognized_) {
104
- auto unrecognizedList = LocalUnrecognized_->GetChildren ();
105
- SortBy (unrecognizedList, [] (const auto & item) { return item.first ; });
106
- for (const auto & [key, child] : unrecognizedList) {
107
- consumer->OnKeyedItem (key);
108
- Serialize (child, consumer);
118
+ auto saveRecognized = [&recognizedIt, this ] (auto * consumer) {
119
+ const auto & parameter = recognizedIt->second ;
120
+ if (!parameter->CanOmitValue (this )) {
121
+ consumer->OnKeyedItem (recognizedIt->first );
122
+ parameter->Save (this , consumer);
109
123
}
124
+ ++recognizedIt;
125
+ };
126
+
127
+ auto saveUnrecognized = [&unrecognizedIt] (auto * consumer) {
128
+ consumer->OnKeyedItem (unrecognizedIt->first );
129
+ Serialize (unrecognizedIt->second , consumer);
130
+ ++unrecognizedIt;
131
+ };
132
+
133
+ while (recognizedIt != recognizedList.end () && unrecognizedIt != unrecognizedList.end ()) {
134
+ if (recognizedIt->first < unrecognizedIt->first ) {
135
+ saveRecognized (consumer);
136
+ } else {
137
+ saveUnrecognized (consumer);
138
+ }
139
+ }
140
+
141
+ while (recognizedIt != recognizedList.end ()) {
142
+ saveRecognized (consumer);
143
+ }
144
+
145
+ while (unrecognizedIt != unrecognizedList.end ()) {
146
+ saveUnrecognized (consumer);
110
147
}
111
148
}
112
149
0 commit comments