Skip to content

Commit 59a80a3

Browse files
committed
[upstream] Optimizations (erincatto/box2d#1050)
Improved performance, up to 20% gain in stacking scenarios. Behavior change: contact solver bias stage no longer applies friction (almost free perf gain) Behavior change: b2World_Step still performs all operations, including collision when timeStep == 0. b2WorldDef now has b2Capacity that lets you pre-size some arrays to reduce allocations. Optimized hit events. These are basically free now. Cleaned up and optimized solver scheduling. Converted to new dynamic array type. Added threading logic to prevent theoretical solver deadlock if the user task system stalls worker 0. Added Compounds benchmark.
1 parent 0dd71f9 commit 59a80a3

27 files changed

Lines changed: 917 additions & 333 deletions

src/Box2D.NET.Samples/Primitives/SampleEntry.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public class SampleEntry
1313
public readonly string Name;
1414
public readonly string Title;
1515
public readonly Func<SampleContext, Sample> CreateFcn;
16+
public readonly Func<B2Capacity> CapacityFcn;
1617

17-
public SampleEntry(string category, string name, Func<SampleContext, Sample> createFcn)
18+
public SampleEntry(string category, string name, Func<SampleContext, Sample> createFcn, Func<B2Capacity> capacityFcn = null)
1819
{
1920
Category = category;
2021
Name = name;
2122
Title = $"{Category} : {Name}";
2223
CreateFcn = createFcn;
24+
CapacityFcn = capacityFcn;
2325
}
24-
}
26+
}

src/Box2D.NET.Samples/SampleContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class SampleContext
4141

4242
// These are persisted
4343
public int sampleIndex = 0;
44+
public B2Capacity capacity;
4445

4546

4647
public B2DebugDraw debugDraw;

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCompound.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010

1111
namespace Box2D.NET.Samples.Samples.Benchmarks;
1212

13-
public class BenchmarkCompound : Sample
13+
public class BenchmarkLargeCompounds : Sample
1414
{
15-
private static readonly int SampleCompound = SampleFactory.Shared.RegisterSample("Benchmark", "Compound", Create);
15+
private static readonly int SampleLargeCompounds = SampleFactory.Shared.RegisterSample("Benchmark", "Large Compounds", Create);
1616

1717
private static Sample Create(SampleContext context)
1818
{
19-
return new BenchmarkCompound(context);
19+
return new BenchmarkLargeCompounds(context);
2020
}
2121

22-
public BenchmarkCompound(SampleContext context) : base(context)
22+
public BenchmarkLargeCompounds(SampleContext context) : base(context)
2323
{
2424
if (m_context.restart == false)
2525
{
@@ -105,4 +105,4 @@ public BenchmarkCompound(SampleContext context) : base(context)
105105
}
106106
}
107107
}
108-
}
108+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
2+
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
3+
// SPDX-License-Identifier: MIT
4+
5+
using static Box2D.NET.Shared.Benchmarks;
6+
7+
namespace Box2D.NET.Samples.Samples.Benchmarks;
8+
9+
public class BenchmarkCompounds : Sample
10+
{
11+
private static readonly int SampleBenchmarkCompounds = SampleFactory.Shared.RegisterSample("Benchmark", "Compounds", Create);
12+
13+
private static Sample Create(SampleContext context)
14+
{
15+
return new BenchmarkCompounds(context);
16+
}
17+
18+
public BenchmarkCompounds(SampleContext context) : base(context)
19+
{
20+
if (m_context.restart == false)
21+
{
22+
m_context.camera.center = new B2Vec2(0.0f, 50.0f);
23+
m_context.camera.zoom = 25.0f * 2.2f;
24+
m_context.enableSleep = false;
25+
}
26+
27+
CreateCompounds(m_worldId);
28+
}
29+
}

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkManyPyramids.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ namespace Box2D.NET.Samples.Samples.Benchmarks;
88

99
public class BenchmarkManyPyramids : Sample
1010
{
11-
private static readonly int SampleBenchmarkManyPyramids = SampleFactory.Shared.RegisterSample("Benchmark", "Many Pyramids", Create);
11+
private static readonly int SampleBenchmarkManyPyramids =
12+
SampleFactory.Shared.RegisterSampleWithCapacity("Benchmark", "Many Pyramids", Create, GetCapacity);
1213

1314
private static Sample Create(SampleContext context)
1415
{
1516
return new BenchmarkManyPyramids(context);
1617
}
1718

19+
private static B2Capacity GetCapacity()
20+
{
21+
return GetManyPyramidsCapacity();
22+
}
23+
1824
public BenchmarkManyPyramids(SampleContext context) : base(context)
1925
{
2026
if (m_context.restart == false)

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkWasher.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
using static Box2D.NET.Shared.Benchmarks;
6+
using static Box2D.NET.B2Worlds;
67

78
namespace Box2D.NET.Samples.Samples.Benchmarks;
89

@@ -25,4 +26,12 @@ private BenchmarkWasher(SampleContext context) : base(context)
2526

2627
CreateWasher(m_worldId);
2728
}
29+
30+
public override void Step()
31+
{
32+
base.Step();
33+
34+
B2ContactEvents events = b2World_GetContactEvents(m_worldId);
35+
DrawTextLine($"hits = {events.hitCount}");
36+
}
2837
}

src/Box2D.NET.Samples/Samples/Collisions/DynamicTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void BuildTree()
139139

140140
float y = -4.0f;
141141

142-
m_tree = b2DynamicTree_Create();
142+
m_tree = b2DynamicTree_Create(16);
143143

144144
B2Vec2 aabbMargin = new B2Vec2(0.1f, 0.1f);
145145

0 commit comments

Comments
 (0)