Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ workflow run on the [GitHub Actions][url-actions] tab of the repository.
./gradlew jvmBenchmark
```

- Run Js:
```shell
./gradlew jsBenchmark
```

- Run WasmJs:
```shell
./gradlew wasmJsBenchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
public fun ByteArray.encodeToString(encoder: Encoder<*>): String {
return encoder.encodeOutMaxSizeOrFail(size) { maxSize ->
val sb = StringBuilder(maxSize)
encoder.encode(this, sb::append)
encoder.encode(this, _outFeed = { OutFeed(sb::append) })
val result = sb.toString()
if (encoder.config.backFillBuffers) {
sb.wipe()
Expand All @@ -248,7 +248,7 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
return encoder.encodeOutMaxSizeOrFail(size) block@ { maxSize ->
var i = 0
val a = CharArray(maxSize)
encoder.encode(this) { c -> a[i++] = c }
encoder.encode(this, _outFeed = { OutFeed { c -> a[i++] = c } })
if (i == maxSize) return@block a
val copy = a.copyOf(i)
if (encoder.config.backFillBuffers) {
Expand All @@ -274,7 +274,7 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
return encoder.encodeOutMaxSizeOrFail(size) block@ { maxSize ->
var i = 0
val a = ByteArray(maxSize)
encoder.encode(this) { char -> a[i++] = char.code.toByte() }
encoder.encode(this, _outFeed = { OutFeed { char -> a[i++] = char.code.toByte() } })
if (i == maxSize) return@block a
val copy = a.copyOf(i)
if (encoder.config.backFillBuffers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,6 @@ private inline fun checkMaxEmitSize(size: Int, parameterName: () -> String) {
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun negativeEncodingSizeException(outSize: Number): EncodingSizeException {
private inline fun negativeEncodingSizeException(outSize: Number): EncodingSizeException {
return EncodingSizeException("Calculated output of Size[$outSize] was negative")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("LocalVariableName", "NOTHING_TO_INLINE")
@file:Suppress("LocalVariableName")

package io.matthewnelson.encoding.core.internal

Expand All @@ -37,19 +37,20 @@ internal inline fun <T: Any> Encoder<*>.encodeOutMaxSizeOrFail(
_block: (maxSize: Int) -> T,
): T {
contract { callsInPlace(_block, InvocationKind.AT_MOST_ONCE) }

val maxSize = config.encodeOutMaxSize(size.toLong())
if (maxSize > MAX_ENCODE_OUT_SIZE) {
throw Config.outSizeExceedsMaxEncodingSizeException(maxSize, MAX_ENCODE_OUT_SIZE)
}

return _block(maxSize.toInt())
}

@OptIn(ExperimentalContracts::class)
internal inline fun <C: Config> Encoder<C>.encode(
data: ByteArray,
out: Encoder.OutFeed,
_outFeed: () -> Encoder.OutFeed,
) {
contract { callsInPlace(_outFeed, InvocationKind.AT_MOST_ONCE) }
if (data.isEmpty()) return
val out = _outFeed()
newEncoderFeed(out).use { feed -> data.forEach { b -> feed.consume(b) } }
}