Skip to content

Commit

Permalink
Various optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
pampersrocker committed Feb 7, 2017
1 parent f5434a4 commit 3ce7043
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
16 changes: 15 additions & 1 deletion EvoNet/Configuration/GameConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class GameConfig

public float ScaleFactor { get; set; }

public long GraphCount { get; set; }


public static GameConfig DefaultConfig
{
Expand All @@ -30,6 +32,7 @@ public static GameConfig DefaultConfig
config.MoveRightKeys = new List<Keys> { Keys.D, Keys.Right };
config.MovementSensitivity = 100.0f;
config.ScaleFactor = 0.1f;
config.GraphCount = 10000;
return config;
}
}
Expand All @@ -42,12 +45,19 @@ public static GameConfig LoadConfigOrDefault()
Deserializer yamlDeserializer = new Deserializer();
GameConfig deserialized = yamlDeserializer.Deserialize<GameConfig>(sourceText);
CheckLoadedConfig(deserialized);
SerializerBuilder builder = new SerializerBuilder();
builder.EmitDefaults();
Serializer yamlSerializer = builder.Build();
string serialized = yamlSerializer.Serialize(deserialized);
File.WriteAllText("Config.cfg", serialized);
return deserialized;
}
else
{
// Write out default config if there is no config for easier adjustment
Serializer yamlSerializer = new Serializer();
SerializerBuilder builder = new SerializerBuilder();
builder.EmitDefaults();
Serializer yamlSerializer = builder.Build();
string serialized = yamlSerializer.Serialize(DefaultConfig);
File.WriteAllText("Config.cfg", serialized);
return DefaultConfig;
Expand All @@ -56,6 +66,10 @@ public static GameConfig LoadConfigOrDefault()

public static void CheckLoadedConfig(GameConfig config)
{
if (config.GraphCount == 0)
{
config.GraphCount = 10000;
}
}
}
}
46 changes: 26 additions & 20 deletions EvoNet/Controls/GraphControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void CreateCache(IGraphValueList graph)
GraphCache cache;
int CurrentVertexIndex = 0;
int graphCount = graph.Count;
int startIndex = Math.Max(graph.Count - 10000, 0);
int startIndex = 0;
int elementCount = graphCount - startIndex;
if (elementCount <= 0)
{
Expand All @@ -124,7 +124,7 @@ private void CreateCache(IGraphValueList graph)
decimal maxX = graph[startIndex].DisplayPosition;
decimal minX = graph[startIndex].DisplayPosition;

for(int elementIndex = startIndex; elementIndex < graphCount; elementIndex++)
for (int elementIndex = startIndex; elementIndex < graphCount; elementIndex++)
{
maxY = Math.Max(maxY, graph[elementIndex].DisplayValue);
minY = Math.Min(minY, graph[elementIndex].DisplayValue);
Expand Down Expand Up @@ -152,8 +152,8 @@ private void CreateCache(IGraphValueList graph)
upperPoint.Position = new Vector2(-1, GetRelativeY(graph.ElementAt(0).DisplayValue));
lowerPoint.Position = new Vector2(-1, -1);

VertexPosition2[] vertexAreaBufferData = new VertexPosition2[((graphCount - startIndex)+1) * 2];
VertexPosition2[] vertexLineBufferData = new VertexPosition2[((graphCount - startIndex)+1) * 2];
VertexPosition2[] vertexAreaBufferData = new VertexPosition2[((graphCount - startIndex) + 1) * 2];
VertexPosition2[] vertexLineBufferData = new VertexPosition2[((graphCount - startIndex) + 1) * 2];
int[] indexBufferData = new int[((graphCount - startIndex)) * 6];
int CurrentIndexBufferIndex = 0;

Expand Down Expand Up @@ -232,14 +232,18 @@ private void CreateCache(IGraphValueList graph)

cache.NumElements = graphCount;
GraphCache oldCache;
if (graphCaches.TryGetValue(graph, out oldCache))
lock (this)
{
graphCaches.Remove(graph);
oldCache.IndexAreaBuffer?.Dispose();
oldCache.VertexAreaBuffer?.Dispose();
oldCache.VertexLineBuffer?.Dispose();
if (graphCaches.TryGetValue(graph, out oldCache))
{
graphCaches.Remove(graph);
oldCache.IndexAreaBuffer?.Dispose();
oldCache.VertexAreaBuffer?.Dispose();
oldCache.VertexLineBuffer?.Dispose();
}
graphCaches[graph] = cache;
}
graphCaches[graph] = cache;

}

private void DrawGraph(IGraphValueList graph)
Expand All @@ -252,13 +256,13 @@ private void DrawGraph(IGraphValueList graph)
GraphCache cache;

bool needsRedraw = true;
if (graphCaches.TryGetValue(graph, out cache))
{
if (cache.NumElements == graph.Count)
{
needsRedraw = false;
}
}
//if (graphCaches.TryGetValue(graph, out cache))
//{
// if (cache.NumElements == graph.Count)
// {
// needsRedraw = false;
// }
//}


if (needsRedraw)
Expand All @@ -276,15 +280,17 @@ private void DrawGraph(IGraphValueList graph)
};
ThreadPool.QueueUserWorkItem(worker);
}
if (graphCaches.TryGetValue(graph, out cache))
lock (this)
{
DrawCache(graph, ref cache);
if (graphCaches.TryGetValue(graph, out cache))
{
DrawCache(graph, ref cache);
}
}
}

protected override void Draw(GameTime gameTime)
{

//GraphicsDevice.RasterizerState.MultiSampleAntiAlias = true;
PresentationParameters pp = GraphicsDevice.PresentationParameters;
if (pp.MultiSampleCount < 4)
Expand Down
16 changes: 16 additions & 0 deletions EvoNet/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ private CreatureManager CreatureManager

private void timer1_Tick(object sender, EventArgs e)
{
if (lastFoodIndex == 0)
{
lastFoodIndex = (int)Math.Max(0, TileMap.FoodRecord.Count - evoSimControl1.gameConfiguration.GraphCount);
}
if (lastCreatureIndex == 0)
{
lastCreatureIndex = (int)Math.Max(0, CreatureManager.AliveCreaturesRecord.Count - evoSimControl1.gameConfiguration.GraphCount);
}
while (lastFoodIndex < TileMap.FoodRecord.Count)
{
fictionalDateForFood += TimeSpan.FromSeconds(TileMap.FixedUpdateTime);
Expand All @@ -93,6 +101,14 @@ private void timer1_Tick(object sender, EventArgs e)
lastCreatureIndex++;
numberCreaturesAlive.Add(new GraphTimeDoubleValue(fictionalDateForCreatures, Value));
}
if (foodValueList.Count > evoSimControl1.gameConfiguration.GraphCount)
{
foodValueList.RemoveRange(0, (int)(foodValueList.Count - evoSimControl1.gameConfiguration.GraphCount));
}
if (numberCreaturesAlive.Count > evoSimControl1.gameConfiguration.GraphCount)
{
numberCreaturesAlive.RemoveRange(0, (int)(numberCreaturesAlive.Count - evoSimControl1.gameConfiguration.GraphCount));
}
//FoodGraph.Refresh();
}

Expand Down
1 change: 1 addition & 0 deletions EvoSim/Objects/Feeler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Vector2 FeelerPos
private WorkingNeuron outFeelerAngle = new WorkingNeuron(-1);
private WorkingNeuron outAttack = new WorkingNeuron(-1);

[NonSerialized]
private Creature feelerCreature = null;
private Creature owner = null;

Expand Down
4 changes: 2 additions & 2 deletions EvoSim/Tasks/MergeCreatureArraysTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected override void Run(float time)
sim.CreatureManager.numberOfDeaths += creatureTask.CreaturesToKill.Count;
foreach (Creature creature in creatureTask.CreaturesToKill)
{
if (sim.SimulationConfiguration.UseGraveyard &&
creature.Generation > 1 || creature.Children.Count > 1)
if (sim.SimulationConfiguration.UseGraveyard && sim.SimulationConfiguration.DoRuntimeSave &&
(creature.Generation > 1 || creature.Children.Count > 1))
{
sim.CreatureManager.Graveyard.Add(creature);
}
Expand Down
5 changes: 5 additions & 0 deletions Graph/GraphValueList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public void RemoveAt(int index)
values.RemoveAt(index);
}

public void RemoveRange(int index, int count)
{
values.RemoveRange(index, count);
}

public IGraphValue this[int index]
{
get
Expand Down

0 comments on commit 3ce7043

Please sign in to comment.