Skip to content

Commit 672d080

Browse files
committed
Add a way for the meta-object to cache values unless the method table changes.
1 parent 3ff2502 commit 672d080

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/how/NQPClassHOW.pm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ knowhow NQPClassHOW {
3030

3131
# Full list of roles that we do.
3232
has @!done;
33+
34+
# Cached values, which are thrown away if the class changes.
35+
has %!cache;
3336

3437
# Parrot-specific vtable mapping hash. Maps vtable name to method.
3538
has %!parrot_vtable_mapping;
@@ -72,6 +75,7 @@ knowhow NQPClassHOW {
7275
pir::die("Cannot add a null method '$name' to class '$!name'");
7376
}
7477
pir::set_method_cache_authoritativeness__vPi($obj, 0);
78+
%!cache := {};
7579
%!methods{$name} := $code_obj;
7680
}
7781

@@ -393,6 +397,7 @@ knowhow NQPClassHOW {
393397
pir::stable_publish_vtable_handler_mapping__vPP($obj, %mapping);
394398
}
395399
}
400+
396401
##
397402
## Introspecty
398403
##
@@ -492,4 +497,14 @@ knowhow NQPClassHOW {
492497
}
493498
pir::null__P()
494499
}
500+
501+
##
502+
## Cache-related
503+
##
504+
method cache($obj, $key, $value_generator) {
505+
%!cache || (%!cache := {});
506+
pir::exists(%!cache, $key) ??
507+
%!cache{$key} !!
508+
(%!cache{$key} := $value_generator())
509+
}
495510
}

0 commit comments

Comments
 (0)