optimize bdecode_node offset calculation (#1767)

optimize bdecode_node offset calculation
This commit is contained in:
Andrei Kurushin 2017-03-05 18:45:27 +03:00 committed by Arvid Norberg
parent a34ce0278e
commit 8cd0bb6eb3
1 changed files with 13 additions and 7 deletions

View File

@ -109,6 +109,14 @@ namespace libtorrent
// reading a key or a vale. 0 means key 1 is value
std::uint32_t state:1;
};
// diff between current and next item offset
// should only be called for non last item in array
int token_source_span(bdecode_token const& t)
{
return (&t)[1].offset - t.offset;
}
} // anonymous namespace
@ -193,7 +201,6 @@ namespace libtorrent
}
}
bdecode_node::bdecode_node()
: m_root_tokens(nullptr)
, m_buffer(nullptr)
@ -470,7 +477,7 @@ namespace libtorrent
{
TORRENT_ASSERT(type() == dict_t);
bdecode_token const* tokens = m_root_tokens;
bdecode_token const* const tokens = m_root_tokens;
// this is the first item
int token = m_token_idx + 1;
@ -479,7 +486,7 @@ namespace libtorrent
{
bdecode_token const& t = tokens[token];
TORRENT_ASSERT(t.type == bdecode_token::string);
int const size = m_root_tokens[token + 1].offset - t.offset - t.start_offset();
int const size = token_source_span(t) - t.start_offset();
if (int(key.size()) == size
&& std::equal(key.data(), key.data() + size, m_buffer
+ t.offset + t.start_offset()))
@ -554,7 +561,7 @@ namespace libtorrent
{
TORRENT_ASSERT(type() == int_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
int size = m_root_tokens[m_token_idx + 1].offset - t.offset;
int const size = token_source_span(t);
TORRENT_ASSERT(t.type == bdecode_token::integer);
// +1 is to skip the 'i'
@ -574,8 +581,7 @@ namespace libtorrent
{
TORRENT_ASSERT(type() == string_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
std::size_t const size = m_root_tokens[m_token_idx + 1].offset - t.offset
- aux::numeric_cast<std::size_t>(t.start_offset());
std::size_t const size = aux::numeric_cast<std::size_t>(token_source_span(t) - t.start_offset());
TORRENT_ASSERT(t.type == bdecode_token::string);
return string_view(m_buffer + t.offset + t.start_offset(), size);
@ -594,7 +600,7 @@ namespace libtorrent
TORRENT_ASSERT(type() == string_t);
bdecode_token const& t = m_root_tokens[m_token_idx];
TORRENT_ASSERT(t.type == bdecode_token::string);
return m_root_tokens[m_token_idx + 1].offset - t.offset - t.start_offset();
return token_source_span(t) - t.start_offset();
}
void bdecode_node::reserve(int tokens)