This is an archive of the discontinued LLVM Phabricator instance.

[Support] Add a UniqueStringSaver: like StringSaver, but deduplicating.
ClosedPublic

Authored by sammccall on Jul 20 2018, 6:22 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

sammccall created this revision.Jul 20 2018, 6:22 AM

Thanks!

lib/Support/StringSaver.cpp
26 ↗(On Diff #156476)

While we have nice assertion message here, but in return we pay an extra lookup cost. I think we should avoid it (this function is likely to be used in hot path).

Maybe just

auto It = Unique.insert(S);
if (It.second) {
  *It.first = Strings.save(V);
}
return *It.first.
sammccall updated this revision to Diff 156485.Jul 20 2018, 7:28 AM
sammccall marked an inline comment as done.

Simplify insertion.

lib/Support/StringSaver.cpp
26 ↗(On Diff #156476)

I was all ready to explain why this doesn't work (keys in a map/set are const) but... it does.
I think this means DenseMap is unsafe (at least less safe than unordered_map) but at least this usage is safe. Left a comment.

hokein accepted this revision.Jul 20 2018, 8:40 AM

LGTM.

lib/Support/StringSaver.cpp
26 ↗(On Diff #156476)

Yeah, it is tricky.

This revision is now accepted and ready to land.Jul 20 2018, 8:40 AM
This revision was automatically updated to reflect the committed changes.