diff options
author | Florin Malita <fmalita@chromium.org> | 2018-06-14 18:03:26 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-06-15 12:27:04 +0000 |
commit | d7bfcaf57c1fe1d84880e0d31fab9c3d303cdb79 (patch) | |
tree | 298fe73fcf9402a0212ed66e5c3f3b6bb3a05add /modules/skjson/include/SkJSON.h | |
parent | 3f13bcba3e5c8234ddd0c36e7758513bb08a72e4 (diff) |
[skjson] Some short string optimizations
1) skip redundant \0 terminator => Value is always zero-initialized
2) skip storing a len record => strlen is cheap for short strings
Change-Id: I3c10c9b9cf6155b95124e2c0194c59e9531a7ca4
Reviewed-on: https://skia-review.googlesource.com/135049
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Diffstat (limited to 'modules/skjson/include/SkJSON.h')
-rw-r--r-- | modules/skjson/include/SkJSON.h | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/modules/skjson/include/SkJSON.h b/modules/skjson/include/SkJSON.h index 1b46c4a100..8baf9fb8fc 100644 --- a/modules/skjson/include/SkJSON.h +++ b/modules/skjson/include/SkJSON.h @@ -12,6 +12,8 @@ #include "SkTo.h" #include "SkTypes.h" +#include <cstring> + class SkString; class SkWStream; @@ -282,7 +284,11 @@ public: size_t size() const { switch (this->getTag()) { case Tag::kShortString: - return kMaxInlineStringSize - SkToSizeT(this->cast<char>()[kMaxInlineStringSize]); + // We don't bother storing a length for short strings on the assumption + // that strlen is fast in this case. If this becomes problematic, we + // can either go back to storing (7-len) in the tag byte or write a fast + // short_strlen. + return strlen(this->cast<char>()); case Tag::kString: return this->cast<VectorValue<char, Value::Type::kString>>()->size(); default: @@ -297,15 +303,10 @@ public: } const char* end() const { - if (this->getTag() == Tag::kShortString) { - const auto* payload = this->cast<char>(); - return payload + kMaxInlineStringSize - SkToSizeT(payload[kMaxInlineStringSize]); - } - return this->cast<VectorValue<char, Value::Type::kString>>()->end(); + return this->getTag() == Tag::kShortString + ? strchr(this->cast<char>(), '\0') + : this->cast<VectorValue<char, Value::Type::kString>>()->end(); } - -private: - static constexpr size_t kMaxInlineStringSize = sizeof(Value) - 1; }; struct Member { |