From c9eed04b51a0da3ae4cc9cbf9c2a1dd815bbb5e4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Aug 2022 16:19:48 +0100 Subject: [PATCH] Add comment regarding the layout of a `map` to explain how it is iterated --- core/fmt/fmt.odin | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index e68280bfe..7429a093d 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -2059,18 +2059,33 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) { ed := runtime.type_info_base(gs.types[1]).variant.(runtime.Type_Info_Dynamic_Array) entry_type := ed.elem.variant.(runtime.Type_Info_Struct) entry_size := ed.elem_size + /* + NOTE: The layout of a `map` is as follows: + map[Key]Value + + ## Internal Layout + struct { + hashes: []int, + entries: [dynamic]struct{ + hash: uintptr, + next: int, + key: Key, + value: Value, + }, + } + */ for i in 0.. 0 { io.write_string(fi.writer, ", ", &fi.n) } data := uintptr(entries.data) + uintptr(i*entry_size) - key := data + entry_type.offsets[2] + key := data + entry_type.offsets[2] // key: Key fmt_arg(&Info{writer = fi.writer}, any{rawptr(key), info.key.id}, 'v') io.write_string(fi.writer, "=", &fi.n) - value := data + entry_type.offsets[3] + value := data + entry_type.offsets[3] // value: Value fmt_arg(fi, any{rawptr(value), info.value.id}, 'v') } }