Skip to content

Commit 7b6e43c

Browse files
committed
metadata: Space-optimize empty vectors and maps.
So that `EsVec 82 EsSub8 00` becomes `EsVec 80` now.
1 parent 84e9a61 commit 7b6e43c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/librbml/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ impl<'doc> Doc<'doc> {
6464
reader::get_doc(*self, tag)
6565
}
6666

67+
pub fn is_empty(&self) -> bool {
68+
self.start == self.end
69+
}
70+
6771
pub fn as_str_slice<'a>(&'a self) -> &'a str {
6872
str::from_utf8(&self.data[self.start..self.end]).unwrap()
6973
}
@@ -462,6 +466,11 @@ pub mod reader {
462466
}
463467

464468
fn _next_sub(&mut self) -> DecodeResult<uint> {
469+
// empty vector/map optimization
470+
if self.parent.is_empty() {
471+
return Ok(0);
472+
}
473+
465474
let (big, doc) = try!(self.next_doc2(EsSub8, EsSub32));
466475
let r = if big {
467476
doc_as_u32(doc) as uint
@@ -1148,6 +1157,10 @@ pub mod writer {
11481157
fn emit_seq<F>(&mut self, len: uint, f: F) -> EncodeResult where
11491158
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
11501159
{
1160+
if len == 0 {
1161+
// empty vector optimization
1162+
return self.wr_tagged_bytes(EsVec as uint, &[]);
1163+
}
11511164

11521165
try!(self.start_tag(EsVec as uint));
11531166
try!(self._emit_tagged_sub(len));
@@ -1167,6 +1180,10 @@ pub mod writer {
11671180
fn emit_map<F>(&mut self, len: uint, f: F) -> EncodeResult where
11681181
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
11691182
{
1183+
if len == 0 {
1184+
// empty map optimization
1185+
return self.wr_tagged_bytes(EsMap as uint, &[]);
1186+
}
11701187

11711188
try!(self.start_tag(EsMap as uint));
11721189
try!(self._emit_tagged_sub(len));

0 commit comments

Comments
 (0)