-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_string_id_optimization.c
More file actions
317 lines (260 loc) · 7.4 KB
/
Copy pathtest_string_id_optimization.c
File metadata and controls
317 lines (260 loc) · 7.4 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <stackvalue.h>
#include <variables.h>
#include <action.h>
// VAL macro is defined in action.h, so don't redefine it
#define INITIAL_STACK_SIZE 8388608
#define INITIAL_SP INITIAL_STACK_SIZE
static SWFAppContext app_context =
{
.frame_funcs = NULL // Will be set in main()
#ifndef NO_GRAPHICS
,
.width = 800,
.height = 600,
.stage_to_ndc = NULL,
.bitmap_count = 0,
.bitmap_highest_w = 0,
.bitmap_highest_h = 0,
.shape_data = NULL,
.shape_data_size = 0,
.transform_data = NULL,
.transform_data_size = 0,
.color_data = NULL,
.color_data_size = 0,
.uninv_mat_data = NULL,
.uninv_mat_data_size = 0,
.gradient_data = NULL,
.gradient_data_size = 0,
.bitmap_data = NULL,
.bitmap_data_size = 0
#endif
};
// Test result tracking
typedef struct
{
int total;
int passed;
int failed;
} TestResults;
TestResults results = {0, 0, 0};
void assert_string_equals(const char* test_name, const char* expected, const char* actual)
{
results.total++;
if (strcmp(expected, actual) == 0)
{
results.passed++;
printf(" ✓ %s: PASS\n", test_name);
}
else
{
results.failed++;
printf(" ✗ %s: FAIL\n", test_name);
printf(" Expected: '%s'\n", expected);
printf(" Got: '%s'\n", actual);
}
}
void assert_true(const char* test_name, bool condition)
{
results.total++;
if (condition)
{
results.passed++;
printf(" ✓ %s: PASS\n", test_name);
}
else
{
results.failed++;
printf(" ✗ %s: FAIL\n", test_name);
}
}
// Test 1: Array-based variable access with string IDs
void test_array_based_variable()
{
printf("\n[TEST 1] Array-based Variable Access (String ID = 5)\n");
char* stack = (char*) calloc(1, INITIAL_STACK_SIZE);
u32 sp_val = INITIAL_SP;
u32* sp = &sp_val;
u32 oldSP;
// Push variable name "x" with ID = 5
PUSH_STR_ID("x", 1, 5);
// Push value "hello"
PUSH_STR_ID("hello", 5, 6);
// Set variable
actionSetVariable(stack, sp);
// Get variable back
PUSH_STR_ID("x", 1, 5);
actionGetVariable(stack, sp);
// Check result
char* result = (char*) VAL(u64, &stack[*sp + 16]);
assert_string_equals("Array-based get/set", "hello", result);
free(stack);
}
// Test 2: Hashmap-based variable access (ID = 0)
void test_hashmap_based_variable()
{
printf("\n[TEST 2] Hashmap-based Variable Access (String ID = 0)\n");
char* stack = (char*) calloc(1, INITIAL_STACK_SIZE);
u32 sp_val = INITIAL_SP;
u32* sp = &sp_val;
u32 oldSP;
char dynamic_name[] = "dynamic_var";
// Push variable name (dynamic, ID = 0)
PUSH_STR(dynamic_name, strlen(dynamic_name));
// Push value
PUSH_STR_ID("world", 5, 0);
// Set variable
actionSetVariable(stack, sp);
// Get variable back
PUSH_STR(dynamic_name, strlen(dynamic_name));
actionGetVariable(stack, sp);
// Check result
char* result = (char*) VAL(u64, &stack[*sp + 16]);
assert_string_equals("Hashmap-based get/set", "world", result);
free(stack);
}
// Test 3: Same ID accesses same variable (deduplication test)
void test_same_id_same_variable()
{
printf("\n[TEST 3] Same ID = Same Variable\n");
char* stack = (char*) calloc(1, INITIAL_STACK_SIZE);
u32 sp_val = INITIAL_SP;
u32* sp = &sp_val;
u32 oldSP;
// Set variable with ID = 5
PUSH_STR_ID("first_name", 10, 5);
PUSH_STR_ID("value1", 6, 0);
actionSetVariable(stack, sp);
// Access same variable with different name but same ID
PUSH_STR_ID("completely_different_name", 25, 5); // Same ID!
actionGetVariable(stack, sp);
// Should get the same variable value
char* result = (char*) VAL(u64, &stack[*sp + 16]);
assert_string_equals("Same ID = same variable", "value1", result);
free(stack);
}
// Test 4: String materialization still works
void test_string_materialization()
{
printf("\n[TEST 4] String Materialization (STR_LIST → heap)\n");
char* stack = (char*) calloc(1, INITIAL_STACK_SIZE);
u32 sp_val = INITIAL_SP;
u32* sp = &sp_val;
u32 oldSP;
// Create a STR_LIST manually
char* strings[] = {"Hello ", "World", "!"};
u32 total_len = 12;
(*sp) -= 4 + 4 + 8 + sizeof(u64) * 4;
(*sp) &= ~7;
stack[*sp] = ACTION_STACK_VALUE_STR_LIST;
VAL(u32, &stack[*sp + 4]) = 0; // String ID = 0 for STR_LIST
VAL(u32, &stack[*sp + 8]) = total_len;
u64* str_list = (u64*) &stack[*sp + 16];
str_list[0] = 3; // Number of strings
str_list[1] = (u64)strings[0];
str_list[2] = (u64)strings[1];
str_list[3] = (u64)strings[2];
// Push variable name
u32 old_sp = *sp;
PUSH_STR_ID("concat_var", 10, 7);
// Swap to get [value] [name] order
u32 temp_sp = VAL(u32, &stack[*sp + 4]);
VAL(u32, &stack[*sp + 4]) = temp_sp;
VAL(u32, &stack[temp_sp + 4]) = *sp;
*sp = temp_sp;
// Set variable (should materialize STR_LIST to heap)
actionSetVariable(stack, sp);
// Get it back
PUSH_STR_ID("concat_var", 10, 7);
actionGetVariable(stack, sp);
// Check materialized result
char* result = (char*) VAL(u64, &stack[*sp + 16]);
assert_string_equals("STR_LIST materialization", "Hello World!", result);
free(stack);
}
// Test 5: Performance comparison
void test_performance()
{
printf("\n[TEST 5] Performance Comparison\n");
char* stack = (char*) calloc(1, INITIAL_STACK_SIZE);
u32 sp;
u32 oldSP;
clock_t start, end;
double cpu_time_array, cpu_time_hashmap;
// Setup: Create a variable with ID = 3
sp = INITIAL_SP;
PUSH_STR_ID("test_var", 8, 3);
PUSH_STR_ID("value", 5, 0);
actionSetVariable(stack, sp);
// Test 1: Array-based access (ID = 3)
sp = INITIAL_SP;
start = clock();
for (int i = 0; i < 100000; i++)
{
PUSH_STR_ID("test_var", 8, 3);
actionGetVariable(stack, sp);
POP();
}
end = clock();
cpu_time_array = ((double) (end - start)) / CLOCKS_PER_SEC;
printf(" Array-based: 100K accesses in %.4f seconds\n", cpu_time_array);
// Test 2: Hashmap-based access (ID = 0)
sp = INITIAL_SP;
start = clock();
for (int i = 0; i < 100000; i++)
{
PUSH_STR("test_var", 8); // ID = 0
actionGetVariable(stack, sp);
POP();
}
end = clock();
cpu_time_hashmap = ((double) (end - start)) / CLOCKS_PER_SEC;
printf(" Hashmap-based: 100K accesses in %.4f seconds\n", cpu_time_hashmap);
double speedup = cpu_time_hashmap / cpu_time_array;
printf(" Speedup: %.2fx faster with array-based access\n", speedup);
assert_true("Array is faster than hashmap", speedup > 1.0);
free(stack);
}
int main()
{
printf("==========================================================\n");
printf(" String ID Optimization - Test Suite\n");
printf("==========================================================\n");
// Initialize both storage systems
printf("\nInitializing variable storage...\n");
printf(" Max String ID: 10\n");
heap_init(app_context, 1024*1024*1024);
initVarArray(app_context, 10);
initMap();
// Run all tests
test_array_based_variable();
test_hashmap_based_variable();
test_same_id_same_variable();
test_string_materialization();
test_performance();
// Cleanup
freeMap();
heap_shutdown(app_context);
// Print results
printf("\n==========================================================\n");
printf(" Test Results\n");
printf("==========================================================\n");
printf(" Total: %d\n", results.total);
printf(" Passed: %d\n", results.passed);
printf(" Failed: %d\n", results.failed);
printf("==========================================================\n");
if (results.failed == 0)
{
printf("\n✓ All tests passed!\n\n");
return 0;
}
else
{
printf("\n✗ Some tests failed!\n\n");
return 1;
}
}