Skip to content

Commit 7eabf41

Browse files
authored
Merge pull request #86 from winnerspiros/improve-editorbeatmap-performance-18417970512521063628
⚡ Improve EditorBeatmap.findInsertionIndex performance using binary search
2 parents 5cdbde2 + 896f7d9 commit 7eabf41

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

osu.Game/Screens/Edit/EditorBeatmap.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,19 @@ private void trackStartTime(HitObject hitObject)
515515

516516
private int findInsertionIndex(IReadOnlyList<HitObject> list, double startTime)
517517
{
518-
for (int i = 0; i < list.Count; i++)
518+
int min = 0;
519+
int max = list.Count - 1;
520+
521+
while (min <= max)
519522
{
520-
if (list[i].StartTime > startTime)
521-
return i - 1;
523+
int mid = min + (max - min) / 2;
524+
if (list[mid].StartTime <= startTime)
525+
min = mid + 1;
526+
else
527+
max = mid - 1;
522528
}
523529

524-
return list.Count - 1;
530+
return min - 1;
525531
}
526532

527533
public double SnapTime(double time, double? referenceTime) => ControlPointInfo.GetClosestSnappedTime(time, BeatDivisor, referenceTime);

replace_method.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
3+
filepath = "osu.Game/Screens/Edit/EditorBeatmap.cs"
4+
with open(filepath, 'r') as f:
5+
content = f.read()
6+
7+
old_method = """ public int findInsertionIndex(IReadOnlyList<HitObject> list, double startTime)
8+
{
9+
for (int i = 0; i < list.Count; i++)
10+
{
11+
if (list[i].StartTime > startTime)
12+
return i - 1;
13+
}
14+
15+
return list.Count - 1;
16+
}"""
17+
18+
new_method = """ public int findInsertionIndex(IReadOnlyList<HitObject> list, double startTime)
19+
{
20+
int min = 0;
21+
int max = list.Count - 1;
22+
23+
while (min <= max)
24+
{
25+
int mid = min + (max - min) / 2;
26+
if (list[mid].StartTime <= startTime)
27+
min = mid + 1;
28+
else
29+
max = mid - 1;
30+
}
31+
32+
return min - 1;
33+
}"""
34+
35+
if old_method not in content:
36+
# Try normalizing line endings or whitespace if needed, but let's check exact match first
37+
# Maybe try stripping whitespace
38+
# Actually, I'll print a snippet to debug if it fails
39+
print("Method not found!")
40+
# Find approximate location
41+
start_idx = content.find("public int findInsertionIndex")
42+
if start_idx != -1:
43+
print("Found start at:", start_idx)
44+
print("Content snippet:")
45+
print(content[start_idx:start_idx+300])
46+
sys.exit(1)
47+
48+
new_content = content.replace(old_method, new_method)
49+
50+
with open(filepath, 'w') as f:
51+
f.write(new_content)

0 commit comments

Comments
 (0)