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