Skip to content

Commit 9212f83

Browse files
committed
Improve perf of InputText/Multiline.
1 parent 1ed6432 commit 9212f83

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

src/ImGui.NET/ImGui.Manual.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Numerics;
3+
using System.Runtime.CompilerServices;
34
using System.Text;
45

56
namespace ImGuiNET
@@ -88,22 +89,25 @@ public static bool InputText(
8889
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
8990
}
9091

91-
int bytesNeeded = Encoding.UTF8.GetByteCount(input);
92-
int stackBufSize = Math.Max((int)maxLength, bytesNeeded);
92+
int originalByteCount = Encoding.UTF8.GetByteCount(input);
93+
int stackBufSize = Math.Max((int)maxLength, originalByteCount);
9394
byte* bufBytes = stackalloc byte[stackBufSize];
9495
fixed (char* u16Ptr = input)
9596
{
9697
Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize);
9798
}
9899

100+
byte* originalBufBytes = stackalloc byte[originalByteCount];
101+
Unsafe.CopyBlock(originalBufBytes, bufBytes, (uint)originalByteCount);
102+
99103
byte result = ImGuiNative.igInputText(
100104
labelBytes,
101105
bufBytes,
102106
(uint)stackBufSize,
103107
flags,
104108
callback,
105109
user_data.ToPointer());
106-
if (!Util.AreStringsEqual(input, bufBytes))
110+
if (!Util.AreStringsEqual(originalBufBytes, originalByteCount, bufBytes))
107111
{
108112
input = Util.StringFromPtr(bufBytes);
109113
}
@@ -148,14 +152,17 @@ public static bool InputTextMultiline(
148152
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
149153
}
150154

151-
int bytesNeeded = Encoding.UTF8.GetByteCount(input);
152-
int stackBufSize = Math.Max((int)maxLength, bytesNeeded);
155+
int originalByteCount = Encoding.UTF8.GetByteCount(input);
156+
int stackBufSize = Math.Max((int)maxLength, originalByteCount);
153157
byte* bufBytes = stackalloc byte[stackBufSize];
154158
fixed (char* u16Ptr = input)
155159
{
156160
Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize);
157161
}
158162

163+
byte* originalBufBytes = stackalloc byte[originalByteCount];
164+
Unsafe.CopyBlock(originalBufBytes, bufBytes, (uint)originalByteCount);
165+
159166
byte result = ImGuiNative.igInputTextMultiline(
160167
labelBytes,
161168
bufBytes,
@@ -164,7 +171,7 @@ public static bool InputTextMultiline(
164171
flags,
165172
callback,
166173
user_data.ToPointer());
167-
if (!Util.AreStringsEqual(input, bufBytes))
174+
if (!Util.AreStringsEqual(originalBufBytes, originalByteCount, bufBytes))
168175
{
169176
input = Util.StringFromPtr(bufBytes);
170177
}

src/ImGui.NET/Util.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,14 @@ public static unsafe string StringFromPtr(byte* ptr)
1515
return Encoding.UTF8.GetString(ptr, characters);
1616
}
1717

18-
internal static unsafe bool AreStringsEqual(string a, byte* b)
18+
internal static unsafe bool AreStringsEqual(byte* a, int aLength, byte* b)
1919
{
20-
if (a.Length == 0) { return b[0] == 0; }
21-
22-
int aCount = Encoding.UTF8.GetByteCount(a);
23-
byte* aBytes = stackalloc byte[aCount];
24-
fixed (char* labelPtr = a)
25-
{
26-
Encoding.UTF8.GetBytes(labelPtr, a.Length, aBytes, aCount);
27-
}
28-
29-
for (int i = 0; i < aCount; i++)
20+
for (int i = 0; i < aLength; i++)
3021
{
31-
if (aBytes[i] != b[i]) { return false; }
22+
if (a[i] != b[i]) { return false; }
3223
}
3324

34-
if (b[aCount] != 0) { return false; }
25+
if (b[aLength] != 0) { return false; }
3526

3627
return true;
3728
}

0 commit comments

Comments
 (0)