Skip to content

Commit 34c5d0d

Browse files
authored
Merge pull request #3 from PassiveLogic/feat/per-class-identity-mode
feat: Add per-class identityMode via @js macro parameter
2 parents f562fcb + b47bd9c commit 34c5d0d

115 files changed

Lines changed: 2472 additions & 455 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Benchmarks/README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,35 @@ node run.js --filter=/string/i
3838

3939
## Identity Mode Benchmarks
4040

41-
Compare `identityMode: "pointer"` vs default (`"none"`) for SwiftHeapObject wrapper caching. Requires `--expose-gc` for memory benchmarks.
41+
Compare `identityMode: "pointer"` vs default (`"none"`) for SwiftHeapObject wrapper caching. Both class variants are compiled into the **same build** via per-class `@JS(identityMode: true)` annotations, so no separate builds or config files are needed.
42+
43+
Requires `--expose-gc` for memory benchmarks.
44+
45+
### Build once
46+
47+
```bash
48+
swift package --swift-sdk $SWIFT_SDK_ID js -c release
49+
```
50+
51+
### Compare both modes in one run
4252

4353
```bash
44-
# Run identity benchmarks comparing both modes
45-
node --expose-gc run.js --identity-mode=both
54+
node --expose-gc run.js --identity-mode=both --identity-iterations=1000000
55+
```
56+
57+
### Run only one mode
4658

47-
# Pointer mode only
48-
node --expose-gc run.js --identity-mode=pointer
59+
```bash
60+
# Only pointer (identity-cached) classes
61+
node --expose-gc run.js --identity-mode=pointer --identity-iterations=1000000
4962

63+
# Only non-identity classes
64+
node --expose-gc run.js --identity-mode=none --identity-iterations=1000000
65+
```
66+
67+
### Additional options
68+
69+
```bash
5070
# Control iteration count (default: 1000000)
5171
node --expose-gc run.js --identity-mode=both --identity-iterations=500000
5272

@@ -59,8 +79,8 @@ node --expose-gc run.js --identity-mode=both --identity-memory
5979
# Combine with adaptive sampling
6080
node --expose-gc run.js --identity-mode=both --adaptive
6181

62-
# Save identity results
63-
node --expose-gc run.js --identity-mode=both --output=results/identity-mode/results.json
82+
# Filter to specific identity benchmarks
83+
node --expose-gc run.js --adaptive --filter=passBothWaysRoundtrip --identity-mode=both --identity-iterations=1000000
6484
```
6585

6686
### Identity Mode Scenarios

Benchmarks/Sources/Benchmarks.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,61 @@ nonisolated(unsafe) var _cachedPool: [SimpleClass] = []
305305
}
306306
}
307307

308+
// MARK: - Identity Mode Benchmark Variants
309+
// These classes use @JS(identityMode: true) so that identity cache benchmarks
310+
// can run in the SAME build alongside the non-identity classes above.
311+
312+
@JS(identityMode: true)
313+
class SimpleClassIdentity {
314+
@JS var name: String
315+
@JS var count: Int
316+
@JS var flag: Bool
317+
@JS var rate: Float
318+
@JS var precise: Double
319+
320+
@JS init(name: String, count: Int, flag: Bool, rate: Float, precise: Double) {
321+
self.name = name
322+
self.count = count
323+
self.flag = flag
324+
self.rate = rate
325+
self.precise = precise
326+
}
327+
}
328+
329+
@JS(identityMode: true)
330+
class ClassRoundtripIdentity {
331+
@JS init() {}
332+
333+
@JS func roundtripSimpleClassIdentity(_ obj: SimpleClassIdentity) -> SimpleClassIdentity {
334+
return obj
335+
}
336+
337+
@JS func makeSimpleClassIdentity() -> SimpleClassIdentity {
338+
return SimpleClassIdentity(name: "Hello", count: 42, flag: true, rate: 0.5, precise: 3.14159)
339+
}
340+
341+
@JS func takeSimpleClassIdentity(_ obj: SimpleClassIdentity) {
342+
// consume without returning
343+
}
344+
}
345+
346+
nonisolated(unsafe) var _cachedPoolIdentity: [SimpleClassIdentity] = []
347+
348+
@JS(identityMode: true)
349+
class IdentityCacheBenchmarkIdentity {
350+
@JS init() {}
351+
352+
@JS func setupPool(_ count: Int) {
353+
_cachedPoolIdentity = (0..<count).map {
354+
SimpleClassIdentity(name: "Item \($0)", count: $0, flag: true, rate: 0.5, precise: 3.14)
355+
}
356+
}
357+
358+
@JS func getPoolRepeated() -> [SimpleClassIdentity] {
359+
return _cachedPoolIdentity
360+
}
361+
}
362+
308363
// MARK: - Array Performance Tests
309364

310365
@JS struct Point {

0 commit comments

Comments
 (0)