Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Add a TagSet class providing equality and hashing for key/value maps.#95

Merged
isturdy merged 1 commit intocensus-instrumentation:masterfrom
isturdy:tagset
Mar 22, 2018
Merged

Add a TagSet class providing equality and hashing for key/value maps.#95
isturdy merged 1 commit intocensus-instrumentation:masterfrom
isturdy:tagset

Conversation

@isturdy
Copy link
Copy Markdown
Contributor

@isturdy isturdy commented Mar 8, 2018

This is needed for the DeltaProducer--the ViewData maps include only the tag values in ViewDescriptor order, but since the DeltaProducer aggregates before view aggregation we need to keep the entire mapping.

@isturdy isturdy requested a review from g-easy March 8, 2018 01:45
@isturdy isturdy force-pushed the tagset branch 8 times, most recently from 45df5b4 to b83e0da Compare March 9, 2018 21:35
TEST(TagSetTest, HashDisregardsOrder) {
TagSet ts1({{"k1", "v1"}, {"k2", "v2"}});
TagSet ts2({{"k2", "v2"}, {"k1", "v1"}});
EXPECT_EQ(TagSet::Hash()(ts1), TagSet::Hash()(ts2));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't testing the hash, this is testing that both tagsets are sorted in Initialize()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the implementation--I want it to catch anything silly (such as hashing before sorting).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's reasonable.

// internally because c++ cannot deduce the conversion needed.
TagSet(std::initializer_list<std::pair<absl::string_view, absl::string_view>>
tags);
// This constructor is needed so that callers can dynamically construct
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a single constructor that takes an absl::Span?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That cannot autoconvert from e.g. {{"key", "value"}}--the compiler can convert the string literal to a string_view or the initialize list to a span, but not both.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't realize.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem--absl::Span is what I initially wrote too. :)

}
}

std::size_t TagSet::Hash::operator()(const TagSet& tag_set) const {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this functor is so that we can have an unordered_map? Should there be a test for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one.

@isturdy isturdy force-pushed the tagset branch 2 times, most recently from 10b23dd to ac29491 Compare March 22, 2018 00:38
std::hash<std::string> hasher;
hash_ = 1;
for (const auto& tag : tags_) {
static const size_t kMul = static_cast<size_t>(0xdc3eb94af8ab4c93ULL);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as-is for now.

But in a followup PR, please factor this against common/internal/string_vector_hash.h. I'd like to have a single place where this hash function (and its magic number) lives. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@isturdy isturdy merged commit f0f7303 into census-instrumentation:master Mar 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants