eab3e02199
This commit refactors the internal representation of `BoundKind::Record` to store a `RecordLayout` and a `Vec` of values, rather than a `Vec` of key-value pairs. This change simplifies the representation and improves efficiency by decoupling the record's structure from its specific values during compilation and analysis. The `RecordLayout` now defines the structure of the record, and the values are stored in a separate vector, ordered according to the layout. This allows for better optimization and type checking, as the record's shape is explicitly defined and immutable once created.
50 lines
1.4 KiB
Plaintext
50 lines
1.4 KiB
Plaintext
;; Benchmark: 1.3us
|
|
;; Benchmark-Repeat: 1599
|
|
;; Output: ["Alice" 101 :admin "Zürich" ["Alice" "Bob"] true]
|
|
|
|
; ---------------------------------------------------------
|
|
; Records & Optimized Field Access Showcase
|
|
; ---------------------------------------------------------
|
|
|
|
(do
|
|
; 1. Record Creation
|
|
; Records use interned layouts for O(1) access and low memory overhead.
|
|
(def user {:id 101 :name "Alice" :role :admin})
|
|
|
|
; 2. Optimized Field Access
|
|
; These calls are transformed by the optimizer into specialized instructions.
|
|
(def name (.name user))
|
|
(def id (.id user))
|
|
|
|
; 3. First-Class Field Accessors
|
|
; Accessors start with a dot and can be passed around like functions.
|
|
(def get-role .role)
|
|
(def role (get-role user))
|
|
|
|
; 4. Nested Records
|
|
(def company {
|
|
:name "Myc Corp"
|
|
:location {:city "Zürich" :zip 8000}
|
|
:employees [user {:id 102 :name "Bob" :role :dev}]
|
|
})
|
|
|
|
; Deep access (also optimized)
|
|
(def city (.city (.location company)))
|
|
|
|
; 5. Higher-Order usage
|
|
; Accessors are perfect for mapping over data.
|
|
(def names ((fn [f list]
|
|
(do
|
|
(def [e1 e2] list)
|
|
[(f e1) (f e2)]
|
|
))
|
|
.name (.employees company)))
|
|
|
|
; 6. Structural Identity
|
|
; Records with the same layout share memory; equality checks values.
|
|
(def is-equal (= {:x 1 :y 2} {:x 1 :y 2}))
|
|
|
|
; Return a summary of all tested features
|
|
[name id role city names is-equal]
|
|
)
|