MCPcopy Create free account
hub / github.com/apache/fory / write_map_data_fast

Function write_map_data_fast

cpp/fory/serialization/map_serializer.h:125–205  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

123/// This is the optimized fast path for common cases like map<string, int>
124template <typename K, typename V, typename MapType>
125inline void write_map_data_fast(const MapType &map, WriteContext &ctx,
126 bool has_generics) {
127 static_assert(!is_polymorphic_v<K> && !is_polymorphic_v<V>,
128 "Fast path is for non-polymorphic types only");
129 static_assert(!is_shared_ref_v<K> && !is_shared_ref_v<V>,
130 "Fast path is for non-shared-ref types only");
131
132 // write total length
133 ctx.write_var_uint32(static_cast<uint32_t>(map.size()));
134
135 if (map.empty()) {
136 return;
137 }
138
139 // Determine if keys/values are declared types (no type info needed)
140 const bool is_key_declared =
141 has_generics && !need_to_write_type_for_field<K>();
142 const bool is_val_declared =
143 has_generics && !need_to_write_type_for_field<V>();
144
145 // State for chunked writing
146 size_t header_offset = 0;
147 uint8_t pair_counter = 0;
148 bool need_write_header = true;
149
150 for (const auto &[key, value] : map) {
151 // For fast path, we assume no null values (primitives/strings)
152 // If nullability is needed, use the slow path
153
154 if (need_write_header) {
155 ctx.enter_flush_barrier();
156 // reserve space for header (1 byte) + chunk size (1 byte)
157 header_offset = ctx.buffer().writer_index();
158 ctx.write_uint16(0); // Placeholder for header and chunk size
159 uint8_t chunk_header = 0;
160 if (is_key_declared) {
161 chunk_header |= DECL_KEY_TYPE;
162 } else {
163 write_type_info<K>(ctx);
164 }
165 if (is_val_declared) {
166 chunk_header |= DECL_VALUE_TYPE;
167 } else {
168 write_type_info<V>(ctx);
169 }
170
171 // write chunk header at reserved position
172 ctx.buffer().unsafe_put_byte(header_offset, chunk_header);
173 need_write_header = false;
174 }
175
176 // write key and value data
177 if (has_generics && is_generic_type_v<K>) {
178 Serializer<K>::write_data_generic(key, ctx, true);
179 } else {
180 Serializer<K>::write_data(key, ctx);
181 }
182

Callers

nothing calls this directly

Calls 12

write_dataFunction · 0.85
writer_indexMethod · 0.80
unsafe_put_byteMethod · 0.80
write_chunk_sizeFunction · 0.70
write_var_uint32Method · 0.45
sizeMethod · 0.45
emptyMethod · 0.45
enter_flush_barrierMethod · 0.45
bufferMethod · 0.45
write_uint16Method · 0.45
exit_flush_barrierMethod · 0.45
try_flushMethod · 0.45

Tested by

no test coverage detected