OSDN Git Service

LoadAsyncを高速化した
authorkonekoneko <test2214@hotmail.co.jp>
Wed, 8 Jul 2015 18:42:08 +0000 (03:42 +0900)
committerkonekoneko <test2214@hotmail.co.jp>
Wed, 8 Jul 2015 18:42:08 +0000 (03:42 +0900)
Common/Document.cs
Common/StringBuffer.cs
Common/WinFileStream.cs

index 71019f7..671ab83 100644 (file)
@@ -736,6 +736,14 @@ namespace FooEditEngine
         /// ストリームから行を読み取った物を返す。LoadAsyncを呼び出す場合は必ず実装してください\r
         /// </summary>\r
         Task<string> ReadLineAsync();\r
+        /// <summary>\r
+        /// ストリームから指定した文字数だけ読み取る\r
+        /// </summary>\r
+        /// <param name="buffer">書き込み先バッファー</param>\r
+        /// <param name="index">書き込み先バッファーのインデックス</param>\r
+        /// <param name="count">読み取る文字数</param>\r
+        /// <returns>読み取った文字数</returns>\r
+        Task<int> ReadAsync(char[] buffer, int index, int count);\r
     }\r
 \r
     public interface IStreamWriter\r
index dcc97a7..f2d5a82 100644 (file)
@@ -138,21 +138,28 @@ namespace FooEditEngine
 \r
         internal async Task LoadAsync(IStreamReader fs, CancellationTokenSource tokenSource = null)\r
         {\r
-            string str;\r
-            for (int i = 0; (str = await fs.ReadLineAsync().ConfigureAwait(false)) != null; i++)\r
+            char[] str = new char[1024 * 256];\r
+            int readCount;\r
+            do\r
             {\r
                 int index = this.Length;\r
                 if (index < 0)\r
                     index = 0;\r
 \r
-                this.buf.InsertRange(index, str + Document.NewLine, str.Length + 1);\r
+                readCount = await fs.ReadAsync(str, 0, str.Length).ConfigureAwait(false);\r
+                \r
+                //内部形式に変換する\r
+                var internal_str = from s in str where s != '\r' select s;\r
+\r
+                //str.lengthは事前に確保しておくために使用するので影響はない\r
+                this.buf.InsertRange(index, internal_str, str.Length);\r
 \r
                 if (tokenSource != null)\r
                     tokenSource.Token.ThrowIfCancellationRequested();\r
 #if TEST_ASYNC\r
                     System.Threading.Thread.Sleep(10);\r
 #endif\r
-            }\r
+            } while (readCount > 0);\r
             this.Update(this, new DocumentUpdateEventArgs(UpdateType.Clear, -1, -1, -1));\r
             this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count));\r
         }\r
index b5e62c9..cfa5295 100644 (file)
@@ -75,6 +75,12 @@ namespace FooEditEngine
             return this.sr.ReadLineAsync();
         }
 
+        public Task<int> ReadAsync(char[] buffer,int index, int count)
+        {
+            return this.sr.ReadAsync(buffer, index, count);
+        }
+
+
         public void Close()
         {
 #if !METRO