From 5394fefc2bc297e3c7bc13d8c54823f34d82877c Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Wed, 7 Dec 2011 08:55:46 -0800 Subject: merge StatCache into State I think I had originally imagined StatCache would contain more state, but at this point it's clear it was just managing a single map, which could just as well be in the already-small State object. --- configure.py | 2 +- src/stat_cache.cc | 68 ------------------------------------------------------- src/stat_cache.h | 41 --------------------------------- src/state.cc | 42 ++++++++++++++++++++++++++++++---- src/state.h | 16 ++++++++----- 5 files changed, 49 insertions(+), 120 deletions(-) delete mode 100644 src/stat_cache.cc delete mode 100644 src/stat_cache.h diff --git a/configure.py b/configure.py index 1baa0b7..2121f47 100755 --- a/configure.py +++ b/configure.py @@ -143,7 +143,7 @@ if platform != 'mingw': n.comment('Core source files all build into ninja library.') for name in ['build', 'build_log', 'clean', 'edit_distance', 'eval_env', - 'graph', 'graphviz', 'parsers', 'util', 'stat_cache', + 'graph', 'graphviz', 'parsers', 'util', 'disk_interface', 'state']: objs += cxx(name) if platform == 'mingw': diff --git a/src/stat_cache.cc b/src/stat_cache.cc deleted file mode 100644 index e414d41..0000000 --- a/src/stat_cache.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2011 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "stat_cache.h" - -#include - -#include "edit_distance.h" -#include "graph.h" - -Node* StatCache::GetFile(const std::string& path) { - Node* node = LookupFile(path); - if (node) - return node; - node = new Node(path); - paths_[node->path().c_str()] = node; - return node; -} - -Node* StatCache::LookupFile(const std::string& path) { - Paths::iterator i = paths_.find(path.c_str()); - if (i != paths_.end()) - return i->second; - return NULL; -} - -Node* StatCache::SpellcheckFile(const std::string& path) { - const bool kAllowReplacements = true; - const int kMaxValidEditDistance = 3; - - int min_distance = kMaxValidEditDistance + 1; - Node* result = NULL; - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { - int distance = EditDistance( - i->first, path, kAllowReplacements, kMaxValidEditDistance); - if (distance < min_distance && i->second) { - min_distance = distance; - result = i->second; - } - } - return result; -} - -void StatCache::Dump() { - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { - Node* node = i->second; - printf("%s %s\n", - node->path().c_str(), - node->status_known() ? (node->dirty() ? "dirty" : "clean") - : "unknown"); - } -} - -void StatCache::Invalidate() { - for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) - i->second->ResetState(); -} diff --git a/src/stat_cache.h b/src/stat_cache.h deleted file mode 100644 index b7d6e8a..0000000 --- a/src/stat_cache.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2011 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef NINJA_STAT_CACHE_H_ -#define NINJA_STAT_CACHE_H_ - -#include -using namespace std; - -#include "hash_map.h" - -#include - -struct Node; - -/// Mapping of path -> Node. -struct StatCache { - Node* GetFile(const string& path); - Node* LookupFile(const string& path); - Node* SpellcheckFile(const string& path); - - /// Dump the mapping to stdout (useful for debugging). - void Dump(); - void Invalidate(); - - typedef ExternalStringHashMap::Type Paths; - Paths paths_; -}; - -#endif // NINJA_STAT_CACHE_H_ diff --git a/src/state.cc b/src/state.cc index bbb2f6a..1635f31 100644 --- a/src/state.cc +++ b/src/state.cc @@ -15,7 +15,9 @@ #include "state.h" #include +#include +#include "edit_distance.h" #include "graph.h" #include "util.h" @@ -46,15 +48,36 @@ Edge* State::AddEdge(const Rule* rule) { } Node* State::GetNode(const string& path) { - return stat_cache_.GetFile(path); + Node* node = LookupNode(path); + if (node) + return node; + node = new Node(path); + paths_[node->path().c_str()] = node; + return node; } Node* State::LookupNode(const string& path) { - return stat_cache_.LookupFile(path); + Paths::iterator i = paths_.find(path.c_str()); + if (i != paths_.end()) + return i->second; + return NULL; } Node* State::SpellcheckNode(const string& path) { - return stat_cache_.SpellcheckFile(path); + const bool kAllowReplacements = true; + const int kMaxValidEditDistance = 3; + + int min_distance = kMaxValidEditDistance + 1; + Node* result = NULL; + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { + int distance = EditDistance( + i->first, path, kAllowReplacements, kMaxValidEditDistance); + if (distance < min_distance && i->second) { + min_distance = distance; + result = i->second; + } + } + return result; } void State::AddIn(Edge* edge, const string& path) { @@ -106,7 +129,18 @@ vector State::DefaultNodes(string* err) { } void State::Reset() { - stat_cache_.Invalidate(); + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) + i->second->ResetState(); for (vector::iterator e = edges_.begin(); e != edges_.end(); ++e) (*e)->outputs_ready_ = false; } + +void State::Dump() { + for (Paths::iterator i = paths_.begin(); i != paths_.end(); ++i) { + Node* node = i->second; + printf("%s %s\n", + node->path().c_str(), + node->status_known() ? (node->dirty() ? "dirty" : "clean") + : "unknown"); + } +} diff --git a/src/state.h b/src/state.h index 1e2cd30..6b8d8b2 100644 --- a/src/state.h +++ b/src/state.h @@ -19,11 +19,10 @@ #include #include #include +using namespace std; #include "eval_env.h" -#include "stat_cache.h" - -using namespace std; +#include "hash_map.h" struct BuildLog; struct Edge; @@ -45,16 +44,21 @@ struct State { void AddIn(Edge* edge, const string& path); void AddOut(Edge* edge, const string& path); bool AddDefault(const string& path, string* error); + /// Reset state. Keeps all nodes and edges, but restores them to the + /// state where we haven't yet examined the disk for dirty state. void Reset(); + /// Dump the nodes (useful for debugging). + void Dump(); + /// @return the root node(s) of the graph. (Root nodes have no output edges). /// @param error where to write the error message if somethings went wrong. vector RootNodes(string* error); vector DefaultNodes(string* error); - StatCache* stat_cache() { return &stat_cache_; } - - StatCache stat_cache_; + /// Mapping of path -> Node. + typedef ExternalStringHashMap::Type Paths; + Paths paths_; /// All the rules used in the graph. map rules_; -- cgit v0.12