@@ -14,10 +14,10 @@ namespace Stats {
1414// if we can help it.
1515StatNamePtr SymbolTableImpl::encode (const std::string& name) {
1616 SymbolVec symbol_vec;
17- std::vector<std::string > name_vec = absl::StrSplit (name, ' .' );
17+ std::vector<absl::string_view > name_vec = absl::StrSplit (name, ' .' );
1818 symbol_vec.reserve (name_vec.size ());
1919 std::transform (name_vec.begin (), name_vec.end (), std::back_inserter (symbol_vec),
20- [this ](std::string x) { return toSymbol (x); });
20+ [this ](absl::string_view x) { return toSymbol (x); });
2121
2222 return std::make_unique<StatNameImpl>(symbol_vec, *this );
2323}
@@ -35,8 +35,7 @@ void SymbolTableImpl::free(const SymbolVec& symbol_vec) {
3535 auto decode_search = decode_map_.find (symbol);
3636 RELEASE_ASSERT (decode_search != decode_map_.end (), " " );
3737
38- const std::string& str = decode_search->second ;
39- auto encode_search = encode_map_.find (str);
38+ auto encode_search = encode_map_.find (decode_search->second );
4039 RELEASE_ASSERT (encode_search != encode_map_.end (), " " );
4140
4241 ((encode_search->second ).second )--;
@@ -50,21 +49,29 @@ void SymbolTableImpl::free(const SymbolVec& symbol_vec) {
5049 }
5150}
5251
53- Symbol SymbolTableImpl::toSymbol (const std::string& str ) {
52+ Symbol SymbolTableImpl::toSymbol (absl::string_view sv ) {
5453 Symbol result;
55- auto encode_insert = encode_map_.insert ({str, std::make_pair (current_symbol_, 1 )});
56- // If the insertion took place, we mirror the insertion in the decode_map.
57- if (encode_insert.second ) {
58- auto decode_insert = decode_map_.insert ({current_symbol_, str});
59- // We expect the decode_map to be in lockstep.
54+ auto encode_find = encode_map_.find (sv);
55+ // If the string segment doesn't already exist,
56+ if (encode_find == encode_map_.end ()) {
57+ // We create the actual string, place it in the decode_map_, and then insert a string_view
58+ // pointing to it in the encode_map_. This allows us to only store the string once.
59+ std::string str = std::string (sv.data (), sv.size ());
60+
61+ auto decode_insert = decode_map_.insert ({current_symbol_, std::move (str)});
6062 RELEASE_ASSERT (decode_insert.second , " " );
63+
64+ auto encode_insert =
65+ encode_map_.insert ({decode_insert.first ->second , std::make_pair (current_symbol_, 1 )});
66+ RELEASE_ASSERT (encode_insert.second , " " );
67+
6168 result = current_symbol_;
6269 newSymbol ();
6370 } else {
64- // If the insertion didn't take place, return the actual value at that location
65- result = (encode_insert. first )-> second . first ;
66- // and up the refcount at that location
67- ++(encode_insert. first ) ->second .second ;
71+ // If the insertion didn't take place, return the actual value at that location and up the
72+ // refcount at that location
73+ result = (encode_find-> second ). first ;
74+ ++(encode_find ->second ) .second ;
6875 }
6976 return result;
7077}
0 commit comments