-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFastReplacer.cs
More file actions
427 lines (346 loc) · 11.5 KB
/
FastReplacer.cs
File metadata and controls
427 lines (346 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Text;
using System.Collections.Concurrent;
namespace ConsoleApplication1
{
internal class Program
{
// Used http://stackoverflow.com/a/20285267/40868 as a template
private static readonly Random Seed = new Random(42); //used a constant to keep result consistant
private static readonly string OutputFormat = "{0, -20} | {1, 8} | {2, -40}";
private static string BuildRandomInputString(int length)
{
var input = new StringBuilder();
for (var i = 0; i < length; i++)
{
var randomNumber = Seed.Next(0, 3);
var character = randomNumber == 0 ? 'A' : randomNumber == 1 ? 'B' : 'C';
input.Append(character);
}
return input.ToString();
}
private static List<string> BuildRandomReplacements(int replacementsCount)
{
var replacements = new List<string>();
for (var i = 0; i < replacementsCount; i++)
{
var randomNumber = Seed.Next(0, 3);
var replaceIteration = randomNumber == 0 ? "AB" : randomNumber == 1 ? "BC" : "CD";
replacements.Add(replaceIteration);
}
return replacements;
}
private static long TestImplementation(string input, string replace, string[] replacements,
Action<string, string, string[]> implementation)
{
GC.Collect();
GC.WaitForPendingFinalizers();
var stopWatch = Stopwatch.StartNew();
implementation(input, replace, replacements);
stopWatch.Stop();
return stopWatch.ElapsedMilliseconds;
}
private static void SimpleImplementation(string input, string replace, string[] replacements)
{
foreach (var replaceBy in replacements)
{
var result = input.Replace(replace, replacements[0]);
}
}
private static void SimpleParallelImplementation(string input, string replace, string[] replacements)
{
var rangePartitioner = Partitioner.Create(0, replacements.Length);
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
for (var i = range.Item1; i < range.Item2; i++)
{
var result = input.Replace(replace, replacements[i]);
}
});
}
private unsafe class FastReplacer : IDisposable
{
private readonly char* _oldValue;
private readonly int _oldValueLength;
private readonly bool* _fastTable;
private readonly char* _input;
public int FoundIndexes;
public FastReplacer(string input, string oldValue)
{
var inputLength = input.Length;
_oldValueLength = oldValue.Length;
_oldValue = (char*) Marshal.AllocHGlobal((_oldValueLength + 1)*sizeof (char));
_input = (char*) Marshal.AllocHGlobal((inputLength + 1)*sizeof (char));
_fastTable = (bool*) Marshal.AllocHGlobal((inputLength)*sizeof (bool));
fixed (char* inputSrc = input, oldValueSrc = oldValue)
{
Copy(inputSrc, _input);
Copy(oldValueSrc, _oldValue);
}
BuildMatchedIndexes();
}
public void Replace(char* outputPtr, int outputLength, string newValue)
{
var newValueLength = newValue.Length;
char* inputPtr = _input;
bool* fastTablePtr = _fastTable;
fixed (char* newValuePtr = newValue)
{
while (*inputPtr != 0)
{
if (*fastTablePtr)
{
Copy(newValuePtr, outputPtr);
outputLength -= newValueLength;
outputPtr += newValueLength;
inputPtr += _oldValueLength;
fastTablePtr += _oldValueLength;
continue;
}
*outputPtr++ = *inputPtr++;
fastTablePtr++;
}
}
*outputPtr = '\0';
}
public void Dispose()
{
if (_fastTable != null) Marshal.FreeHGlobal(new IntPtr(_fastTable));
if (_input != null) Marshal.FreeHGlobal(new IntPtr(_input));
if (_oldValue != null) Marshal.FreeHGlobal(new IntPtr(_oldValue));
}
private void Copy(char* sourcePtr,
char* targetPtr)
{
while ((*targetPtr++ = *sourcePtr++) != 0) ;
}
/// <summary>
/// KMP?!
/// </summary>
private void BuildMatchedIndexes()
{
var sourcePtr = _input;
var fastTablePtr = _fastTable;
var i = 0;
while (sourcePtr[i] != 0)
{
fastTablePtr[i] = false;
if (sourcePtr[i] == _oldValue[0])
{
var tempSourcePtr = &sourcePtr[i];
var tempOldValuePtr = _oldValue;
var isMatch = true;
while (isMatch &&
*tempSourcePtr != 0 &&
*tempOldValuePtr != 0)
{
if (*tempSourcePtr != *tempOldValuePtr)
isMatch = false;
tempSourcePtr++;
tempOldValuePtr++;
}
if (isMatch)
{
fastTablePtr[i] = true;
i += _oldValueLength;
FoundIndexes++;
continue;
}
}
i++;
}
}
}
private static unsafe void UnsafeUnmanagedImplementation(string input, string oldValue, string[] newValues)
{
using (var fastReplacer = new FastReplacer(input, oldValue))
{
var replaceLength = oldValue.Length;
var inputLength = input.Length;
var replacedDataLength = fastReplacer.FoundIndexes*replaceLength;
var maximumParallelWork = newValues.Length > Environment.ProcessorCount
? Environment.ProcessorCount
: newValues.Length;
var partitions = Partitioner.Create(0, newValues.Length).GetPartitions(maximumParallelWork);
var threads = new Thread[partitions.Count];
var biggestIndex = 0;
for (var k = 1; k < newValues.Length; k++)
{
if (newValues[biggestIndex].Length < newValues[k].Length)
biggestIndex = k;
}
var biggestStackSize = inputLength - replacedDataLength + fastReplacer.FoundIndexes*newValues[biggestIndex].Length;
for (var i = 0; i < partitions.Count; i++)
{
var partition = partitions[i];
partition.MoveNext();
threads[i] = new Thread(() =>
{
var range = partition.Current;
var maxLength = newValues[range.Item1].Length;
for (var j = range.Item1 + 1; j < range.Item2; j++)
maxLength = Math.Max(newValues[j].Length, maxLength);
var outputLength = inputLength - replacedDataLength + fastReplacer.FoundIndexes*maxLength;
var outputPtr = stackalloc char[(outputLength + 1)];
for (var d = range.Item1; d < range.Item2; d++)
{
fastReplacer.Replace(outputPtr, outputLength, newValues[d]);
//Console.WriteLine(new string(outputPtr));
}
}, biggestStackSize + 1000 * 1000 * 1000);
threads[i].Start();
}
for (var k = 0; k < threads.Length; k++)
threads[k].Join();
}
}
private static void ParallelSubstringImplementation(string input, string replace, string[] replaceBy)
{
var startingPosition = 0;
var indexes = new List<int>();
int currentPosition;
while ((currentPosition = input.IndexOf(replace, startingPosition)) >= 0)
{
indexes.Add(currentPosition);
startingPosition = currentPosition + 1;
}
var replaceByPartitioner = Partitioner.Create(0, replaceBy.Length);
var rangePartitioner = Partitioner.Create(0, indexes.Count);
Parallel.ForEach(replaceByPartitioner, (outerRange, outerLoopState) =>
{
for (var g = outerRange.Item1; g < outerRange.Item2; g++)
{
var replaceWith = replaceBy[g];
var finalSize = input.Length - (indexes.Count * replace.Length) + (indexes.Count * replaceWith.Length);
var finalResult = new char[finalSize];
Parallel.ForEach(rangePartitioner, (innerRange, innerLoopState) =>
{
for (var i = innerRange.Item1; i < innerRange.Item2; i++)
{
var currentIndex = indexes[i];
var prevIndex = i > 0 ? indexes[i - 1] : -replace.Length;
var n = 0;
if (prevIndex >= 0)
{
n = prevIndex + replace.Length;
// note that due to offset changes,
// prevIndex doesn't map to 1:1 if lens are different
if (replace.Length != replaceWith.Length)
{
var offset = (replace.Length - replaceWith.Length) * i;
var dir = replace.Length < replaceWith.Length;
n = prevIndex + offset + replaceWith.Length + (dir ? 1 : -1);
}
}
// append everything between two indexes.
for (var k = prevIndex + replace.Length; k < currentIndex; k++)
finalResult[n++] = input[k];
// append replaced text.
foreach (var ch in replaceWith)
finalResult[n++] = ch;
// if dealing with last index.
// we need to append remaining parts
if (currentIndex == indexes[indexes.Count - 1])
{
for (var k = currentIndex + replace.Length; k < input.Length; k++)
finalResult[n++] = input[k];
}
}
});
}
});
}
private unsafe static void FredouImplementation(string input, string replace, string[] replaceBy)
{
var inputLength = input.Length;
var indexes = new List<int>();
//my own string.indexof to save a few ms
int len = inputLength;
fixed (char* i = input, r = replace)
{
while (--len > -1)
{
//i wish i knew how to do this in 1 compare :-)
if (i[len] == r[0] && i[len + 1] == r[1])
{
indexes.Add(len--);
}
}
}
var idx = indexes.ToArray();
len = indexes.Count;
Parallel.For(0, replaceBy.Length, l =>
Process(input, inputLength, replaceBy[l], idx, len)
);
}
private unsafe static void Process(string input, int len, string replaceBy, int[] idx, int idxLen)
{
var output = new char[len];
int l;
fixed (char* o = output, i = input)
{
//direct copy, simulate string.copy
for (l = 0; l < len; ++l)
{
o[l] = i[l];
}
//oldValue, i also wish to know how to do this in 1 shot
for (l = 0; l < idxLen; ++l)
{
o[idx[l]] = replaceBy[0];
o[idx[l] + 1] = replaceBy[1];
}
}
//Console.WriteLine(output);
}
private static void Main()
{
var implementations = new[]
{
new Tuple<string, Action<string, string, string[]>>("Simple", SimpleImplementation),
new Tuple<string, Action<string, string, string[]>>("SimpleParallel", SimpleParallelImplementation),
new Tuple<string, Action<string, string, string[]>>("ParallelSubstring", ParallelSubstringImplementation),
new Tuple<string, Action<string, string, string[]>>("Fredou unsafe", FredouImplementation),
new Tuple<string, Action<string, string, string[]>>("Unsafe+unmanaged_mem", UnsafeUnmanagedImplementation)
};
Console.WriteLine(OutputFormat, "Implementation", "Average", "Seperate runs");
var replace ="BC";
var testCases = new[]
{
new {ReplacementCount = 500, InputLength = (int)Math.Pow(10, 6) * 2},
new {ReplacementCount = 500, InputLength = (int)Math.Pow(10, 6)},
new {ReplacementCount = 100, InputLength = (int)Math.Pow(10, 6) / 2},
new {ReplacementCount = 50, InputLength = (int)Math.Pow(10, 3)}
};
var result = new Dictionary<string, List<long>>();
foreach (var testCase in testCases)
{
var input = BuildRandomInputString(testCase.InputLength);
var replacements = BuildRandomReplacements(testCase.ReplacementCount).ToArray();
foreach (var implementation in implementations)
{
var elapsedTime = TestImplementation(input, replace, replacements, implementation.Item2);
if (!result.ContainsKey(implementation.Item1))
result[implementation.Item1] = new List<long>();
result[implementation.Item1].Add(elapsedTime);
}
}
foreach (var implementation in implementations)
{
var l = result[implementation.Item1];
var averageTime = (long)l.Average();
Console.WriteLine(OutputFormat, implementation.Item1, averageTime,
string.Join(", ", l.Select(x => x.ToString(CultureInfo.InvariantCulture))));
}
Console.ReadKey();
}
}
}