servo: Merge #12896 - Improve Blob.Size() speed by avoid reading content (from izgzhen:fix-blob-size); r=Manishearth
authorZhen Zhang <izgzhen@gmail.com>
Wed, 17 Aug 2016 04:52:42 -0500
changeset 339509 a95e7913c1fcfc17faa0ed79b027f812a369141c
parent 339508 e1e842672ebf6a1ff8f88cd7497ee819f97243bb
child 339510 27e7e5cc53f38a4b9724f422f7db4ac64e52f1ec
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersManishearth
servo: Merge #12896 - Improve Blob.Size() speed by avoid reading content (from izgzhen:fix-blob-size); r=Manishearth r? @Manishearth --- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors Source-Repo: https://github.com/servo/servo Source-Revision: 2eb03287388346517344b90c7d6da09d54da2596
servo/components/script/dom/blob.rs
--- a/servo/components/script/dom/blob.rs
+++ b/servo/components/script/dom/blob.rs
@@ -352,21 +352,22 @@ pub fn blob_parts_to_bytes(blobparts: Ve
     }
 
     Ok(ret)
 }
 
 impl BlobMethods for Blob {
     // https://w3c.github.io/FileAPI/#dfn-size
     fn Size(&self) -> u64 {
-        // XXX: This will incur reading if file-based
-        match self.get_bytes() {
-            Ok(s) => s.len() as u64,
-            _ => 0,
-        }
+         match *self.blob_impl.borrow() {
+            BlobImpl::File(ref f) => f.size,
+            BlobImpl::Memory(ref v) => v.len() as u64,
+            BlobImpl::Sliced(ref parent, ref rel_pos) =>
+                rel_pos.to_abs_range(parent.Size() as usize).len() as u64,
+         }
     }
 
     // https://w3c.github.io/FileAPI/#dfn-type
     fn Type(&self) -> DOMString {
         DOMString::from(self.typeString.clone())
     }
 
     // https://w3c.github.io/FileAPI/#slice-method-algo