-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfo4-gc-test
executable file
·425 lines (361 loc) · 12.7 KB
/
fo4-gc-test
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
#!/usr/bin/perl
# Basic testing script for Bethesda's busted Papyrus VM iterative GC.
#
# Per Nukem, `struct_cleanup_v0` is the method used by the vanilla game. It
# unfortunately has a critical flaw where if the first index is to be GC'ed it
# will process that index and that index only and then return. This is due to
# its over-reliance on index positions being consistent even though the same
# function modifies the array it's operating on (and does not increment the
# working index in that case). A better approach is to guard against processing
# no more than n elements of the array (v1) rather than worrying if the current
# array index == starting index.
#
# To see an example of how the vanilla approach is problematic, run:
#
# $ fo4-gc-test --dead-chance=99 --max-cycles=100
#
# Then watch the algorithm return after the first GC-able element multiple
# times in a row until it finally finds a live element which it can skip. This
# results in the working index being advanced past the tracking index, hence
# making the while condition true and allowing it to GC multiple elements
# until either wrap-around to the tracking index or time runs out.
package fo4_gc_test;
use Getopt::Long qw(:config no_ignore_case no_auto_abbrev bundling);
use Carp qw(confess);
use Data::Dumper;
use Pod::Usage;
use strict;
use warnings FATAL => qw(all);
local $SIG{__DIE__} = \&confess;
local $Data::Dumper::Sortkeys = 1;
local $Data::Dumper::Useqq = 1;
our $VERSION = '$Id$';
my $verbose = 0;
my $method = 'struct_cleanup_v0';
my $method_tab = {
'struct_cleanup_v0' => \&struct_cleanup_v0,
'struct_cleanup_v1' => \&struct_cleanup_v1,
'struct_cleanup_v2' => \&struct_cleanup_v2,
};
sub new {
my ($class, $rc) = @_;
return bless { id => int(rand(2**32)), ref_count => $rc || 1 }, $class;
}
use Class::XSAccessor (
accessors => { map +($_, $_), qw(ref_count) }
);
# bool CleanupStructs(float TimeBudget, BSTArray<BSTSmartPointer<BSScript::Struct>>& Elements, uint32_t& NextIndexToClean)
# {
# bool didGC = false;
#
# float startTime = BSPrecisionTimer::GetTimer();
# float budget = BSPrecisionTimer::fFrequencyMS * TimeBudget;
#
# // NextIndexToClean stores the last checked entry for iterative GC purposes.
# if (NextIndexToClean >= Elements.QSize())
# NextIndexToClean = Elements.QSize() - 1;
#
# uint32_t index = NextIndexToClean;
#
# if (!Elements.QEmpty())
# {
# do
# {
# auto& element = Elements[index];
#
# if (element->RefCount == 1)
# {
# didGC = true;
# Elements.RemoveIndex(index);
#
# if (!Elements.QEmpty() && NextIndexToClean >= Elements.QSize())
# NextIndexToClean = Elements.QSize() - 1;
#
# // Note that 'index' isn't incremented when an entry is deleted. There's a chance that 'NextIndexToClean' == 'index' on the first loop.
# }
# else
# {
# index++;
# }
#
# if (index >= Elements.QSize())
# index = 0;
#
# } while (
# index != NextIndexToClean && // Break when 'index' == 'NextIndexToClean'. Refer to the previous note. This CAN exit after a single object is collected.
# !Elements.QEmpty() &&
# BSPrecisionTimer::GetTimer() - startTime <= budget);
# }
#
# NextIndexToClean = index;
# return didGC;
# }
sub struct_cleanup_v0 {
my ($elements, $index_next, %opts) = @_;
my $min_proc = $opts{'min_proc'} // 8;
my $max_proc = $opts{'max_proc'} // 32;
my $gc = 0;
if ($$index_next >= scalar @$elements) {
$$index_next = scalar @$elements - 1;
}
my $index = $$index_next;
return $gc unless (scalar @$elements);
# simulate variable time-cost within budget with a random amount of cycles
my $time_max = ($min_proc < $max_proc ? $min_proc : $max_proc)
+ ($max_proc > $min_proc ? int(rand($max_proc - $min_proc)) : 0);
do {
if ($verbose) {
printf STDERR ("size == %2d, index == %2d, index_next == %2d, time_max == %2d, will_gc == %d\n",
scalar @$elements, $index, $$index_next, $time_max, $elements->[$index]->ref_count <= 1);
}
my $elem = $elements->[$index];
if ($elem->ref_count == 1) {
splice(@$elements, $index, 1);
$elem->{'ref_count'}--;
$gc++;
if (scalar @$elements && $$index_next >= scalar @$elements) {
$$index_next = scalar @$elements - 1;
}
# do not advance index here as the array has shrunk
# and index now points to the previously next index
} else {
$index++;
}
# wrap around if necessary
if ($index >= scalar @$elements) {
$index = 0;
}
} while ($index != $$index_next && scalar @$elements && --$time_max > 0);
$$index_next = $index;
return $gc;
}
# bool CleanupStructs(float TimeBudget, BSTArray<BSTSmartPointer<BSScript::Struct>>& Elements, uint32_t& NextIndexToClean)
# {
# bool didGC = false;
#
# float startTime = BSPrecisionTimer::GetTimer();
# float budget = BSPrecisionTimer::fFrequencyMS * TimeBudget;
#
# // NextIndexToClean stores the last checked entry for iterative GC purposes. If
# // it's beyond the size of the array, reset it to the start and go from there.
# uint32_t index = (NextIndexToClean < Elements.QSize() ? NextIndexToClean : 0);
#
# // Examine no more elements than the array currently holds, regardless of position.
# // If no elements are cleaned this will not be more than a full wrap-around.
# uint32_t elem_max = Elements.QSize();
#
# while (elem_max-- && !Elements.QEmpty() && BSPrecisionTimer::GetTimer() - startTime <= budget)
# {
# auto& element = Elements[index];
#
# if (element->RefCount == 1)
# {
# Elements.RemoveIndex(index);
# didGC = true;
#
# // do not advance index here as the array has shrunk
# // and index now points to the previously next index
# }
# else
# {
# index++;
# }
#
# // wrap around if necessary
# if (index >= Elements.QSize())
# index = 0;
# }
#
# NextIndexToClean = index;
#
# return didGC;
# }
sub struct_cleanup_v1 {
my ($elements, $index_next, %opts) = @_;
my $min_proc = $opts{'min_proc'} // 8;
my $max_proc = $opts{'max_proc'} // 32;
my $gc = 0;
# if index-next is past the end of the array, reset to the start
my $index = $$index_next < scalar @$elements ? $$index_next : 0;
# simulate variable time-cost within budget with a random amount of cycles
my $time_max = ($min_proc < $max_proc ? $min_proc : $max_proc)
+ ($max_proc > $min_proc ? int(rand($max_proc - $min_proc)) : 0);
# do not process more than these many elements, regardless of index position
my $elem_max = scalar @$elements;
while ($elem_max-- && scalar @$elements && $time_max--) {
if ($verbose) {
printf STDERR ("size == %2d, index == %2d, elem_max == %2d, time_max == %2d, will_gc == %d\n",
scalar @$elements, $index, $elem_max, $time_max, $elements->[$index]->ref_count <= 1);
}
my $elem = $elements->[$index];
if ($elem->ref_count == 1) {
splice(@$elements, $index, 1);
$elem->{'ref_count'}--;
$gc++;
# do not advance index here as the array has shrunk
# and index now points to the previously next index
} else {
$index++;
}
# wrap around if necessary
if ($index >= scalar @$elements) {
$index = 0;
}
}
$$index_next = $index;
return $gc;
}
# bool CleanupStructs(float TimeBudget, BSTArray<BSTSmartPointer<BSScript::Struct>>& Elements, uint32_t& NextIndexToClean)
# {
# bool didGC = false;
#
# float startTime = BSPrecisionTimer::GetTimer();
# float budget = BSPrecisionTimer::fFrequencyMS * TimeBudget;
#
# // NextIndexToClean stores the last checked entry for iterative GC purposes. If
# // it's beyond the size of the array, reset it to the end and go backwards from there.
# uint32_t index = (NextIndexToClean < Elements.QSize() ? NextIndexToClean : Elements.QSize() - 1);
#
# // Examine no more elements than the array currently holds, regardless of position.
# // If no elements are cleaned this will not be more than a full wrap-around.
# uint32_t elem_max = Elements.QSize();
#
# while (elem_max-- && !Elements.QEmpty() && BSPrecisionTimer::GetTimer() - startTime <= budget)
# {
# auto& element = Elements[index];
#
# if (element->RefCount == 1)
# {
# Elements.RemoveIndex(index);
# didGC = true;
# }
#
# // wrap around if necessary
# if (!index--)
# index = Elements.QSize() - 1;
# }
#
# NextIndexToClean = index;
#
# return didGC;
# }
sub struct_cleanup_v2 {
my ($elements, $index_next, %opts) = @_;
my $min_proc = $opts{'min_proc'} // 8;
my $max_proc = $opts{'max_proc'} // 32;
my $gc = 0;
# if index-next is past the end of the array, reset to the end
my $index = $$index_next < scalar @$elements ? $$index_next : scalar @$elements - 1;
# simulate variable time-cost within budget with a random amount of cycles
my $time_max = ($min_proc < $max_proc ? $min_proc : $max_proc)
+ ($max_proc > $min_proc ? int(rand($max_proc - $min_proc)) : 0);
# do not process more than these many elements, regardless of index position
my $elem_max = scalar @$elements;
while ($elem_max-- && scalar @$elements && $time_max--) {
if ($verbose) {
printf STDERR ("size == %2d, index == %2d, elem_max == %2d, time_max == %2d, will_gc == %d\n",
scalar @$elements, $index, $elem_max, $time_max, $elements->[$index]->ref_count <= 1);
}
my $elem = $elements->[$index];
if ($elem->ref_count == 1) {
splice(@$elements, $index, 1);
$elem->{'ref_count'}--;
$gc++;
}
# wrap around if necessary
if (!$index--) {
$index = scalar @$elements - 1;
}
}
$$index_next = $index;
return $gc;
}
sub elements_dump {
my ($elements, $max_size, $pre, $pst, $gc) = @_;
my $element_count = scalar @$elements;
my @lout;
my $live_count = 0;
for (my $i = 0; $i < $element_count; $i++) {
push @lout, $elements->[$i]->ref_count == 1 ? '.' : '*';
$live_count++ if ($elements->[$i]->ref_count > 1);
}
my @iout = split('', ' ' x $max_size);
$iout[$pre] = '^' if (defined $pre);
$iout[$pst] = ($pst == $pre ? '>' : '$') if (defined $pst);
my @xout = split('', '0123456789' x ($max_size / 10));
printf STDERR ("%2s |%*.*s|\n", ' ', $max_size, $element_count, join('', @xout));
printf STDERR ("%2d :: |%*.*s| live: %d, dead: %d\n", $element_count, $max_size, $max_size, join('', @lout), $live_count, $element_count - $live_count);
printf STDERR ("%2d :: |%*.*s| idx: %d -> %d\n", $gc // 0, $max_size, $element_count, join('', @iout), $pre, $pst);
printf STDERR "\n";
}
sub elements_gen {
my ($max, $dead_chance) = @_;
my @tmp;
while ($max--) {
my $live = rand(1) > $dead_chance / 100 ? 1 : 0;
my $elem = __PACKAGE__->new(1 + $live);
push @tmp, $elem;
}
return @tmp;
}
sub main {
my $ret;
my $help = 0;
my $max_size = 80;
my $max_cycles = 20;
my $index_init = 0;
my $constant_fill = 0;
my $alter_scanned = 0;
my $alter_scanned_chance = 10;
my $dead_chance = 50;
my $min_proc = 8;
my $max_proc = 32;
local @ARGV = @_;
GetOptions(
'h|help' => \$help,
'verbose|v+' => \$verbose,
'max-size=i' => \$max_size,
'max-cycles=i' => \$max_cycles,
'init-index=i' => \$index_init,
'constant-fill+' => \$constant_fill,
'alter-scanned+' => \$alter_scanned,
'alter-scanned-chance=i' => \$alter_scanned_chance,
'dead-chance=i' => \$dead_chance,
'min-proc=i' => \$min_proc,
'max-proc=i' => \$max_proc,
'method=s' => \$method,
) && (!$help && ($ret = 1));
$ret || pod2usage(-verbose => 2, -noperldoc => 1, -exitval => -1);
# gc method to use
my $cb = $method_tab->{$method} || die "Unknown method";
my %cb_opts = (
min_proc => $min_proc,
max_proc => $max_proc,
);
# initial index
my $index_next = $index_init;
# initial propagation
my $elements = [ elements_gen($max_size, $dead_chance) ];
# dump initial layout
elements_dump($elements, $max_size, $index_next, $index_next, 0);
while ($max_cycles-- && grep +($_->ref_count <= 1), @$elements) {
my $index_next_pre = $index_next;
my $gc = $cb->($elements, \$index_next, %cb_opts);
if ($constant_fill) {
# fill in new objects to keep array a constant length
push @$elements, elements_gen($max_size - scalar @$elements, $dead_chance);
}
elements_dump($elements, $max_size, $index_next_pre, $index_next, $gc);
if ($alter_scanned) {
# randomly set some already scanned live objects to cleanable
for (my $i = $index_next_pre; $i < $index_next && $i < scalar @$elements; $i++) {
next if ($elements->[$i]->ref_count <= 1);
next if (rand(1) > $alter_scanned_chance / 100);
$elements->[$i]{'ref_count'}--;
}
}
}
return 1;
}
exit !main(@ARGV) unless (caller);
__END__