Implements Zero-Copy String Views onto Contiguous Memory, resolves #3265.

- http://www.boost.org/doc/libs/1_61_0/libs/utility/doc/html/string_ref.html
- http://en.cppreference.com/w/cpp/string/basic_string_view
This commit is contained in:
Daniel J. Hofmann
2016-12-15 18:27:09 +01:00
parent b1f1c26703
commit c277b95f03
13 changed files with 219 additions and 144 deletions
+9 -6
View File
@@ -3,6 +3,7 @@
#include "engine/plugins/plugin_base.hpp"
#include "util/coordinate_calculation.hpp"
#include "util/string_view.hpp"
#include "util/vector_tile.hpp"
#include "util/web_mercator.hpp"
@@ -287,8 +288,8 @@ Status TilePlugin::HandleRequest(const std::shared_ptr<const datafacade::BaseDat
std::unordered_map<int, std::size_t> line_int_offsets;
// Same idea for street names - one lookup table for names for all features
std::vector<std::string> names;
std::unordered_map<std::string, std::size_t> name_offsets;
std::vector<util::StringView> names;
std::unordered_map<util::StringView, std::size_t> name_offsets;
// And again for integer values used by points.
std::vector<int> used_point_ints;
@@ -714,14 +715,15 @@ Status TilePlugin::HandleRequest(const std::shared_ptr<const datafacade::BaseDat
reverse_datasource_vector[reverse_datasource_vector.size() -
edge.fwd_segment_position - 1];
std::string name = facade->GetNameForID(edge.name_id);
auto name = facade->GetNameForID(edge.name_id);
const auto name_offset = [&name, &names, &name_offsets]() {
auto iter = name_offsets.find(name);
if (iter == name_offsets.end())
{
auto offset = names.size();
name_offsets[name] = offset;
names.push_back(std::move(name));
names.push_back(name);
return offset;
}
return iter->second;
@@ -873,7 +875,7 @@ Status TilePlugin::HandleRequest(const std::shared_ptr<const datafacade::BaseDat
util::vector_tile::VARIANT_TAG);
// Attribute value 1 == string type
values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING,
facade->GetDatasourceName(i));
facade->GetDatasourceName(i).to_string());
}
for (auto value : used_line_ints)
{
@@ -892,7 +894,8 @@ Status TilePlugin::HandleRequest(const std::shared_ptr<const datafacade::BaseDat
protozero::pbf_writer values_writer(line_layer_writer,
util::vector_tile::VARIANT_TAG);
// Attribute value 1 == string type
values_writer.add_string(util::vector_tile::VARIANT_TYPE_STRING, name);
values_writer.add_string(
util::vector_tile::VARIANT_TYPE_STRING, name.data(), name.size());
}
}
+15 -4
View File
@@ -1,7 +1,9 @@
#include "extractor/suffix_table.hpp"
#include "extractor/scripting_environment.hpp"
#include <algorithm>
#include <iterator>
#include <boost/algorithm/string.hpp>
namespace osrm
@@ -11,13 +13,22 @@ namespace extractor
SuffixTable::SuffixTable(ScriptingEnvironment &scripting_environment)
{
std::vector<std::string> suffixes_vector = scripting_environment.GetNameSuffixList();
for (auto &suffix : suffixes_vector)
suffixes = scripting_environment.GetNameSuffixList();
for (auto &suffix : suffixes)
boost::algorithm::to_lower(suffix);
suffix_set.insert(std::begin(suffixes_vector), std::end(suffixes_vector));
auto into = std::inserter(suffix_set, end(suffix_set));
auto to_view = [](const auto &s) { return util::StringView{s}; };
std::transform(begin(suffixes), end(suffixes), into, to_view);
}
bool SuffixTable::isSuffix(const std::string &possible_suffix) const
{
return isSuffix(util::StringView{possible_suffix});
}
bool SuffixTable::isSuffix(util::StringView possible_suffix) const
{
return suffix_set.count(possible_suffix) > 0;
}
+26 -26
View File
@@ -1,9 +1,10 @@
#include "util/name_table.hpp"
#include "storage/io.hpp"
#include "util/exception.hpp"
#include "util/log.hpp"
#include <algorithm>
#include <fstream>
#include <iterator>
#include <limits>
#include <boost/filesystem/fstream.hpp>
@@ -18,11 +19,9 @@ NameTable::NameTable(const std::string &filename)
storage::io::FileReader name_stream_file_reader(filename,
storage::io::FileReader::HasNoFingerprint);
// name_stream >> m_name_table;
m_name_table.ReadARangeTable(name_stream_file_reader);
unsigned number_of_chars = name_stream_file_reader.ReadElementCount32();
const auto number_of_chars = name_stream_file_reader.ReadElementCount32();
m_names_char_list.resize(number_of_chars + 1); //+1 gives sentinel element
m_names_char_list.back() = 0;
@@ -37,58 +36,59 @@ NameTable::NameTable(const std::string &filename)
}
}
std::string NameTable::GetNameForID(const unsigned name_id) const
StringView NameTable::GetNameForID(const NameID id) const
{
if (std::numeric_limits<unsigned>::max() == name_id)
if (std::numeric_limits<NameID>::max() == id)
{
return "";
return {};
}
auto range = m_name_table.GetRange(name_id);
std::string result;
result.reserve(range.size());
if (range.begin() != range.end())
auto range = m_name_table.GetRange(id);
if (range.begin() == range.end())
{
result.resize(range.back() - range.front() + 1);
std::copy(m_names_char_list.begin() + range.front(),
m_names_char_list.begin() + range.back() + 1,
result.begin());
return {};
}
return result;
auto first = begin(m_names_char_list) + range.front();
auto last = begin(m_names_char_list) + range.back() + 1;
const std::size_t len = last - first;
return StringView{&*first, len};
}
std::string NameTable::GetRefForID(const unsigned name_id) const
StringView NameTable::GetRefForID(const NameID id) const
{
// Way string data is stored in blocks based on `name_id` as follows:
// Way string data is stored in blocks based on `id` as follows:
//
// | name | destination | pronunciation | ref |
// ^ ^
// [range)
// ^ name_id + 3
// ^ id + 3
//
// `name_id + offset` gives us the range of chars.
// `id + offset` gives us the range of chars.
//
// Offset 0 is name, 1 is destination, 2 is pronunciation, 3 is ref.
// See datafacades and extractor callbacks for details.
const constexpr auto OFFSET_REF = 3u;
return GetNameForID(name_id + OFFSET_REF);
return GetNameForID(id + OFFSET_REF);
}
std::string NameTable::GetPronunciationForID(const unsigned name_id) const
StringView NameTable::GetPronunciationForID(const NameID id) const
{
// Way string data is stored in blocks based on `name_id` as follows:
// Way string data is stored in blocks based on `id` as follows:
//
// | name | destination | pronunciation | ref |
// ^ ^
// [range)
// ^ name_id + 2
// ^ id + 2
//
// `name_id + offset` gives us the range of chars.
// `id + offset` gives us the range of chars.
//
// Offset 0 is name, 1 is destination, 2 is pronunciation, 3 is ref.
// See datafacades and extractor callbacks for details.
const constexpr auto OFFSET_PRONUNCIATION = 2u;
return GetNameForID(name_id + OFFSET_PRONUNCIATION);
return GetNameForID(id + OFFSET_PRONUNCIATION);
}
} // namespace util