Skip to content

Commit

Permalink
Merge pull request #1199 from hzeller/20220217-store-stringpiece-in-s…
Browse files Browse the repository at this point in the history
…ignature

Store string_view in Signature() array, not std::string.
  • Loading branch information
hzeller authored Feb 18, 2022
2 parents a8eea15 + a184aca commit a4323db
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion verilog/tools/kythe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ cc_library(
":kythe_facts",
"//common/util:auto_pop_stack",
"//common/util:iterator_range",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/container:node_hash_map",
"@com_google_absl//absl/strings",
],
)

Expand Down Expand Up @@ -94,6 +94,7 @@ cc_library(
"//verilog/analysis:verilog_project",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
],
Expand Down
9 changes: 4 additions & 5 deletions verilog/tools/kythe/kythe_facts.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ inline constexpr absl::string_view kEmptyKytheLanguage = "";
// Unique identifier for Kythe facts.
class Signature {
public:
explicit Signature(absl::string_view name = "")
: names_({std::string(name)}) {}
explicit Signature(absl::string_view name = "") : names_({name}) {}

Signature(const Signature& parent, absl::string_view name)
: names_(parent.Names()) {
names_.push_back(std::string(name));
names_.push_back(name);
}

bool operator==(const Signature& other) const {
Expand All @@ -52,7 +51,7 @@ class Signature {
// Returns the signature concatenated as a string in base 64.
std::string ToBase64() const;

const std::vector<std::string>& Names() const { return names_; }
const std::vector<absl::string_view>& Names() const { return names_; }

private:
// List that uniquely determines this signature and differentiates it from any
Expand All @@ -65,7 +64,7 @@ class Signature {
//
// for "m" ==> ["m"]
// for "x" ==> ["m", "x"]
std::vector<std::string> names_;
std::vector<absl::string_view> names_;
};
template <typename H>
H AbslHashValue(H state, const Signature& v) {
Expand Down
13 changes: 11 additions & 2 deletions verilog/tools/kythe/kythe_facts_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/node_hash_set.h"
#include "absl/memory/memory.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -279,6 +280,13 @@ class KytheFactsExtractor {

// Contains resulting kythe facts and edges to output.
KytheIndexingData kythe_data_;

// Location signature backing store.
// Inside signatures, we record locations as string_views,
// this is the backing store for assembled strings as
// location markers such as "@123:456"
// Needs to be a node_hash_set to provide value stability.
absl::node_hash_set<std::string> signature_locations_;
};

void StreamKytheFactsEntries(KytheOutput* kythe_output,
Expand Down Expand Up @@ -1266,10 +1274,11 @@ VName KytheFactsExtractor::CreateAnchor(const Anchor& anchor) {
const int start_location =
std::distance(source_text.begin(), anchor.Text().begin());
const int end_location = start_location + anchor.Text().length();
const auto [location_str, _] = signature_locations_.emplace(
absl::StrCat("@", start_location, ":", end_location));
const VName anchor_vname = {.path = FilePath(),
.root = "",
.signature = Signature(absl::StrCat(
"@", start_location, ":", end_location)),
.signature = Signature(*location_str),
.corpus = Corpus()};

CreateFact(anchor_vname, kFactNodeKind, kNodeAnchor);
Expand Down
2 changes: 1 addition & 1 deletion verilog/tools/kythe/scope_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Scope {
// Appends the member of the given scope to the current scope.
void AppendScope(const Scope& scope);

using MemberMap = absl::node_hash_map<std::string, ScopeMemberItem>;
using MemberMap = absl::node_hash_map<absl::string_view, ScopeMemberItem>;
const MemberMap& Members() const { return members_; }
const Signature& GetSignature() const { return signature_; }

Expand Down

0 comments on commit a4323db

Please sign in to comment.