Permalink
Browse files

Optimize Observable.Return(int)

  • Loading branch information...
1 parent 26a7e81 commit d10db055647d50ab352ccdf75c6ad90eed22312d @neuecc committed Sep 30, 2016
@@ -163,6 +163,14 @@ public static IObservable<bool> Return(bool value)
}
/// <summary>
+ /// Return single sequence Immediately, optimized for Int32.
+ /// </summary>
+ public static IObservable<Int32> Return(int value)
+ {
+ return ImmutableReturnInt32Observable.GetInt32Observable(value);
+ }
+
+ /// <summary>
/// Same as Observable.Return(Unit.Default); but no allocate memory.
/// </summary>
public static IObservable<Unit> ReturnUnit()
@@ -155,4 +155,51 @@ public IDisposable Subscribe(IObserver<bool> observer)
return Disposable.Empty;
}
}
+
+ internal class ImmutableReturnInt32Observable : IObservable<int>, IOptimizedObservable<int>
+ {
+ static ImmutableReturnInt32Observable[] Caches = new ImmutableReturnInt32Observable[]
+ {
+ new ImmutableReturnInt32Observable(-1),
+ new ImmutableReturnInt32Observable(0),
+ new ImmutableReturnInt32Observable(1),
+ new ImmutableReturnInt32Observable(2),
+ new ImmutableReturnInt32Observable(3),
+ new ImmutableReturnInt32Observable(4),
+ new ImmutableReturnInt32Observable(5),
+ new ImmutableReturnInt32Observable(6),
+ new ImmutableReturnInt32Observable(7),
+ new ImmutableReturnInt32Observable(8),
+ new ImmutableReturnInt32Observable(9),
+ };
+
+ public static IObservable<int> GetInt32Observable(int x)
+ {
+ if (-1 <= x && x <= 9)
+ {
+ return Caches[x + 1];
+ }
+
+ return new ImmediateReturnObservable<int>(x);
+ }
+
+ readonly int x;
+
+ ImmutableReturnInt32Observable(int x)
+ {
+ this.x = x;
+ }
+
+ public bool IsRequiredSubscribeOnCurrentThread()
+ {
+ return false;
+ }
+
+ public IDisposable Subscribe(IObserver<int> observer)
+ {
+ observer.OnNext(x);
+ observer.OnCompleted();
+ return Disposable.Empty;
+ }
+ }
}
@@ -32,4 +32,4 @@
[assembly: AssemblyVersion("5.4.1.0")]
[assembly: AssemblyFileVersion("5.4.1.0")]
-[assembly: InternalsVisibleTo("UniRx.Unity")]
+[assembly: InternalsVisibleTo("UniRx.Tests")]
@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UniRx</RootNamespace>
<AssemblyName>UniRx</AssemblyName>
- <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
@@ -103,5 +103,23 @@ public void Throw()
var ex = new Exception();
Observable.Throw<string>(ex).Materialize().ToArray().Wait().IsCollection(Notification.CreateOnError<string>(ex));
}
+
+ [TestMethod]
+ public void OptimizeReturnTest()
+ {
+ for (int i = -1; i <= 9; i++)
+ {
+ var r = Observable.Return(i);
+ var xs = r.Record();
+ xs.Values[0].Is(i);
+ r.GetType().FullName.Contains("ImmutableReturnInt32Observable").IsTrue();
+ }
+ foreach (var i in new[] { -2, 10, 100 })
+ {
+ var r = Observable.Return(i);
+ r.Record().Values[0].Is(i);
+ r.GetType().FullName.Contains("ImmediateReturnObservable").IsTrue();
+ }
+ }
}
}

0 comments on commit d10db05

Please sign in to comment.