2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2019-03-31 15:01:46 +02:00
|
|
|
use rustc_data_structures::{fx::FxHashMap, sync::Lock};
|
2018-04-01 08:17:25 +02:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
use std::cell::{RefCell, Cell};
|
2015-01-20 15:45:07 -08:00
|
|
|
use std::fmt::Debug;
|
2019-03-31 15:01:46 +02:00
|
|
|
use std::hash::Hash;
|
2016-08-23 13:23:58 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2014-11-10 12:27:56 -08:00
|
|
|
|
2017-07-19 15:04:30 -06:00
|
|
|
use std::sync::mpsc::{Sender};
|
2017-09-16 21:43:05 +03:00
|
|
|
use syntax_pos::{SpanData};
|
2019-05-17 15:21:31 +10:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::dep_graph::{DepNode};
|
|
|
|
use crate::session::Session;
|
2017-07-19 15:04:30 -06:00
|
|
|
|
2019-08-01 03:35:26 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// The name of the associated type for `Fn` return types.
|
2019-05-17 15:21:31 +10:00
|
|
|
pub const FN_OUTPUT_NAME: Symbol = sym::Output;
|
2015-01-10 11:54:15 -05:00
|
|
|
|
2014-11-07 16:26:26 -05:00
|
|
|
// Useful type to use with `Result<>` indicate that an error has already
|
|
|
|
// been reported to the user, so no need to continue checking.
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2014-11-08 06:59:10 -05:00
|
|
|
pub struct ErrorReported;
|
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
|
|
|
|
|
2017-07-19 15:04:30 -06:00
|
|
|
/// Parameters to the `Dump` variant of type `ProfileQueriesMsg`.
|
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
pub struct ProfQDumpParams {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// A base path for the files we will dump.
|
2017-07-19 15:04:30 -06:00
|
|
|
pub path:String,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// To ensure that the compiler waits for us to finish our dumps.
|
2017-07-19 15:04:30 -06:00
|
|
|
pub ack:Sender<()>,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Toggle dumping a log file with every `ProfileQueriesMsg`.
|
2017-07-19 15:04:30 -06:00
|
|
|
pub dump_profq_msg_log:bool,
|
|
|
|
}
|
|
|
|
|
2018-08-29 08:21:01 -05:00
|
|
|
#[allow(nonstandard_style)]
|
2018-04-19 02:33:24 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct QueryMsg {
|
|
|
|
pub query: &'static str,
|
|
|
|
pub msg: Option<String>,
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:04:30 -06:00
|
|
|
/// A sequence of these messages induce a trace of query-based incremental compilation.
|
2019-02-28 22:43:53 +00:00
|
|
|
// FIXME(matthewhammer): Determine whether we should include cycle detection here or not.
|
2017-07-19 15:04:30 -06:00
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
pub enum ProfileQueriesMsg {
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Begin a timed pass.
|
2017-07-21 12:32:38 -06:00
|
|
|
TimeBegin(String),
|
2019-02-28 22:43:53 +00:00
|
|
|
/// End a timed pass.
|
2017-07-21 12:32:38 -06:00
|
|
|
TimeEnd,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Begin a task (see `dep_graph::graph::with_task`).
|
2017-07-23 10:02:07 -06:00
|
|
|
TaskBegin(DepNode),
|
2019-02-28 22:43:53 +00:00
|
|
|
/// End a task.
|
2017-07-23 10:02:07 -06:00
|
|
|
TaskEnd,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Begin a new query.
|
|
|
|
/// Cannot use `Span` because queries are sent to other thread.
|
2017-09-16 21:43:05 +03:00
|
|
|
QueryBegin(SpanData, QueryMsg),
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Query is satisfied by using an already-known value for the given key.
|
2017-07-19 15:04:30 -06:00
|
|
|
CacheHit,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Query requires running a provider; providers may nest, permitting queries to nest.
|
2017-07-19 15:04:30 -06:00
|
|
|
ProviderBegin,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Query is satisfied by a provider terminating with a value.
|
2017-07-19 15:04:30 -06:00
|
|
|
ProviderEnd,
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Dump a record of the queries to the given path.
|
2017-07-19 15:04:30 -06:00
|
|
|
Dump(ProfQDumpParams),
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Halt the profiling/monitoring background thread.
|
2017-07-19 15:04:30 -06:00
|
|
|
Halt
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// If enabled, send a message to the profile-queries thread.
|
2017-12-03 14:21:23 +01:00
|
|
|
pub fn profq_msg(sess: &Session, msg: ProfileQueriesMsg) {
|
|
|
|
if let Some(s) = sess.profile_channel.borrow().as_ref() {
|
|
|
|
s.send(msg).unwrap()
|
|
|
|
} else {
|
2019-02-28 22:43:53 +00:00
|
|
|
// Do nothing.
|
2017-12-03 14:21:23 +01:00
|
|
|
}
|
2017-07-19 15:04:30 -06:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Set channel for profile queries channel.
|
2017-12-03 14:21:23 +01:00
|
|
|
pub fn profq_set_chan(sess: &Session, s: Sender<ProfileQueriesMsg>) -> bool {
|
|
|
|
let mut channel = sess.profile_channel.borrow_mut();
|
|
|
|
if channel.is_none() {
|
|
|
|
*channel = Some(s);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2017-07-19 15:04:30 -06:00
|
|
|
}
|
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
/// Read the current depth of `time()` calls. This is used to
|
|
|
|
/// encourage indentation across threads.
|
|
|
|
pub fn time_depth() -> usize {
|
|
|
|
TIME_DEPTH.with(|slot| slot.get())
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Sets the current depth of `time()` calls. The idea is to call
|
2016-12-27 21:35:34 -05:00
|
|
|
/// `set_time_depth()` with the result from `time_depth()` in the
|
|
|
|
/// parent thread.
|
|
|
|
pub fn set_time_depth(depth: usize) {
|
|
|
|
TIME_DEPTH.with(|slot| slot.set(depth));
|
|
|
|
}
|
|
|
|
|
2017-12-03 14:21:23 +01:00
|
|
|
pub fn time<T, F>(sess: &Session, what: &str, f: F) -> T where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
|
|
|
time_ext(sess.time_passes(), Some(sess), what, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn time_ext<T, F>(do_it: bool, sess: Option<&Session>, what: &str, f: F) -> T where
|
2015-06-25 10:07:01 -07:00
|
|
|
F: FnOnce() -> T,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-06-25 10:07:01 -07:00
|
|
|
if !do_it { return f(); }
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2016-12-27 21:35:34 -05:00
|
|
|
let old = TIME_DEPTH.with(|slot| {
|
2014-11-14 14:20:57 -08:00
|
|
|
let r = slot.get();
|
|
|
|
slot.set(r + 1);
|
|
|
|
r
|
|
|
|
});
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2017-12-03 14:21:23 +01:00
|
|
|
if let Some(sess) = sess {
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
profq_msg(sess, ProfileQueriesMsg::TimeBegin(what.to_string()))
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 17:31:49 -08:00
|
|
|
let start = Instant::now();
|
|
|
|
let rv = f();
|
|
|
|
let dur = start.elapsed();
|
2017-12-03 14:21:23 +01:00
|
|
|
if let Some(sess) = sess {
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
profq_msg(sess, ProfileQueriesMsg::TimeEnd)
|
|
|
|
}
|
|
|
|
}
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2019-06-25 13:52:07 +10:00
|
|
|
print_time_passes_entry(true, what, dur);
|
2017-07-31 14:51:47 +02:00
|
|
|
|
|
|
|
TIME_DEPTH.with(|slot| slot.set(old));
|
|
|
|
|
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
|
|
|
|
if !do_it {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let indentation = TIME_DEPTH.with(|slot| slot.get());
|
|
|
|
|
2015-06-27 18:37:33 -07:00
|
|
|
let mem_string = match get_resident() {
|
|
|
|
Some(n) => {
|
|
|
|
let mb = n as f64 / 1_000_000.0;
|
|
|
|
format!("; rss: {}MB", mb.round() as usize)
|
|
|
|
}
|
2018-08-23 10:14:52 +02:00
|
|
|
None => String::new(),
|
2015-06-27 18:37:33 -07:00
|
|
|
};
|
2016-08-23 13:23:58 -04:00
|
|
|
println!("{}time: {}{}\t{}",
|
2018-04-01 13:48:15 +09:00
|
|
|
" ".repeat(indentation),
|
2016-08-23 13:23:58 -04:00
|
|
|
duration_to_secs_str(dur),
|
|
|
|
mem_string,
|
|
|
|
what);
|
2013-03-21 18:50:02 +09:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-08-23 13:23:58 -04:00
|
|
|
// Hack up our own formatting for the duration to make it easier for scripts
|
|
|
|
// to parse (always use the same number of decimal places and the same unit).
|
|
|
|
pub fn duration_to_secs_str(dur: Duration) -> String {
|
|
|
|
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
|
|
|
|
let secs = dur.as_secs() as f64 +
|
|
|
|
dur.subsec_nanos() as f64 / NANOS_PER_SEC;
|
|
|
|
|
|
|
|
format!("{:.3}", secs)
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:37:39 -04:00
|
|
|
pub fn to_readable_str(mut val: usize) -> String {
|
|
|
|
let mut groups = vec![];
|
|
|
|
loop {
|
|
|
|
let group = val % 1000;
|
|
|
|
|
|
|
|
val /= 1000;
|
|
|
|
|
|
|
|
if val == 0 {
|
2018-07-27 11:11:18 +02:00
|
|
|
groups.push(group.to_string());
|
2017-03-13 02:12:13 +02:00
|
|
|
break;
|
2016-11-04 11:37:39 -04:00
|
|
|
} else {
|
|
|
|
groups.push(format!("{:03}", group));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.reverse();
|
|
|
|
|
|
|
|
groups.join("_")
|
|
|
|
}
|
|
|
|
|
2018-04-01 08:17:25 +02:00
|
|
|
pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T where
|
2016-08-23 13:23:58 -04:00
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
|
|
|
let start = Instant::now();
|
|
|
|
let rv = f();
|
|
|
|
let duration = start.elapsed();
|
2018-04-01 08:17:25 +02:00
|
|
|
let mut accu = accu.lock();
|
|
|
|
*accu = *accu + duration;
|
2016-08-23 13:23:58 -04:00
|
|
|
rv
|
|
|
|
}
|
|
|
|
|
2015-06-27 18:37:33 -07:00
|
|
|
// Memory reporting
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(unix)]
|
2015-06-27 18:37:33 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2018-01-10 08:58:39 -08:00
|
|
|
use std::fs;
|
2015-07-08 08:29:41 -04:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
let field = 1;
|
2018-03-30 09:48:18 -07:00
|
|
|
let contents = fs::read("/proc/self/statm").ok()?;
|
|
|
|
let contents = String::from_utf8(contents).ok()?;
|
2017-12-08 17:32:04 -08:00
|
|
|
let s = contents.split_whitespace().nth(field)?;
|
|
|
|
let npages = s.parse::<usize>().ok()?;
|
2015-08-11 17:27:05 -07:00
|
|
|
Some(npages * 4096)
|
2015-06-27 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(windows)]
|
2015-08-11 17:27:05 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2015-11-02 16:23:22 -08:00
|
|
|
type BOOL = i32;
|
|
|
|
type DWORD = u32;
|
|
|
|
type HANDLE = *mut u8;
|
|
|
|
use libc::size_t;
|
2015-07-08 08:29:41 -04:00
|
|
|
use std::mem;
|
2017-03-13 02:12:13 +02:00
|
|
|
#[repr(C)]
|
|
|
|
#[allow(non_snake_case)]
|
2015-07-08 08:29:41 -04:00
|
|
|
struct PROCESS_MEMORY_COUNTERS {
|
|
|
|
cb: DWORD,
|
|
|
|
PageFaultCount: DWORD,
|
2015-11-02 16:23:22 -08:00
|
|
|
PeakWorkingSetSize: size_t,
|
|
|
|
WorkingSetSize: size_t,
|
|
|
|
QuotaPeakPagedPoolUsage: size_t,
|
|
|
|
QuotaPagedPoolUsage: size_t,
|
|
|
|
QuotaPeakNonPagedPoolUsage: size_t,
|
|
|
|
QuotaNonPagedPoolUsage: size_t,
|
|
|
|
PagefileUsage: size_t,
|
|
|
|
PeakPagefileUsage: size_t,
|
2015-07-08 08:29:41 -04:00
|
|
|
}
|
|
|
|
type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
|
|
|
|
#[link(name = "psapi")]
|
|
|
|
extern "system" {
|
2015-11-02 16:23:22 -08:00
|
|
|
fn GetCurrentProcess() -> HANDLE;
|
2015-07-08 08:29:41 -04:00
|
|
|
fn GetProcessMemoryInfo(Process: HANDLE,
|
|
|
|
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
|
|
|
|
cb: DWORD) -> BOOL;
|
|
|
|
}
|
|
|
|
let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
|
|
|
|
pmc.cb = mem::size_of_val(&pmc) as DWORD;
|
|
|
|
match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
|
|
|
|
0 => None,
|
|
|
|
_ => Some(pmc.WorkingSetSize as usize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn indent<R, F>(op: F) -> R where
|
2015-01-20 15:45:07 -08:00
|
|
|
R: Debug,
|
2014-12-08 20:26:43 -05:00
|
|
|
F: FnOnce() -> R,
|
|
|
|
{
|
2012-04-05 20:59:07 -07:00
|
|
|
// Use in conjunction with the log post-processor like `src/etc/indenter`
|
|
|
|
// to make debug output more readable.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2012-10-23 11:28:20 -07:00
|
|
|
let r = op();
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("<< (Result = {:?})", r);
|
2013-02-15 04:14:34 -05:00
|
|
|
r
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub struct Indenter {
|
2017-03-13 02:12:13 +02:00
|
|
|
_cannot_construct_outside_of_this_module: (),
|
2013-02-27 19:13:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
impl Drop for Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
fn drop(&mut self) { debug!("<<"); }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub fn indenter() -> Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2014-06-06 15:51:42 +02:00
|
|
|
Indenter { _cannot_construct_outside_of_this_module: () }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:39:33 -05:00
|
|
|
pub trait MemoizationMap {
|
|
|
|
type Key: Clone;
|
|
|
|
type Value: Clone;
|
|
|
|
|
2017-08-11 20:34:14 +02:00
|
|
|
/// If `key` is present in the map, return the value,
|
2015-12-22 16:39:33 -05:00
|
|
|
/// otherwise invoke `op` and store the value in the map.
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// N.B., if the receiver is a `DepTrackingMap`, special care is
|
2015-12-22 16:39:33 -05:00
|
|
|
/// needed in the `op` to ensure that the correct edges are
|
|
|
|
/// added into the dep graph. See the `DepTrackingMap` impl for
|
|
|
|
/// more details!
|
2017-07-10 17:10:30 -04:00
|
|
|
fn memoize<OP>(&self, key: Self::Key, op: OP) -> Self::Value
|
2015-12-22 16:39:33 -05:00
|
|
|
where OP: FnOnce() -> Self::Value;
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:01:46 +02:00
|
|
|
impl<K, V> MemoizationMap for RefCell<FxHashMap<K,V>>
|
|
|
|
where K: Hash+Eq+Clone, V: Clone
|
2015-02-17 20:48:07 -08:00
|
|
|
{
|
2015-12-22 16:39:33 -05:00
|
|
|
type Key = K;
|
|
|
|
type Value = V;
|
|
|
|
|
2017-07-10 17:10:30 -04:00
|
|
|
fn memoize<OP>(&self, key: K, op: OP) -> V
|
2015-12-22 16:39:33 -05:00
|
|
|
where OP: FnOnce() -> V
|
|
|
|
{
|
|
|
|
let result = self.borrow().get(&key).cloned();
|
|
|
|
match result {
|
|
|
|
Some(result) => result,
|
|
|
|
None => {
|
|
|
|
let result = op();
|
|
|
|
self.borrow_mut().insert(key, result.clone());
|
|
|
|
result
|
|
|
|
}
|
2015-02-17 20:48:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|