@@ -64,6 +64,10 @@ impl<'doc> Doc<'doc> {
64
64
reader:: get_doc ( * self , tag)
65
65
}
66
66
67
+ pub fn is_empty ( & self ) -> bool {
68
+ self . start == self . end
69
+ }
70
+
67
71
pub fn as_str_slice < ' a > ( & ' a self ) -> & ' a str {
68
72
str:: from_utf8 ( & self . data [ self . start ..self . end ] ) . unwrap ( )
69
73
}
@@ -462,6 +466,11 @@ pub mod reader {
462
466
}
463
467
464
468
fn _next_sub ( & mut self ) -> DecodeResult < uint > {
469
+ // empty vector/map optimization
470
+ if self . parent . is_empty ( ) {
471
+ return Ok ( 0 ) ;
472
+ }
473
+
465
474
let ( big, doc) = try!( self . next_doc2 ( EsSub8 , EsSub32 ) ) ;
466
475
let r = if big {
467
476
doc_as_u32 ( doc) as uint
@@ -1148,6 +1157,10 @@ pub mod writer {
1148
1157
fn emit_seq < F > ( & mut self , len : uint , f : F ) -> EncodeResult where
1149
1158
F : FnOnce ( & mut Encoder < ' a > ) -> EncodeResult ,
1150
1159
{
1160
+ if len == 0 {
1161
+ // empty vector optimization
1162
+ return self . wr_tagged_bytes ( EsVec as uint , & [ ] ) ;
1163
+ }
1151
1164
1152
1165
try!( self . start_tag ( EsVec as uint ) ) ;
1153
1166
try!( self . _emit_tagged_sub ( len) ) ;
@@ -1167,6 +1180,10 @@ pub mod writer {
1167
1180
fn emit_map < F > ( & mut self , len : uint , f : F ) -> EncodeResult where
1168
1181
F : FnOnce ( & mut Encoder < ' a > ) -> EncodeResult ,
1169
1182
{
1183
+ if len == 0 {
1184
+ // empty map optimization
1185
+ return self . wr_tagged_bytes ( EsMap as uint , & [ ] ) ;
1186
+ }
1170
1187
1171
1188
try!( self . start_tag ( EsMap as uint ) ) ;
1172
1189
try!( self . _emit_tagged_sub ( len) ) ;
0 commit comments