OSDN Git Service

ファイルロードを高速化した
authortest <test@yahoo.co.jp>
Tue, 5 Jan 2021 12:31:15 +0000 (21:31 +0900)
committertest <test@yahoo.co.jp>
Tue, 5 Jan 2021 12:31:15 +0000 (21:31 +0900)
Core/Document.cs
Core/GapBuffer.cs
Core/StringBuffer.cs

index 67fcf1f..da7b6b0 100644 (file)
@@ -1124,12 +1124,13 @@ namespace FooEditEngine
         /// </summary>
         /// <param name="fs">IStreamReaderオブジェクト</param>
         /// <param name="tokenSource">キャンセルトークン</param>
+        /// <param name="file_size">ファイルサイズ。-1を指定しても動作しますが、読み取りが遅くなります</param>
         /// <returns>Taskオブジェクト</returns>
         /// <remarks>
         /// 読み取り操作は別スレッドで行われます。
         /// また、非同期操作中はこのメソッドを実行することはできません。
         /// </remarks>
-        public async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null)
+        public async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null, int file_size = -1)
         {
             if (fs.Peek() == -1)
                 return;
@@ -1140,6 +1141,8 @@ namespace FooEditEngine
             try
             {
                 this.Clear();
+                if (file_size > 0)
+                    this.buffer.Allocate(file_size);
                 await this.buffer.LoadAsync(fs, tokenSource);
             }
             finally
index e40b7c1..22941ce 100644 (file)
@@ -385,6 +385,18 @@ namespace Slusser.Collections.Generic
                }
 
 
+        /// <summary>
+        /// Allocate buffer
+        /// </summary>
+        /// <param name="count">more than zero</param>
+        public void Allocate(int count)
+        {
+            if(count > this._gapEnd - this._gapStart)
+            {
+                Capacity = count + count;
+            }
+        }
+
                /// <summary>
                /// Searches for the specified object and returns the zero-based index of the first 
                /// occurrence within the <see cref="GapBuffer{T}"/>.
index b1a03c9..175f516 100644 (file)
@@ -83,6 +83,11 @@ namespace FooEditEngine
 
         internal DocumentUpdateEventHandler Update;
 
+        internal void Allocate(int count)
+        {
+            this.buf.Allocate(count);
+        }
+
         internal void Replace(StringBuffer buf)
         {
             this.Replace(buf.buf);
@@ -112,14 +117,10 @@ namespace FooEditEngine
 
         internal async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null)
         {
-            char[] str = new char[1024 * 256];
+            char[] str = new char[1024 * 1024];
             int readCount;
             do
             {
-                int index = this.Length;
-                if (index < 0)
-                    index = 0;
-
                 readCount = await fs.ReadAsync(str, 0, str.Length).ConfigureAwait(false);
 
                 //内部形式に変換する
@@ -128,7 +129,7 @@ namespace FooEditEngine
                 using (await this.rwlock.WriterLockAsync())
                 {
                     //str.lengthは事前に確保しておくために使用するので影響はない
-                    this.buf.InsertRange(index, internal_str);
+                    this.buf.AddRange(internal_str);
                 }
 
                 if (tokenSource != null)