Skip to content

Commit

Permalink
Merge pull request #100 from /issues/97
Browse files Browse the repository at this point in the history
Issues/97 Delete IStateClone
  • Loading branch information
mattak authored Sep 4, 2017
2 parents 6fa88df + 7908f01 commit 50b748a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
7 changes: 0 additions & 7 deletions Assets/Plugins/Unidux/Scripts/IStateClone.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Assets/Plugins/Unidux/Scripts/IStateClone.cs.meta

This file was deleted.

7 changes: 4 additions & 3 deletions Assets/Plugins/Unidux/Scripts/StateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
namespace Unidux
{
[Serializable]
public class StateBase : IState, IStateClone, IStateChanged
public class StateBase : IState, IStateChanged, ICloneable
{
public virtual TValue Clone<TValue>() where TValue : IStateClone
public virtual object Clone()
{
return (TValue) StateUtil.MemoryClone(this);
// It's slow. in case of requiring performance override this deep clone method by your code.
return StateUtil.MemoryClone(this);
}

public bool IsStateChanged { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion Assets/Plugins/Unidux/Scripts/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void ForceUpdate()
lock (this._state)
{
// Prevent writing state object
fixedState = this._state.Clone<TState>();
fixedState = (TState) this._state.Clone();

// The function may slow
StateUtil.ResetStateChanged(this._state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using NUnit.Framework;

namespace Unidux.Test
namespace Unidux.Performance
{
public class ClonePerformanceTest
{
Expand All @@ -23,7 +23,7 @@ public void CloneTest()
{
watch.Reset();
watch.Start();
var clone1 = state.Clone<SampleState>();
var clone1 = (SampleState)state.Clone();
watch.Stop();
UnityEngine.Debug.Log("Clone1: " + watch.Elapsed.Milliseconds + "[ms]");
}
Expand All @@ -48,10 +48,10 @@ public static SampleState Create(int loop)
return state;
}

// public override TValue Clone<TValue>()
// custom implementation of Clone is faster than default BinaryFormatter implementation.
// public override object Clone()
// {
// object clonee = CustomClone();
// return (TValue)clonee;
// return CustomClone();
// }

public SampleState CustomClone()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Plugins/Unidux/Test/Editor/StateBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void CloneTest()
var sample1 = new SampleState();
sample1.List = new List<string>() {"a", "b", "c"};

var sample2 = sample1.Clone<SampleState>();
var sample2 = (SampleState)sample1.Clone();
sample2.List[1] = "bb";
Assert.AreEqual(new List<string>() {"a", "b", "c"}, sample1.List);
Assert.AreEqual(new List<string>() {"a", "bb", "c"}, sample2.List);
Expand Down
11 changes: 6 additions & 5 deletions Assets/Plugins/Unidux/Test/Editor/StateCloneTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using NUnit.Framework;
using System;
using NUnit.Framework;
namespace Unidux
{
public class StateCloneTest
{
class Hoge : IStateClone
class Hoge : ICloneable
{
private bool Flag = false;

Expand All @@ -17,9 +18,9 @@ public bool GetFlag()
return this.Flag;
}

public T Clone<T>() where T : IStateClone
public object Clone()
{
return (T) MemberwiseClone();
return MemberwiseClone();
}
}

Expand Down Expand Up @@ -47,7 +48,7 @@ public void CloneTest()
Assert.AreEqual(true, hoge1.GetFlag());
Assert.AreEqual(100, hoge1.GetCount());

var hoge2 = hoge1.Clone<HogeHoge>();
var hoge2 = (HogeHoge)hoge1.Clone();
Assert.AreEqual(true, hoge2.GetFlag());
Assert.AreEqual(100, hoge2.GetCount());
}
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ Foo.Instance

The instance of the base class.

# TIPS

## Performance of object cloning

Default implemention of `StateBase.Clone()` is not fast, because it uses `BinaryFormatter` & `MemoryStream`.
And Unidux creates new State on every State chaning (it affects a few milliseconds).
So in case of requiring performance, override clone method with your own logic.

e.g.

```
[Serializable]
class State : StateBase
{
public override object Clone()
{
// implement your custom deep clone code
}
}
```

# Thanks

- [@austinmao](https://github.com/austinmao) for suggestion of Ducks and UniRx.
Expand Down

0 comments on commit 50b748a

Please sign in to comment.