Skip to content

Commit 10146b0

Browse files
authored
chore: misc keymap.nr refactor (#76)
1 parent 8c9d7fc commit 10146b0

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

src/keymap.nr

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::_comparison_tools::lt::assert_lte_240_bit;
2-
use crate::_comparison_tools::lt::lt_field_16_bit;
32
use crate::_comparison_tools::lt::lte_field_240_bit;
43
use crate::json::JSON;
54
use crate::json_entry::{JSONEntry, JSONEntryPacked};
@@ -62,6 +61,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
6261
let KeyIndexData { json_index, json_length, parent_id, array_index } =
6362
KeyIndexData::from_field(self.key_data[i]);
6463
let hash = hasher.get_keyhash(self.json_packed, json_index, json_length);
64+
//ensures hash:0-199 bits, array_index:200-215 bits, parent_id: 216-239 bits
6565
hashlist[i] = hash + array_index * two_pow_200 + parent_id * two_pow_216;
6666
}
6767

@@ -72,69 +72,60 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7272

7373
let mut sorted_entries: [JSONEntryPacked; MaxNumValues] =
7474
[JSONEntryPacked::default(); MaxNumValues];
75+
7576
for i in 0..MaxNumValues {
7677
sorted_entries[sort_result.sort_indices[i]] = self.json_entries_packed[i];
7778
}
7879

79-
let mut ids: [Field; MaxNumValues] = [0; MaxNumValues];
80-
let mut parent_indices: [Field; MaxNumValues] = [0; MaxNumValues];
81-
let mut entry_types: [Field; MaxNumValues] = [0; MaxNumValues];
80+
let mut parent_indices: [u32; MaxNumValues] = [0; MaxNumValues];
8281

82+
let mut identity_to_json_map: [u32; MaxNumValues] = [0; MaxNumValues];
8383
for i in 0..MaxNumValues {
8484
// 11.75 + 3.5 = 15.25 gates per iteration
8585
let (id, parent_index, entry_type) = JSONEntry::extract_entry_type_id_and_parent_index_from_field(
8686
sorted_entries[i].value,
8787
);
88-
ids[i] = id;
89-
parent_indices[i] = parent_index;
90-
entry_types[i] = entry_type;
91-
}
92-
93-
let mut identity_to_json_map: [Field; MaxNumValues] = [0; MaxNumValues];
94-
// 6.5 gates per iteration
95-
for i in 0..MaxNumValues {
96-
let id = ids[i];
97-
let entry_type = entry_types[i];
88+
parent_indices[i] = cast_num_to_u32(parent_index);
9889
// 2 gates
90+
// update is 1 for end of object/array, 0 for other
9991
let update = TOKEN_ENDS_OBJECT_OR_ARRAY[cast_num_to_u32(entry_type)];
10092
// NOTE THIS RELIES ON MaxNumValues ACTUALLY DESCRIBING NUMMaxNumValues + 1
10193
let index = if update {
102-
id
94+
cast_num_to_u32(id)
10395
} else {
104-
(MaxNumValues as Field - 1)
96+
MaxNumValues - 1
10597
};
10698
// 3.5 gates
107-
identity_to_json_map[cast_num_to_u32(index)] = i as Field;
99+
identity_to_json_map[index] = i;
108100
}
109-
110101
// 13.5 gates per iteration
111102
let mut parent_identity_pre = parent_indices[0];
112103
for i in 1..MaxNumValues {
113104
let parent_identity_post = parent_indices[i];
114105
// if the parent identity changes,
115106
// 3.5 gate
116-
// the list is sorted according to parent_ideneity,
117-
// n.b. parent_identity_post - parent_identity_pre is not neccessarily 0 or 1 (can be larger)
107+
// the list is sorted according to parent_identity,
108+
// n.b. parent_identity_post - parent_identity_pre is not necessarily 0 or 1 (can be larger)
118109
// due to empty objects and arrays increasing identity value without creating associated child json entries
119-
let new_parent = lt_field_16_bit(parent_identity_pre, parent_identity_post) as Field;
120-
// let new_parent = (parent_identity_post as u32 > parent_identity_pre as u32) as Field;
110+
let new_parent = parent_identity_pre < parent_identity_post;
121111
// 3.5 gates
122-
let index_of_parent = identity_to_json_map[cast_num_to_u32(parent_identity_post)];
112+
let index_of_parent = identity_to_json_map[parent_identity_post];
123113
// 1 gate + 3.5 gates
124114
let updated = JSONEntry::add_child_pointer_into_field(
125-
sorted_entries[cast_num_to_u32(index_of_parent)].value,
115+
sorted_entries[index_of_parent].value,
126116
i as Field,
127117
);
128118

129119
// RELIES ON THE SMALLEST ENTRY IN THE SORTED LIST BEING EMPTY
130120
// 1 gate
131-
let index = (index_of_parent * new_parent);
121+
let index = if new_parent { index_of_parent } else { 0 };
132122
// 3.5 gates
133-
sorted_entries[cast_num_to_u32(index)] = JSONEntryPacked { value: updated };
123+
//index is just 0 if new_parent is false, so sorted_entries[0] is useless info
124+
sorted_entries[index] = JSONEntryPacked { value: updated };
134125

135126
parent_identity_pre = parent_identity_post;
136127
}
137-
sorted_entries[0] = JSONEntryPacked::default(); // TODO document why we want to always make 0 a dead entry
128+
sorted_entries[0] = JSONEntryPacked::default();
138129
self.unsorted_json_entries_packed = self.json_entries_packed;
139130
self.json_entries_packed = sorted_entries;
140131
self.key_hashes = sort_result.sorted;

0 commit comments

Comments
 (0)