speedup dynamicwstream
- move bytesWritten calculation to query the tail, allowing write() to be faster since it doesn't have to update anything extra per-write.
- enforce that all blocks are multiple-of-4 bytes big
- update the minimum block size to 4K
Before: 30ms
After: 23ms for non-4-bytes writes
13ms for 4-bytes writes
BUG=skia:
Change-Id: Id06ecad3b9fe426747e02accf1393595e3356ce3
Reviewed-on: https://skia-review.googlesource.com/6087
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/core/SkStream.h b/include/core/SkStream.h
index 089a4ca..e6df9ea 100644
--- a/include/core/SkStream.h
+++ b/include/core/SkStream.h
@@ -194,11 +194,19 @@
// helpers
- bool write8(U8CPU);
- bool write16(U16CPU);
- bool write32(uint32_t);
+ bool write8(U8CPU value) {
+ uint8_t v = SkToU8(value);
+ return this->write(&v, 1);
+ }
+ bool write16(U16CPU value) {
+ uint16_t v = SkToU16(value);
+ return this->write(&v, 2);
+ }
+ bool write32(uint32_t v) {
+ return this->write(&v, 4);
+ }
- bool writeText(const char text[]) {
+ bool writeText(const char text[]) {
SkASSERT(text);
return this->write(text, strlen(text));
}
@@ -376,10 +384,11 @@
virtual ~SkDynamicMemoryWStream();
bool write(const void* buffer, size_t size) override;
- size_t bytesWritten() const override { return fBytesWritten; }
-
+ size_t bytesWritten() const override;
bool read(void* buffer, size_t offset, size_t size);
- size_t getOffset() const { return fBytesWritten; }
+
+ // Why do we have this as a separate method???
+ size_t getOffset() const { return this->bytesWritten(); }
// copy what has been written to the stream into dst
void copyTo(void* dst) const;
@@ -398,7 +407,13 @@
struct Block;
Block* fHead;
Block* fTail;
- size_t fBytesWritten;
+ size_t fBytesWrittenBeforeTail;
+
+#ifdef SK_DEBUG
+ void validate() const;
+#else
+ void validate() const {}
+#endif
// For access to the Block type.
friend class SkBlockMemoryStream;