From 5d71d3cd1863bf47c17d53b57efeaccded32fe2c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 11 Jun 2016 09:38:14 -0700 Subject: 3045 - generalize core refcounting data structure --- 030container.cc | 4 +-- 036refcount.cc | 91 ++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/030container.cc b/030container.cc index 8c570498..e2cd857e 100644 --- a/030container.cc +++ b/030container.cc @@ -92,14 +92,12 @@ def main [ ] +mem: storing 0 in location 7 -//: Global data structure for container metadata. //: Can't put this in type_info because later layers will add support for more //: complex type trees where metadata depends on *combinations* of types. - :(before "struct reagent") struct container_metadata { int size; - vector offset; + vector offset; // not used by exclusive containers // End container_metadata Fields container_metadata() :size(0) { // End container_metadata Constructor diff --git a/036refcount.cc b/036refcount.cc index bd5dccfd..34a59f6d 100644 --- a/036refcount.cc +++ b/036refcount.cc @@ -222,12 +222,49 @@ struct address_element_info { return *this; } }; -:(before "struct container_metadata ") -// valid fields for containers: size, offset, address, maybe_address (if container directly or indirectly contains exclusive containers with addresses) -// valid fields for exclusive containers: size, maybe_address + +// For exclusive containers we might sometimes have an address at some offset +// if some other offset has a specific tag. This struct encapsulates such +// guards. +struct tag_condition_info { + int offset; + int tag; + tag_condition_info(int o, int t) :offset(o), tag(t) {} +}; + :(before "End container_metadata Fields") -vector address; // list of offsets containing addresses, and the sizes of their corresponding payloads -map, vector > maybe_address; +// a list of facts of the form: +// +// IF offset o1 has tag t2 AND offset o2 has tag t2 AND .., THEN +// for all address_element_infos: +// you need to update refcounts for the address at offset pointing to a payload of type payload_type (just in case we need to abandon something in the process) +map, set > address; +:(code) +bool operator<(const set& a, const set& b) { + if (a.size() != b.size()) return a.size() < b.size(); + for (set::const_iterator pa = a.begin(), pb = b.begin(); pa != a.end(); ++pa, ++pb) { + if (pa->offset != pb->offset) return pa->offset < pb->offset; + if (pa->tag != pb->tag) return pa->tag < pb->tag; + } + return false; // equal +} +bool operator<(const tag_condition_info& a, const tag_condition_info& b) { + if (a.offset != b.offset) return a.offset < b.offset; + if (a.tag != b.tag) return a.tag < b.tag; + return false; // equal +} +bool operator<(const set& a, const set& b) { + if (a.size() != b.size()) return a.size() < b.size(); + for (set::const_iterator pa = a.begin(), pb = b.begin(); pa != a.end(); ++pa, ++pb) { + if (pa->offset != pb->offset) return pa->offset < pb->offset; + } + return false; // equal +} +bool operator<(const address_element_info& a, const address_element_info& b) { + if (a.offset != b.offset) return a.offset < b.offset; + return false; // equal +} + //: populate metadata.address in a separate transform, because it requires //: already knowing the sizes of all types @@ -263,22 +300,24 @@ void compute_container_address_offsets(type_tree* type) { container_metadata& metadata = get(Container_metadata, type); if (!metadata.address.empty()) return; trace(9994, "transform") << "compute address offsets for container " << info.name << end(); - append_addresses(0, type, metadata.address, metadata); + append_addresses(0, type, metadata.address, set()); } if (info.kind == EXCLUSIVE_CONTAINER) { container_metadata& metadata = get(Container_metadata, type); - if (!metadata.maybe_address.empty()) return; trace(9994, "transform") << "compute address offsets for exclusive container " << info.name << end(); - for (int tag = 0; tag < SIZE(info.elements); ++tag) - append_addresses(/*skip tag offset*/1, variant_type(type, tag).type, get_or_insert(metadata.maybe_address, pair(/*tag offset*/0, tag)), metadata); + for (int tag = 0; tag < SIZE(info.elements); ++tag) { + set key; + key.insert(tag_condition_info(/*tag is at offset*/0, tag)); + append_addresses(/*skip tag offset*/1, variant_type(type, tag).type, metadata.address, key); + } } } -void append_addresses(int base_offset, const type_tree* type, vector& out, container_metadata& out_metadata) { +void append_addresses(int base_offset, const type_tree* type, map, set >& out, const set& key) { const type_info& info = get(Type, type->value); if (type->name == "address") { assert(type->right && type->right->name != "array"); // array types can't be handled without a full reagent and its value - out.push_back(address_element_info(base_offset, new type_tree(*type->right))); + get_or_insert(out, key).insert(address_element_info(base_offset, new type_tree(*type->right))); return; } if (info.kind == PRIMITIVE) return; @@ -288,19 +327,21 @@ void append_addresses(int base_offset, const type_tree* type, vectorright))); + get_or_insert(out, key).insert(address_element_info(curr_offset, new type_tree(*element.type->right))); ++curr_offset; } else if (is_mu_container(element)) { - append_addresses(curr_offset, element.type, out, out_metadata); + append_addresses(curr_offset, element.type, out, key); curr_offset += size_of(element); } else if (is_mu_exclusive_container(element)) { const type_info& element_info = get(Type, element.type->value); for (int tag = 0; tag < SIZE(element_info.elements); ++tag) { - vector& tmp = get_or_insert(out_metadata.maybe_address, pair(curr_offset, tag)); + set new_key = key; + new_key.insert(tag_condition_info(curr_offset, tag)); + set& tmp = get_or_insert(out, new_key); if (tmp.empty()) - append_addresses(curr_offset+1, variant_type(element.type, tag).type, tmp, out_metadata); + append_addresses(curr_offset+/*skip tag*/1, variant_type(element.type, tag).type, out, new_key); } curr_offset += size_of(element); } @@ -341,17 +382,19 @@ if (is_mu_container(product) || is_mu_exclusive_container(product)) { void update_container_refcounts(const reagent& x, const vector& data) { assert(is_mu_container(x) || is_mu_exclusive_container(x)); const container_metadata& metadata = get(Container_metadata, x.type); - for (int i = 0; i < SIZE(metadata.address); ++i) { - const address_element_info& info = metadata.address.at(i); - update_refcounts(get_or_insert(Memory, x.value + info.offset), data.at(info.offset), info.payload_type, size_of(info.payload_type)+/*refcount*/1); + for (map, set >::const_iterator p = metadata.address.begin(); p != metadata.address.end(); ++p) { + if (!all_match(data, p->first)) continue; + for (set::const_iterator info = p->second.begin(); info != p->second.end(); ++info) + update_refcounts(get_or_insert(Memory, x.value + info->offset), data.at(info->offset), info->payload_type, size_of(info->payload_type)+/*refcount*/1); } - for (map, vector >::const_iterator p = metadata.maybe_address.begin(); p != metadata.maybe_address.end(); ++p) { - if (data.at(p->first.first) != p->first.second) continue; - for (int i = 0; i < SIZE(p->second); ++i) { - const address_element_info& info = p->second.at(i); - update_refcounts(get_or_insert(Memory, x.value + info.offset), data.at(info.offset), info.payload_type, size_of(info.payload_type)+/*refcount*/1); - } +} + +bool all_match(const vector& data, const set& conditions) { + for (set::const_iterator p = conditions.begin(); p != conditions.end(); ++p) { + if (data.at(p->offset) != p->tag) + return false; } + return true; } :(scenario refcounts_put_container) -- cgit 1.4.1-2-gfad0 ='n176' href='#n176'>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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412