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
//! OPT-022: Inline Caching Foundation - Hidden Class System
//!
//! EXTREME TDD: RED Phase - These tests MUST fail initially
//!
//! Goal: Implement hidden class system for 2-4x property access speedup
//!
//! References:
//! - Brunthaler (2010) - Inline Caching Meets Quickening
//! - Chambers et al. (1989) - An Efficient Implementation of SELF
//! - Hölzle et al. (1991) - Optimizing Dynamically-Typed Languages
//!
//! Expected Performance:
//! - Property access: 4-10x faster (monomorphic sites)
//! - Method dispatch: 2-5x faster
//! - Cache hit rate: 85-95%
//! - Overall: 2-4x speedup for property-heavy code
use assert_cmd::Command;
fn ruchy_cmd() -> Command {
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}
// ============================================================================
// Test 1: Hidden Class Creation - Objects get assigned hidden classes
// ============================================================================
#[test]
fn test_opt_022_01_hidden_class_creation() {
// Objects with same structure should share hidden class
let code = r#"
struct Point { x: i32, y: i32 }
fun main() {
let p1 = Point { x: 10, y: 20 }
let p2 = Point { x: 30, y: 40 }
// Internal check: p1 and p2 should share same hidden class
// This requires runtime introspection API
println("Points created")
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("Points created\n");
}
// ============================================================================
// Test 2: Hidden Class Transition - Adding properties creates new classes
// ============================================================================
#[test]
fn test_opt_022_02_hidden_class_transition() {
// Adding properties should transition to new hidden class
let code = r#"
class Point {
x: f64,
y: f64
}
fun main() {
let p = Point { x: 10.0, y: 20.0 }
// Original hidden class: [x, y]
println!("{}", p.x)
// Note: Dynamic property addition not currently supported in Ruchy
// This test documents expected behavior for future implementation
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("10.0\n");
}
// ============================================================================
// Test 3: Monomorphic Property Access - Single type at call site
// ============================================================================
#[test]
fn test_opt_022_03_monomorphic_property_access() {
// Property access at same site with same type = monomorphic = fast
let code = r#"
struct Rectangle {
width: i32,
height: i32,
fun area(&self) -> i32 {
self.width * self.height
}
}
fun main() {
let r1 = Rectangle { width: 10, height: 20 }
let r2 = Rectangle { width: 5, height: 8 }
let r3 = Rectangle { width: 3, height: 7 }
// All three calls access .area() on Rectangle
// Should be monomorphic (same hidden class)
println!("{}", r1.area())
println!("{}", r2.area())
println!("{}", r3.area())
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("200\n40\n21\n");
}
// ============================================================================
// Test 4: Polymorphic Property Access - Multiple types at call site
// ============================================================================
#[test]
fn test_opt_022_04_polymorphic_property_access() {
// Property access at same site with different types = polymorphic
let code = r#"
struct Rectangle {
width: i32,
height: i32,
fun area(&self) -> i32 {
self.width * self.height
}
}
struct Circle {
radius: f64,
fun area(&self) -> f64 {
3.14159 * self.radius * self.radius
}
}
fun main() {
let r = Rectangle { width: 10, height: 20 }
let c = Circle { radius: 5.0 }
// Polymorphic call site - two different types
println!("{}", r.area())
println!("{}", c.area())
}
"#;
ruchy_cmd().arg("-e").arg(code).assert().success();
}
// ============================================================================
// Test 5: Property Access Cache Hit - Repeated access to same property
// ============================================================================
#[test]
fn test_opt_022_05_property_cache_hit() {
// Accessing same property multiple times should hit inline cache
let code = r#"
struct Point {
x: i32,
y: i32
}
fun main() {
let p = Point { x: 10, y: 20 }
// All these accesses to p.x should hit the inline cache
let sum = p.x + p.x + p.x + p.x + p.x
println!("{}", sum)
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("50\n");
}
// ============================================================================
// Test 6: Method Dispatch Cache - Repeated method calls
// ============================================================================
#[test]
fn test_opt_022_06_method_dispatch_cache() {
// Method dispatch should be cached for performance
let code = r#"
struct Counter {
count: i32,
fun new() -> Counter {
Counter { count: 0 }
}
fun get(&self) -> i32 {
self.count
}
}
fun main() {
let c = Counter::new()
// Multiple calls to .get() should hit method dispatch cache
println!("{}", c.get())
println!("{}", c.get())
println!("{}", c.get())
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("0\n0\n0\n");
}
// ============================================================================
// Test 7: Cache Invalidation - Struct field mutation
// ============================================================================
#[test]
fn test_opt_022_07_cache_invalidation_on_mutation() {
// Mutating struct fields should invalidate caches appropriately
let code = r#"
struct Counter {
count: i32,
fun new() -> Counter {
Counter { count: 0 }
}
fun increment(&mut self) {
self.count += 1
}
fun get(&self) -> i32 {
self.count
}
}
fun main() {
let mut c = Counter::new()
println!("{}", c.get()) // 0
c.increment()
println!("{}", c.get()) // 1
c.increment()
println!("{}", c.get()) // 2
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("0\n1\n2\n");
}
// ============================================================================
// Test 8: Hidden Class Shape Consistency - Property order matters
// ============================================================================
#[test]
fn test_opt_022_08_hidden_class_property_order() {
// Objects with same properties in different order = different hidden classes
// Note: Rust/Ruchy structs have fixed property order, so this tests
// that the hidden class system respects declaration order
let code = r#"
struct PointXY {
x: i32,
y: i32
}
struct PointYX {
y: i32,
x: i32
}
fun main() {
let p1 = PointXY { x: 10, y: 20 }
let p2 = PointYX { y: 20, x: 10 }
// Different struct types = different hidden classes
println!("{} {}", p1.x, p1.y)
println!("{} {}", p2.x, p2.y)
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("10 20\n10 20\n");
}
// ============================================================================
// Test 9: Performance Baseline - Property access timing
// ============================================================================
#[test]
fn test_opt_022_09_property_access_performance() {
// Baseline: How fast is property access WITHOUT inline caching?
// This establishes the performance to beat
let code = r#"
struct Point {
x: i32,
y: i32,
z: i32
}
fun main() {
let p = Point { x: 10, y: 20, z: 30 }
// Access properties 10,000 times
let mut sum = 0
let mut i = 0
while i < 10000 {
sum = sum + p.x + p.y + p.z
i = i + 1
}
println!("{}", sum)
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("600000\n");
}
// ============================================================================
// Test 10: Cache Statistics - Monitoring cache effectiveness
// ============================================================================
#[test]
fn test_opt_022_10_cache_statistics() {
// System should track cache hit/miss statistics
// Target: 85-95% hit rate
let code = r#"
struct Point {
x: i32,
y: i32
}
fun main() {
let p1 = Point { x: 10, y: 20 }
let p2 = Point { x: 30, y: 40 }
// Monomorphic access pattern (should have high cache hit rate)
let s1 = p1.x + p1.y
let s2 = p2.x + p2.y
let s3 = p1.x + p1.y
println!("{} {} {}", s1, s2, s3)
// TODO: Add introspection API to check cache statistics
// Expected: >90% hit rate for this code
}
"#;
ruchy_cmd()
.arg("-e")
.arg(code)
.assert()
.success()
.stdout("30 70 30\n");
}