optimize: parallel tasks
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0.524" package="org.tsanie.pixiview" android:versionCode="15">
 | 
			
		||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0.524" package="org.tsanie.pixiview" android:versionCode="16">
 | 
			
		||||
	<uses-sdk android:minSdkVersion="25" android:targetSdkVersion="28" />
 | 
			
		||||
	<application android:label="Pixiview" android:icon="@mipmap/icon" android:roundIcon="@mipmap/icon_round"></application>
 | 
			
		||||
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 | 
			
		||||
 
 | 
			
		||||
@@ -31,6 +31,6 @@
 | 
			
		||||
	<key>CFBundleShortVersionString</key>
 | 
			
		||||
	<string>1.0.524</string>
 | 
			
		||||
	<key>CFBundleVersion</key>
 | 
			
		||||
	<string>15</string>
 | 
			
		||||
	<string>16</string>
 | 
			
		||||
</dict>
 | 
			
		||||
</plist>
 | 
			
		||||
 
 | 
			
		||||
@@ -81,7 +81,7 @@
 | 
			
		||||
	<key>CFBundleShortVersionString</key>
 | 
			
		||||
	<string>1.0.524</string>
 | 
			
		||||
	<key>CFBundleVersion</key>
 | 
			
		||||
	<string>15</string>
 | 
			
		||||
	<string>16</string>
 | 
			
		||||
	<key>CFBundleDevelopmentRegion</key>
 | 
			
		||||
	<string>China</string>
 | 
			
		||||
</dict>
 | 
			
		||||
 
 | 
			
		||||
@@ -87,8 +87,9 @@ namespace Pixiview.Illust
 | 
			
		||||
            base.OnUnload();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustItem[] data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustItem[] data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = startIndex;
 | 
			
		||||
            return data;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -431,7 +432,8 @@ namespace Pixiview.Illust
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                return true;
 | 
			
		||||
            }, () =>
 | 
			
		||||
            },
 | 
			
		||||
            complete: () =>
 | 
			
		||||
            {
 | 
			
		||||
                Stores.SaveFavoritesIllusts();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -77,13 +77,16 @@ namespace Pixiview.Illust
 | 
			
		||||
        protected double topOffset;
 | 
			
		||||
        protected string lastError;
 | 
			
		||||
 | 
			
		||||
        private readonly object sync = new object();
 | 
			
		||||
        private readonly Queue<ParallelTask> tasks;
 | 
			
		||||
        private T illustData;
 | 
			
		||||
        private ParallelTask task;
 | 
			
		||||
 | 
			
		||||
        public IllustCollectionPage()
 | 
			
		||||
        {
 | 
			
		||||
            commandIllustImageTapped = new Command<IllustItem>(OnIllustImageTapped);
 | 
			
		||||
            commandUserTapped = new Command<IIllustItem>(OnIllustUserItemTapped);
 | 
			
		||||
            tasks = new Queue<ParallelTask>();
 | 
			
		||||
 | 
			
		||||
            BindingContext = this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -117,10 +120,15 @@ namespace Pixiview.Illust
 | 
			
		||||
 | 
			
		||||
        public override void OnUnload()
 | 
			
		||||
        {
 | 
			
		||||
            if (task != null)
 | 
			
		||||
            lock (sync)
 | 
			
		||||
            {
 | 
			
		||||
                task.Dispose();
 | 
			
		||||
                task = null;
 | 
			
		||||
                while (tasks.TryDequeue(out var task))
 | 
			
		||||
                {
 | 
			
		||||
                    if (task != null)
 | 
			
		||||
                    {
 | 
			
		||||
                        task.Dispose();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            InvalidateCollection();
 | 
			
		||||
            Illusts = null;
 | 
			
		||||
@@ -195,7 +203,7 @@ namespace Pixiview.Illust
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        protected abstract T DoLoadIllustData(bool force);
 | 
			
		||||
        protected abstract IEnumerable<IllustItem> DoGetIllustList(T data);
 | 
			
		||||
        protected abstract IEnumerable<IllustItem> DoGetIllustList(T data, out int tag);
 | 
			
		||||
 | 
			
		||||
        protected virtual IllustCollection GetIllustsLoadedCollection(IllustCollection collection, bool bottom)
 | 
			
		||||
        {
 | 
			
		||||
@@ -614,7 +622,7 @@ namespace Pixiview.Illust
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var r18 = Configs.IsOnR18;
 | 
			
		||||
            var data = DoGetIllustList(illustData).Where(i => i != null && (r18 || !i.IsRestrict));
 | 
			
		||||
            var data = DoGetIllustList(illustData, out int tag).Where(i => i != null && (r18 || !i.IsRestrict));
 | 
			
		||||
 | 
			
		||||
            var collection = new IllustCollection(data);
 | 
			
		||||
            var favorites = Stores.Favorites;
 | 
			
		||||
@@ -655,19 +663,31 @@ namespace Pixiview.Illust
 | 
			
		||||
            }
 | 
			
		||||
            DoIllustsLoaded(collection, bottom);
 | 
			
		||||
 | 
			
		||||
            DoLoadImages(collection);
 | 
			
		||||
            DoLoadImages(collection, tag);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void DoLoadImages(IllustCollection collection)
 | 
			
		||||
        private void DoLoadImages(IllustCollection collection, int tag)
 | 
			
		||||
        {
 | 
			
		||||
            if (task != null)
 | 
			
		||||
            lock (sync)
 | 
			
		||||
            {
 | 
			
		||||
                task.Dispose();
 | 
			
		||||
                task = null;
 | 
			
		||||
                if (tasks.TryPeek(out var peek))
 | 
			
		||||
                {
 | 
			
		||||
                    if (peek.TagIndex >= tag)
 | 
			
		||||
                    {
 | 
			
		||||
                        App.DebugPrint($"tasks expired ({tasks.Count}), peek: {peek.TagIndex}, now: {tag}, will be disposing.");
 | 
			
		||||
                        while (tasks.TryPeek(out var t))
 | 
			
		||||
                        {
 | 
			
		||||
                            if (t != null)
 | 
			
		||||
                            {
 | 
			
		||||
                                t.Dispose();
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            var list = collection.Where(i => i.Image == null || i.ProfileImage == null).ToArray();
 | 
			
		||||
 | 
			
		||||
            task = ParallelTask.Start("collection.load", 0, list.Length, Configs.MaxThreads, i =>
 | 
			
		||||
            var task = ParallelTask.Start("collection.load", 0, list.Length, Configs.MaxThreads, i =>
 | 
			
		||||
            {
 | 
			
		||||
                if (!collection.Running)
 | 
			
		||||
                {
 | 
			
		||||
@@ -695,7 +715,12 @@ namespace Pixiview.Illust
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                return true;
 | 
			
		||||
            });
 | 
			
		||||
            }, tagIndex: tag);
 | 
			
		||||
 | 
			
		||||
            lock (sync)
 | 
			
		||||
            {
 | 
			
		||||
                tasks.Enqueue(task);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #endregion
 | 
			
		||||
 
 | 
			
		||||
@@ -57,8 +57,9 @@ namespace Pixiview.Illust
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = 0;
 | 
			
		||||
            if (data.body == null)
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -162,8 +162,9 @@ namespace Pixiview.Illust
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustRankingData data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustRankingData data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = currentPage;
 | 
			
		||||
            if (lastQueryKey != null && lastQueryKey.StartsWith(segmentDates[3]))
 | 
			
		||||
            {
 | 
			
		||||
                return data.contents.Where(i => i.illust_type == "0").Select(i => i.ConvertToItem());
 | 
			
		||||
 
 | 
			
		||||
@@ -198,8 +198,9 @@ namespace Pixiview.Illust
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = 0;
 | 
			
		||||
            if (data.body == null)
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -39,8 +39,9 @@ namespace Pixiview.Illust
 | 
			
		||||
        protected override bool IsAutoReload => false;
 | 
			
		||||
        protected override ActivityIndicator LoadingIndicator => activityLoading;
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustRecommendsData data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustRecommendsData data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = startIndex;
 | 
			
		||||
            if (data.body == null)
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -51,8 +51,9 @@ namespace Pixiview.Illust
 | 
			
		||||
        protected override bool IsAutoReload => false;
 | 
			
		||||
        protected override ActivityIndicator LoadingIndicator => activityLoading;
 | 
			
		||||
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustUserData data)
 | 
			
		||||
        protected override IEnumerable<IllustItem> DoGetIllustList(IllustUserData data, out int tag)
 | 
			
		||||
        {
 | 
			
		||||
            tag = startIndex;
 | 
			
		||||
            if (data.body == null)
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -96,7 +96,7 @@ namespace Pixiview.Utils
 | 
			
		||||
 | 
			
		||||
    public class ParallelTask : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        public static ParallelTask Start(string tag, int from, int toExclusive, int maxCount, Predicate<int> action, Action complete = null)
 | 
			
		||||
        public static ParallelTask Start(string tag, int from, int toExclusive, int maxCount, Predicate<int> action, int tagIndex = -1, Action complete = null)
 | 
			
		||||
        {
 | 
			
		||||
            if (toExclusive <= from)
 | 
			
		||||
            {
 | 
			
		||||
@@ -106,7 +106,7 @@ namespace Pixiview.Utils
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            var task = new ParallelTask(tag, from, toExclusive, maxCount, action, complete);
 | 
			
		||||
            var task = new ParallelTask(tag, from, toExclusive, maxCount, action, tagIndex, complete);
 | 
			
		||||
            return task.Start();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -114,6 +114,7 @@ namespace Pixiview.Utils
 | 
			
		||||
        private int count;
 | 
			
		||||
        private bool disposed;
 | 
			
		||||
 | 
			
		||||
        public int TagIndex { get; private set; }
 | 
			
		||||
        private readonly string tag;
 | 
			
		||||
        private readonly int max;
 | 
			
		||||
        private readonly int from;
 | 
			
		||||
@@ -121,7 +122,7 @@ namespace Pixiview.Utils
 | 
			
		||||
        private readonly Predicate<int> action;
 | 
			
		||||
        private readonly Action complete;
 | 
			
		||||
 | 
			
		||||
        private ParallelTask(string tag, int from, int to, int maxCount, Predicate<int> action, Action complete)
 | 
			
		||||
        private ParallelTask(string tag, int from, int to, int maxCount, Predicate<int> action, int tagIndex, Action complete)
 | 
			
		||||
        {
 | 
			
		||||
            if (maxCount <= 0)
 | 
			
		||||
            {
 | 
			
		||||
@@ -132,6 +133,7 @@ namespace Pixiview.Utils
 | 
			
		||||
            {
 | 
			
		||||
                throw new ArgumentOutOfRangeException(nameof(from));
 | 
			
		||||
            }
 | 
			
		||||
            TagIndex = tagIndex;
 | 
			
		||||
            this.tag = tag;
 | 
			
		||||
            this.from = from;
 | 
			
		||||
            this.to = to;
 | 
			
		||||
 
 | 
			
		||||
@@ -721,7 +721,7 @@ namespace Pixiview.Utils
 | 
			
		||||
        public const string FavoriteTypeKey = "favorite_type";
 | 
			
		||||
 | 
			
		||||
        public const int MaxPageThreads = 3;
 | 
			
		||||
        public const int MaxThreads = 4;
 | 
			
		||||
        public const int MaxThreads = 8;
 | 
			
		||||
        public const string Referer = "https://www.pixiv.net/";
 | 
			
		||||
        public const string RefererIllust = "https://www.pixiv.net/artworks/{0}";
 | 
			
		||||
        public const string RefererIllustRanking = "https://www.pixiv.net/ranking.php?{0}";
 | 
			
		||||
 
 | 
			
		||||
@@ -294,7 +294,7 @@ namespace Pixiview.Utils
 | 
			
		||||
                    downloadTask.Dispose();
 | 
			
		||||
                    downloadTask = null;
 | 
			
		||||
                }
 | 
			
		||||
                downloadTask = ParallelTask.Start("ugoira.download", 0, inSegs.Count, 2, i =>
 | 
			
		||||
                downloadTask = ParallelTask.Start("ugoira.download", 0, inSegs.Count, 3, i =>
 | 
			
		||||
                {
 | 
			
		||||
                    var seg = inSegs[i];
 | 
			
		||||
#if DEBUG
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user