minor performance tuning;
authorwenzelm
Sat, 15 Jun 2024 20:30:31 +0200
changeset 80372 6e74f0fc8a52
parent 80371 e43944fae5e5
child 80373 dc220d47b85e
minor performance tuning;
src/Pure/General/bytes.scala
--- a/src/Pure/General/bytes.scala	Sat Jun 15 20:29:50 2024 +0200
+++ b/src/Pure/General/bytes.scala	Sat Jun 15 20:30:31 2024 +0200
@@ -168,6 +168,19 @@
         buffer = new ByteArrayOutputStream
       }
 
+    def += (string: String): Unit =
+      if (string.nonEmpty) {
+        if (UTF8.relevant(string)) { this += UTF8.bytes(string) }
+        else {
+          synchronized {
+            for (c <- string) {
+              buffer.write(c.toByte)
+              buffer_check()
+            }
+          }
+        }
+      }
+
     def += (array: Array[Byte], offset: Int, length: Int): Unit = {
       if (offset < 0 || length < 0 || offset.toLong + length.toLong > array.length) {
         throw new IndexOutOfBoundsException
@@ -194,8 +207,6 @@
 
     def += (a: Subarray): Unit = { this += (a.array, a.offset, a.length) }
 
-    def += (string: String): Unit = if (string.nonEmpty) { this += UTF8.bytes(string) }
-
     private def done(): Bytes = synchronized {
       val cs = chunks.toArray
       val b = buffer.toByteArray