string_view as a value vs as an rvalue?

So I have a templated function, SetMessage(), that takes a string_view as one of its arguments:
1
2
3
4
5
6
7
8
9
template <typename... Args> constexpr void SetMessage( std::string_view message, Args &&...args ){
    msg.clear( );
    msgBuffer.clear( );
        using iterator = std::back_insert_iterator<std::vector<char>>;
	using context  = std::basic_format_context<iterator, char>;
	std::vformat_to( std::back_inserter( msgBuffer ), message, std::make_format_args<context>( std::forward<Args>( args )... ) );
	
msg.append( msgBuffer.data( ), msgBuffer.size( ) ).append( "\n" );
}


The loop looks like so and I'm just doing a crude test by commenting out the other lines and using chrono's steady_clock to measure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned int i { 0 }, iterations { 1000000 };
std::string mockArg;
	for( int i = 0; i < 399; i++ ) {
		mockArg += "a";
	}
for( i; i < iterations; ++i ) {
    auto message { "{}" };
    auto now      = std::chrono::system_clock::now( );
    auto pointSec = std::chrono::duration_cast<std::chrono::seconds>( now.time_since_epoch( ) );
        if( info.TimeDetails( ).LastLogPoint( ) != pointSec ) {
            info.TimeDetails( ).UpdateCache( now );
	}
	info.SetMsgLevel( LoggerLevel::trace );
	info.SetMessage( message, mockArg );
        auto formatted { format.GetFormatters( ).Format( ) };
        std::fwrite( formatted.data( ), sizeof( char ), formatted.size( ), file );
}


If I pass this string_view in as an rvalue, I'm able to get 0.45us on my system, but when I pass this string_view in as a value instead, I get 0.47us. So really, given that string_view is just a non-owning pointer to the char type, is there any downside to passing by rvalue? I'm just getting into rvalues/rvalue references overall, so there's probably some nuances I'm overlooking here. My gut says this would lead to undefined behavior in certain situations and that I should just pass this in by value, but I would like to hear other thoughts - when I searched the web, I found similar questions on passing by reference, but not so much by rvalue so I'm unsure if reference concerns trail over to rvalues as well in this case. Thank you in advance for any insight into this!
EDIT: I realize these times aren't significant to each other, but more so asking based on string_view rvalues
Last edited on
Topic archived. No new replies allowed.