Skip to content

Commit 99629eb

Browse files
committed
Pre-allocate capacity for maps in json! macro.
Attempt to address issue serde-rs#810. This implementation works by expanding the contents of the map twice, first to produce a capacity value, then actually inserting the elements. Because it expands the contents of the map twice, when encountering invalid syntax, it may print error messages twice, which may be unwanted.
1 parent 4f194c9 commit 99629eb

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

src/macros.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,107 @@ macro_rules! json_internal {
234234
json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
235235
};
236236

237+
238+
//////////////////////////////////////////////////////////////////////////
239+
// TT muncher for counting the elements inside of an object {...}.
240+
// Each entry is replaced with `1 + ` (or `1` if it is the last element).
241+
// 0 is inserted if the object has a trailing comma.
242+
//
243+
// Must be invoked as: json_internal!(@object_capacity () ($($tt)*) ($($tt)*))
244+
//
245+
// We require two copies of the input tokens so that we can match on one
246+
// copy and trigger errors on the other copy.
247+
//////////////////////////////////////////////////////////////////////////
248+
249+
// Done.
250+
(@object_capacity () () ()) => {0};
251+
252+
// Current entry followed by trailing comma.
253+
(@object_capacity entry , $($rest:tt)*) => {
254+
1 + json_internal!(@object_capacity () ($($rest)*) ($($rest)*))
255+
};
256+
257+
// Current entry followed by unexpected token. The actual parsing macro will print the error message.
258+
(@object_capacity entry $unexpected:tt $($rest:tt)*) => {
259+
0
260+
};
261+
262+
// Insert the last entry without trailing comma.
263+
(@object_capacity entry) => {
264+
1
265+
};
266+
267+
// Next value is `null`.
268+
(@object_capacity ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
269+
json_internal!(@object_capacity entry $($rest)*)
270+
};
271+
272+
// Next value is `true`.
273+
(@object_capacity ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
274+
json_internal!(@object_capacity entry $($rest)*)
275+
};
276+
277+
// Next value is `false`.
278+
(@object_capacity ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
279+
json_internal!(@object_capacity entry $($rest)*)
280+
};
281+
282+
// Next value is an array.
283+
(@object_capacity ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
284+
json_internal!(@object_capacity entry $($rest)*)
285+
};
286+
287+
// Next value is a map.
288+
(@object_capacity ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
289+
json_internal!(@object_capacity entry $($rest)*)
290+
};
291+
292+
// Next value is an expression followed by comma.
293+
(@object_capacity ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
294+
json_internal!(@object_capacity entry , $($rest)*)
295+
};
296+
297+
// Last value is an expression with no trailing comma.
298+
(@object_capacity ($($key:tt)+) (: $value:expr) $copy:tt) => {
299+
json_internal!(@object_capacity entry)
300+
};
301+
302+
// Missing value for last entry. The actual parsing macro will print the error message.
303+
(@object_capacity ($($key:tt)+) (:) $copy:tt) => {
304+
0
305+
};
306+
307+
// Missing colon and value for last entry. The actual parsing macro will print the error message.
308+
(@object_capacity ($($key:tt)+) () $copy:tt) => {
309+
0
310+
};
311+
312+
// Misplaced colon. The actual parsing macro will print the error message.
313+
(@object_capacity () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
314+
0
315+
};
316+
317+
// Found a comma inside a key. The actual parsing macro will print the error message.
318+
(@object_capacity ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
319+
0
320+
};
321+
322+
// Key is fully parenthesized. This is not necessary for counting capacity
323+
// since we don't evaluate $key anyway, so just use the munching below.
324+
// (@object_capacity () (($key:expr) : $($rest:tt)*) $copy:tt) => {
325+
// json_internal!(@object_capacity ($key) (: $($rest)*) (: $($rest)*))
326+
// };
327+
328+
// Refuse to absorb colon token into key expression.
329+
(@object_capacity ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
330+
json_expect_expr_comma!($($unexpected)+)
331+
};
332+
333+
// Munch a token into the current key.
334+
(@object_capacity ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
335+
json_internal!(@object_capacity ($($key)* $tt) ($($rest)*) ($($rest)*))
336+
};
337+
237338
//////////////////////////////////////////////////////////////////////////
238339
// The main implementation.
239340
//
@@ -266,7 +367,8 @@ macro_rules! json_internal {
266367

267368
({ $($tt:tt)+ }) => {
268369
$crate::Value::Object({
269-
let mut object = $crate::Map::new();
370+
let capacity = json_internal!(@object_capacity () ($($tt)+) ($($tt)+));
371+
let mut object = $crate::Map::with_capacity(capacity);
270372
json_internal!(@object object () ($($tt)+) ($($tt)+));
271373
object
272374
})

0 commit comments

Comments
 (0)