Skip to content

Commit 784a467

Browse files
committed
sped up parsing by ~0..40%
1 parent 0fffbc3 commit 784a467

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

include/tao/json/events/set_value.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ namespace tao::json::events
3434
InPlaceVector() {}
3535
~InPlaceVector()
3636
{
37-
std::destroy_n( array, std::min( count, count_ ) );
37+
std::destroy_n( array, size() );
3838
}
3939
void emplace_back( basic_value< Traits >&& v )
4040
{
4141
::new( array + count ) basic_value< Traits >{ std::move( v ) };
4242
++count;
4343
}
44+
size_t size() const
45+
{
46+
return std::min( count, count_ );
47+
}
4448
};
4549
std::vector< basic_value< Traits > > stack_;
4650
std::deque< InPlaceVector > elements_;
@@ -138,8 +142,7 @@ namespace tao::json::events
138142
auto& a = stack_.back().get_array();
139143
if( elements.count == count_ ) {
140144
a.reserve( count_ + 1 );
141-
for( auto& i : elements.array )
142-
a.push_back( std::move( i ) );
145+
a.resize( count_ );
143146
++elements.count;
144147
}
145148
a.push_back( std::move( value_ ) );
@@ -149,9 +152,19 @@ namespace tao::json::events
149152
{
150153
auto& elements = elements_.back();
151154
auto& v = stack_.back();
152-
if( elements.count <= count_ )
153-
v.get_array().assign( std::make_move_iterator( elements.array ),
154-
std::make_move_iterator( elements.array + elements.count ) );
155+
if( elements.count ) {
156+
const auto size = elements.size();
157+
auto& a = v.get_array();
158+
if( a.empty() ) {
159+
a.assign( std::make_move_iterator( elements.array ),
160+
std::make_move_iterator( elements.array + size ) );
161+
}
162+
else {
163+
std::copy_n( std::make_move_iterator( elements.array ),
164+
size,
165+
a.begin() );
166+
}
167+
}
155168
value_ = std::move( v );
156169
stack_.pop_back();
157170
elements_.pop_back();

0 commit comments

Comments
 (0)