From 46b0fd9cecb840865ed08dd4b5e65574b87dc3c4 Mon Sep 17 00:00:00 2001 From: rafikamal Date: Fri, 7 Feb 2020 14:16:55 -0800 Subject: [PATCH] Project import generated by Copybara PiperOrigin-RevId: 293893031 --- .../runtime/kernel/lazy_accessor.js | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/js/experimental/runtime/kernel/lazy_accessor.js b/js/experimental/runtime/kernel/lazy_accessor.js index cebf0c0bd..ce6a67398 100644 --- a/js/experimental/runtime/kernel/lazy_accessor.js +++ b/js/experimental/runtime/kernel/lazy_accessor.js @@ -1482,18 +1482,35 @@ class LazyAccessor { * @private */ getRepeatedMessageArray_(fieldNumber, instanceCreator, pivot) { + // This method can be shortened using getFieldWithDefault and + // getRepeatedNonPrimitive methods. But that will require creating and + // passing a reader closure every time getRepeatedMessageArray_ is called, + // which is expensive. checkInstanceCreator(instanceCreator); - const bytesInstanceCreator = (bufferDecoder) => - instanceCreator(LazyAccessor.fromBufferDecoder_(bufferDecoder, pivot)); - const readMessageFunc = (bufferDecoder, start) => - bytesInstanceCreator(reader.readDelimited(bufferDecoder, start)); + checkFieldNumber(fieldNumber); - const readRepeatedMessageFunc = (indexArray, bufferDecoder) => - readRepeatedNonPrimitive(indexArray, bufferDecoder, readMessageFunc); - const encoder = (writer, fieldNumber, values) => - writeRepeatedMessage(writer, fieldNumber, values); - return this.getFieldWithDefault_( - fieldNumber, /* defaultValue= */[], readRepeatedMessageFunc, encoder); + const field = this.fields_.get(fieldNumber); + if (field === undefined) { + return []; + } + + if (field.hasDecodedValue()) { + return field.getDecodedValue(); + } + + const indexArray = checkDefAndNotNull(field.getIndexArray()); + const result = new Array(indexArray.length); + for (let i = 0; i < indexArray.length; i++) { + validateWireType(indexArray[i], WireType.DELIMITED); + const subMessageBuffer = reader.readDelimited( + checkDefAndNotNull(this.bufferDecoder_), + Field.getStartIndex(indexArray[i])); + result[i] = instanceCreator( + LazyAccessor.fromBufferDecoder_(subMessageBuffer, pivot)); + } + field.setCache(result, writeRepeatedMessage); + + return result; } /**