Skip to content

Commit

Permalink
Removed a bunch of foreach's on PositionedObjectList, causing allocat…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
vchelaru committed Dec 27, 2023
1 parent ba93de3 commit be59dc8
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,9 @@ public void AssignAllShapesToRepositionOutward()
List<AxisAlignedRectangle> rectanglesWithNoneReposition = new List<AxisAlignedRectangle>();

// fill it with any rectangles
foreach (var rectangle in this.Rectangles)
for(int i = 0; i < this.Rectangles.Count; i++)
{
var rectangle = this.Rectangles[i];
if (rectangle.RepositionDirections == RepositionDirections.None)
{
rectanglesWithNoneReposition.Add(rectangle);
Expand Down Expand Up @@ -1455,8 +1456,10 @@ public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection
float dimensionHalf = dimension / 2.0f;
tileShapeCollection.GridSize = dimension;

foreach (var layer in layeredTileMap.MapLayers)
//foreach (var layer in layeredTileMap.MapLayers)
for(int i = 0; i < layeredTileMap.MapLayers.Count; i++)
{
var layer = layeredTileMap.MapLayers[i];
List<int> indexesToRemove = null;
if (removeTilesOnAdd)
{
Expand Down Expand Up @@ -1541,8 +1544,9 @@ public static void AddMergedCollisionFrom(this TileShapeCollection tileShapeColl

Dictionary<int, List<int>> rectangleIndexes = new Dictionary<int, List<int>>();

foreach (var layer in layeredTileMap.MapLayers)
for(int i = 0; i < layeredTileMap.MapLayers.Count; i++)
{
var layer = layeredTileMap.MapLayers[i];
AddCollisionFromLayerInternal(tileShapeCollection, predicate, properties, dimension, dimensionHalf, rectangleIndexes, layer, removeTilesOnAdd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,10 @@ public void AddToManagers(FlatRedBall.Graphics.Layer layer)
{
SpriteManager.AddPositionedObject(this);
}
foreach (var item in this.mMapLists)

for(int i = 0; i < mMapLists.Count; i++)
{
item.AddToManagers(layer);
mMapLists[i].AddToManagers(layer);
}
}

Expand Down Expand Up @@ -1165,8 +1166,9 @@ public static void RemoveTiles(this LayeredTileMap map,
// Force execution now for performance reasons
var filteredInfos = Properties.Values.Where(predicate).ToList();

foreach (var layer in map.MapLayers)
for(int i = 0; i < map.MapLayers.Count; i++)
{
var layer = map.MapLayers[i];
RemoveTilesFromLayer(filteredInfos, layer);
}
}
Expand Down
21 changes: 19 additions & 2 deletions Engines/FlatRedBallXNA/FlatRedBall/Debugging/Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,32 @@ public static string GetAutomaticallyUpdatedObjectInformation()
// SpriteManager
stringBuilder.AppendLine(SpriteManager.ManagedPositionedObjects.Count + " PositionedObjects");

var entityCount = SpriteManager.ManagedPositionedObjects.Where(item => item.GetType().FullName.Contains(".Entities")).Count();
var entityCount = 0;

for(int i = 0; i < SpriteManager.ManagedPositionedObjects.Count; i++)
{
var item = SpriteManager.ManagedPositionedObjects[i];
if(item.GetType().FullName.Contains(".Entities"))
{
entityCount++;
}
}
stringBuilder.AppendLine($"{indentString} {entityCount} Entities");

total += SpriteManager.ManagedPositionedObjects.Count;

var totalSpriteCount = SpriteManager.AutomaticallyUpdatedSprites.Count;
stringBuilder.AppendLine(totalSpriteCount + " Sprites");

var spriteNonParticleEntityCount = SpriteManager.AutomaticallyUpdatedSprites.Where(item => item.GetType().FullName.Contains(".Entities")).Count();
var spriteNonParticleEntityCount = 0;
for(int i = 0; i < SpriteManager.AutomaticallyUpdatedSprites.Count; i++)
{
var item = SpriteManager.AutomaticallyUpdatedSprites[i];
if(item.GetType().FullName.Contains(".Entities"))
{
spriteNonParticleEntityCount++;
}
}
stringBuilder.AppendLine(indentString + spriteNonParticleEntityCount + " Entity Sprites");
stringBuilder.AppendLine(indentString + SpriteManager.ParticleCount + " Particles");
var normalSpriteCount = SpriteManager.AutomaticallyUpdatedSprites.Count - spriteNonParticleEntityCount - SpriteManager.ParticleCount;
Expand Down
3 changes: 2 additions & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/FlatRedBallServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,9 @@ private static void HandleResize()
//Window_ClientSizeChanged(sender, e);

// Update cameras
foreach (Camera camera in SpriteManager.Cameras)
for (int i = 0; i < SpriteManager.Cameras.Count; i++)
{
Camera camera = SpriteManager.Cameras[i];
camera.UpdateOnResize();
}

Expand Down
3 changes: 2 additions & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/Math/AttachableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ public AttachableList<T> FindAllWithNameContaining(string stringToSearchFor)
return listToReturn;
}


// Faster than LINQ:
public T FirstOrDefault() => Count == 0 ? default(T) : this[0];

/// <summary>
/// Inserts the argument IAttachable at the argument index and creates a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,13 +1026,13 @@ internal void ClearLastCollisionLists()

internal void ResetLastUpdateTimes()
{
foreach (var shape in mAxisAlignedRectangles) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mCircles) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mPolygons) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mLines) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mSpheres) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mAxisAlignedCubes) { shape.LastDependencyUpdate = -1; }
foreach (var shape in mCapsule2Ds) { shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mAxisAlignedRectangles.Count; i++) { AxisAlignedRectangle shape = mAxisAlignedRectangles[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mCircles.Count; i++) { Circle shape = mCircles[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mPolygons.Count; i++) { Polygon shape = mPolygons[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mLines.Count; i++) { Line shape = mLines[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mSpheres.Count; i++) { Sphere shape = mSpheres[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mAxisAlignedCubes.Count; i++) { AxisAlignedCube shape = mAxisAlignedCubes[i]; shape.LastDependencyUpdate = -1; }
for (int i = 0; i < mCapsule2Ds.Count; i++) { Capsule2D shape = mCapsule2Ds[i]; shape.LastDependencyUpdate = -1; }
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task RefreshProfilingData()
var totalCollisionCount = response.Data.CollisionData.Sum(item => item.DeepCollisions);
text += $"Total Collisions: {totalCollisionCount}\n\n";

var ordered = response.Data.CollisionData.OrderByDescending(item => item.DeepCollisions);
var ordered = response.Data.CollisionData.OrderByDescending(item => item.DeepCollisions).Where(item => item.DeepCollisions > 0).ToArray();

foreach (var item in ordered)
{
Expand All @@ -70,6 +70,14 @@ public async Task RefreshProfilingData()

text += $"{item.DeepCollisions} - {item.RelationshipName}{itemCountString}{partitionText}\n";
}

var itemsWith0 = response.Data.CollisionData.Count() - ordered.Length;

if(itemsWith0 > 0)
{
text += $"{itemsWith0} relationship(s) with 0 deep collisions";
}

ProfilingViewModel.CollisionText = text;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ private static void GenerateSetAspectRatio(ICodeBlock classContents)
elseBlock.Line("x = (FlatRedBall.FlatRedBallServices.GraphicsOptions.ResolutionWidth - destinationRectangleWidth) / 2;");
}

var foreachBlock = functionBlock.ForEach("var camera in FlatRedBall.SpriteManager.Cameras");
var foreachBlock = functionBlock.For("int i = 0; i < FlatRedBall.SpriteManager.Cameras.Count; i++");
{

foreachBlock.Line("var camera = FlatRedBall.SpriteManager.Cameras[i];");
foreachBlock.Line("int currentX = x;");
foreachBlock.Line("int currentY = y;");
foreachBlock.Line("int currentWidth = destinationRectangleWidth;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap, Instantiati
{
var entitiesToRemove = new List<string>();

foreach (var layer in layeredTileMap.MapLayers)
for(int i = 0; i < layeredTileMap.MapLayers.Count; i++)
{
var layer = layeredTileMap.MapLayers[i];
CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.TileProperties, layeredTileMap.WidthPerTile ?? 16, restrictions);
}
if (CurrentSettings.RemoveTileObjectsAfterEntityCreation)
Expand Down

0 comments on commit be59dc8

Please sign in to comment.