perf: cache `PythonVersion` objects (#461)
This commit is contained in:
Родитель
b42401966e
Коммит
08f5b489a3
|
@ -201,7 +201,7 @@ public class PyPiClient : IPyPiClient
|
|||
{
|
||||
try
|
||||
{
|
||||
var parsedVersion = new PythonVersion(release.Key);
|
||||
var parsedVersion = PythonVersion.Create(release.Key);
|
||||
if (release.Value != null && release.Value.Count > 0 &&
|
||||
parsedVersion.Valid && parsedVersion.IsReleasedPackage &&
|
||||
PythonVersionUtilities.VersionValidForSpec(release.Key, spec.DependencySpecifiers))
|
||||
|
|
|
@ -14,9 +14,11 @@ public class PythonVersion : IComparable<PythonVersion>
|
|||
|
||||
private static readonly Dictionary<string, int> PreReleaseMapping = new Dictionary<string, int> { { "a", 0 }, { "alpha", 0 }, { "b", 1 }, { "beta", 1 }, { "c", 2 }, { "rc", 2 }, { "pre", 2 }, { "preview", 2 } };
|
||||
|
||||
private static readonly Dictionary<string, PythonVersion> Cache = new();
|
||||
|
||||
private readonly Match match;
|
||||
|
||||
public PythonVersion(string version)
|
||||
private PythonVersion(string version)
|
||||
{
|
||||
var toOperate = version;
|
||||
if (version.EndsWith(".*"))
|
||||
|
@ -105,6 +107,23 @@ public class PythonVersion : IComparable<PythonVersion>
|
|||
|
||||
public static bool operator <=(PythonVersion operand1, PythonVersion operand2) => operand1.CompareTo(operand2) <= 0;
|
||||
|
||||
public static PythonVersion Create(string version)
|
||||
{
|
||||
if (version == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(version));
|
||||
}
|
||||
|
||||
if (Cache.TryGetValue(version, out var cachedVersion))
|
||||
{
|
||||
return cachedVersion;
|
||||
}
|
||||
|
||||
var pythonVersion = new PythonVersion(version);
|
||||
Cache.Add(version, pythonVersion);
|
||||
return pythonVersion;
|
||||
}
|
||||
|
||||
public int CompareTo(PythonVersion other)
|
||||
{
|
||||
if (other == null || !other.Valid)
|
||||
|
|
|
@ -5,8 +5,8 @@ public class PythonVersionComparer : IComparer<string>
|
|||
{
|
||||
public int Compare(string x, string y)
|
||||
{
|
||||
var xVer = new PythonVersion(x);
|
||||
var yVer = new PythonVersion(y);
|
||||
var xVer = PythonVersion.Create(x);
|
||||
var yVer = PythonVersion.Create(y);
|
||||
|
||||
return xVer.CompareTo(yVer);
|
||||
}
|
||||
|
|
|
@ -95,8 +95,8 @@ public static class PythonVersionUtilities
|
|||
|
||||
var op = spec[..i];
|
||||
|
||||
var targetVer = new PythonVersion(version);
|
||||
var specVer = new PythonVersion(spec[i..]);
|
||||
var targetVer = PythonVersion.Create(version);
|
||||
var specVer = PythonVersion.Create(spec[i..]);
|
||||
|
||||
if (!targetVer.Valid)
|
||||
{
|
||||
|
|
|
@ -14,7 +14,7 @@ public class PythonVersionTests
|
|||
[TestMethod]
|
||||
public void TestBasicVersionConstruction()
|
||||
{
|
||||
var pythonVersion = new PythonVersion("4!3.2.1.1rc2.post99.dev2");
|
||||
var pythonVersion = PythonVersion.Create("4!3.2.1.1rc2.post99.dev2");
|
||||
|
||||
Assert.AreEqual(4, pythonVersion.Epoch);
|
||||
Assert.AreEqual("3.2.1.1", pythonVersion.Release);
|
||||
|
@ -27,7 +27,7 @@ public class PythonVersionTests
|
|||
[TestMethod]
|
||||
public void TestDefaultDevVersionConstruction()
|
||||
{
|
||||
var pythonVersion = new PythonVersion("4!3.2.1.1rc2.post90.dev");
|
||||
var pythonVersion = PythonVersion.Create("4!3.2.1.1rc2.post90.dev");
|
||||
|
||||
Assert.AreEqual(4, pythonVersion.Epoch);
|
||||
Assert.AreEqual("3.2.1.1", pythonVersion.Release);
|
||||
|
@ -69,7 +69,7 @@ public class PythonVersionTests
|
|||
"2.10.0.dev1",
|
||||
"v2.10.0.dev2",
|
||||
"v2.10.0",
|
||||
}.Select(x => new { Raw = x, Version = new PythonVersion(x) }).ToList();
|
||||
}.Select(x => new { Raw = x, Version = PythonVersion.Create(x) }).ToList();
|
||||
|
||||
for (var i = 1; i < versionItems.Count; i++)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче