Commit 2a267ddc authored by Joshua Bell's avatar Joshua Bell Committed by Commit Bot

Improve perf of TextDecoder::decode by re-using TextCodec

In the spec[1], a new decoder is created unconditionally on each call
to decode() unless the previous call passed {stream:true}, but in the
real world that requires an allocation of a TextCodec object.

Since the previous TextCodec would be flushed by the previous decode()
call *unless* {stream:true} was passed, then we don't need a new
TextCodec to match the spec behavior.

On a simple test (64k strings, length 16-32, 50 iterations), the
change showed an 11% performance improvement (with a standard
deviation of 1.3% over 10 runs).

[1] https://encoding.spec.whatwg.org/#dom-textdecoder-decode

Bug: 1092582
Change-Id: I815a3e4c2885397aaf24ef790b7470c7ed761ada
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2271061
Commit-Queue: Joshua Bell <jsbell@chromium.org>
Reviewed-by: default avatarAdam Rice <ricea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784233}
parent 880a3c6f
......@@ -112,7 +112,15 @@ String TextDecoder::decode(const char* start,
ExceptionState& exception_state) {
DCHECK(options);
if (!do_not_flush_) {
codec_ = NewTextCodec(encoding_);
if (!codec_) {
// In the spec, a new decoder is created unconditionally here, but that
// requires an extra allocation. Since the TextCodec would be flushed
// here by the previous call if `!do_not_flush` (sorry about the double
// negatives), then we don't need a new TextCodec to match the spec
// behavior.
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
codec_ = NewTextCodec(encoding_);
}
bom_seen_ = false;
}
......@@ -125,6 +133,10 @@ String TextDecoder::decode(const char* start,
String s = codec_->Decode(start, length, flush, fatal_, saw_error);
if (fatal_ && saw_error) {
if (!do_not_flush_) {
// If flushing, the error should not persist.
codec_.reset();
}
exception_state.ThrowTypeError("The encoded data was not valid.");
return String();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment