Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Computer programs expand so as to fill the core available.


devel / comp.lang.c++ / Re: Returning small strings as std::array

SubjectAuthor
* Returning small strings as std::arrayMarcel Mueller
+- Re: Returning small strings as std::arrayBonita Montero
+* Re: Returning small strings as std::arrayPaavo Helde
|`* Re: Returning small strings as std::arrayMarcel Mueller
| `- Re: Returning small strings as std::arrayPaavo Helde
`* Re: Returning small strings as std::arrayAndrey Tarasevich
 `- Re: Returning small strings as std::arrayMarcel Mueller

1
Returning small strings as std::array

<ushcg8$3ekm$1@gwaiyur.mb-net.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3337&group=comp.lang.c%2B%2B#3337

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: news.5.maazl@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.c++
Subject: Returning small strings as std::array
Date: Sat, 9 Mar 2024 11:09:12 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Mar 2024 10:09:12 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="113302"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MyY5v8p8kaHpPDMu2KFBu/0Ir14= sha256:o0wiVi9jPYcvp5S4lwrR49/voBasM/z14P44EX4zcpg=
sha1:6GW2Z7oAdNs/QbjlPVSQCszcNzM= sha256:DtRiMaegtBLO9sABZcOWJ/tD1GnyoZz3SbOu5mWEm7c=
Content-Language: de-DE, en-US
 by: Marcel Mueller - Sat, 9 Mar 2024 10:09 UTC

Is it reasonable to return small strings a std::array to avoid copying?

std::string might require allocation if it has no small string
optimization build in. Furthermore it cannot be initialized from old C
style APIs that require char* buffer and size_t buffer_size.

The idea is to return std::array<char,10> or something like that. This
causes no allocation and the compiler should be able to optimize the
return value to emplace the result into the callers storage.

Any other idea?

Marcel

Re: Returning small strings as std::array

<ushusd$2cm5k$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3339&group=comp.lang.c%2B%2B#3339

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Sat, 9 Mar 2024 16:23:07 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <ushusd$2cm5k$1@raubtier-asyl.eternal-september.org>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Mar 2024 15:22:53 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="d8b238c2f8964366cc937da0e9bee2ec";
logging-data="2513076"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jGtPw55ZxFwT5Yb73gm/e0JVcE+PSZX8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6Q4IZTq+iTEAKoz3J50acSY2XNw=
Content-Language: de-DE
In-Reply-To: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
 by: Bonita Montero - Sat, 9 Mar 2024 15:23 UTC

Am 09.03.2024 um 11:09 schrieb Marcel Mueller:

> Is it reasonable to return small strings a std::array to avoid copying?
> std::string might require allocation if it has no small string
> optimization build in. Furthermore it cannot be initialized from old C
> style APIs that require char* buffer and size_t buffer_size.
> The idea is to return std::array<char,10> or something like that. This
> causes no allocation and the compiler should be able to optimize the
> return value to emplace the result into the callers storage.
> Any other idea?
> Marcel

C++ strings can store small strings internally. For MSVC,
clang / libc++ and g++ / libstdc++ the maximum string length
that can be stored this way is 16.
The following program prints the size stored internally with
your implementation:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{ size_t length = 1;
string from, to;
char const *src, *dst;
do
{
from.resize( 0 );
fill_n( back_inserter( from ), length++, '*' );
src = from.data();
to = move( from );
dst = to.data();

} while( src != dst );
cout << --length << endl;
}

Iterators to basic_string objects are allowed to change when you move
them (other containers doesn't allow this), so also their addresses.
I simply construct a increasingly large string and look if the star-
ting address varies on moving. If it is equal you've got an externally
stored string and the size which can be stored internally is one less.

Re: Returning small strings as std::array

<uskgge$30n72$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3342&group=comp.lang.c%2B%2B#3342

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesnimi@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Sun, 10 Mar 2024 16:35:57 +0200
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uskgge$30n72$1@dont-email.me>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 10 Mar 2024 14:35:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3a2578e453ee8dc66b3b059a467e4c96";
logging-data="3169506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ct2DS2jkzNxe9YOhyDQW/aSOdkYVFJGg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HLmpli5LY7ROWuHz40fUvUkbKjA=
In-Reply-To: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
Content-Language: en-US
 by: Paavo Helde - Sun, 10 Mar 2024 14:35 UTC

09.03.2024 12:09 Marcel Mueller kirjutas:
> Is it reasonable to return small strings a std::array to avoid copying?

No, I believe it's not reasonable as it would complicate the code and
make it less readable, probably without any measurable benefit whatsoever.

> std::string might require allocation if it has no small string
> optimization build in.

All mainstream C++ implementations are using small string optimization
nowadays.

> Furthermore it cannot be initialized from old C
> style APIs that require char* buffer and size_t buffer_size.

Cannot really understand this statement. Are you worrying about the
overhead of initializing 10 bytes in a std::string before calling a C
style API? Can you actually measure this overhead?

> The idea is to return std::array<char,10> or something like that. This
> causes no allocation and the compiler should be able to optimize the
> return value to emplace the result into the callers storage.

Seems like a perfect example of premature optimization. If your
application really requires to win these hypothetical nanoseconds, then
you should probably write your own string class tuned to maximum
performance for that particular application.

Re: Returning small strings as std::array

<uskmev$ehu0$1@gwaiyur.mb-net.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3343&group=comp.lang.c%2B%2B#3343

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: news.5.maazl@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Sun, 10 Mar 2024 17:17:35 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <uskmev$ehu0$1@gwaiyur.mb-net.net>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net> <uskgge$30n72$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 10 Mar 2024 16:17:35 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="477120"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:s97r4p921IuujJ1W+845gbhWJZY= sha256:XVZyet+ru51eqFakqzzrp5wriYOhdadv0l1ULwDAW/k=
sha1:xjWwA+DybQ4WZ7FFqw8NofN1iUM= sha256:g0ZAHmsKx12Jl1vlrtNtOZDj6NGUHjvjxzNDiH/SoTM=
Content-Language: de-DE, en-US
In-Reply-To: <uskgge$30n72$1@dont-email.me>
 by: Marcel Mueller - Sun, 10 Mar 2024 16:17 UTC

Am 10.03.24 um 15:35 schrieb Paavo Helde:
>> std::string might require allocation if it has no small string
>> optimization build in.
>
> All mainstream C++ implementations are using small string optimization
> nowadays.

Yes, it seems so.
Several years ago it was not implemented on platform.

>> Furthermore it cannot be initialized from old C style APIs that
>> require char* buffer and size_t buffer_size.
>
> Cannot really understand this statement. Are you worrying about the
> overhead of initializing 10 bytes in a std::string before calling a C
> style API? Can you actually measure this overhead?

The other way around. The C-API cannot write to a std::string because
std::string once created with sufficient size can no longer be converted
to char*. So I always need a temporary char array to create std::string.
AFAIR using &str.front() to write the string is not allowed.

Marcel

Re: Returning small strings as std::array

<usknum$32cm8$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3344&group=comp.lang.c%2B%2B#3344

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesnimi@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Sun, 10 Mar 2024 18:43:01 +0200
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <usknum$32cm8$1@dont-email.me>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net> <uskgge$30n72$1@dont-email.me>
<uskmev$ehu0$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 10 Mar 2024 16:43:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3a2578e453ee8dc66b3b059a467e4c96";
logging-data="3224264"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YhM/KvSg9Hh/28sKX6S1shHEOaO/PKlg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xPQMmnwVfMeWnUciVrdKsuO09iI=
In-Reply-To: <uskmev$ehu0$1@gwaiyur.mb-net.net>
Content-Language: en-US
 by: Paavo Helde - Sun, 10 Mar 2024 16:43 UTC

10.03.2024 18:17 Marcel Mueller kirjutas:
>
>>> Furthermore it cannot be initialized from old C style APIs that
>>> require char* buffer and size_t buffer_size.
>>
>> Cannot really understand this statement. Are you worrying about the
>> overhead of initializing 10 bytes in a std::string before calling a C
>> style API? Can you actually measure this overhead?
>
> The other way around. The C-API cannot write to a std::string because
> std::string once created with sufficient size can no longer be converted
> to char*. So I always need a temporary char array to create std::string.
> AFAIR using &str.front() to write the string is not allowed.

Your information is out of date for 13 years formally, and more than
that in practice. The C++11 standard added a guarantee that the string
internal buffer is contiguous, and it also added a non-const overload of
data(), so you can write freely in the string via str.data(). One can
even write the terminating zero at str.data()[str.length()] (but no
other value), meaning one can even use strcpy() for writing.

Re: Returning small strings as std::array

<usoqsp$4p88$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3349&group=comp.lang.c%2B%2B#3349

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: andreytarasevich@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Mon, 11 Mar 2024 22:57:41 -0700
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <usoqsp$4p88$1@dont-email.me>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 12 Mar 2024 05:57:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9b2e7856086109ea3ed8159e4d0c75e4";
logging-data="156936"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hW7KiVth+KcXvwYXGaGVU"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:DNol+5iq61Y1u5dXIKaS/UIailI=
In-Reply-To: <ushcg8$3ekm$1@gwaiyur.mb-net.net>
Content-Language: en-US
 by: Andrey Tarasevich - Tue, 12 Mar 2024 05:57 UTC

On 03/09/24 2:09 AM, Marcel Mueller wrote:
> std::string might require allocation if it has no small string
> optimization build in.

True. SSO is not required, but virtually all competent implementations
do it.

> Furthermore it cannot be initialized from old C
> style APIs that require char* buffer and size_t buffer_size.

That is not true.

A non-const version of `std::string::data()` was added in C++17
specifically to support this usage model. (But even before that you
could gain non-const access to its internal buffer through `&str[0]`).

You can pre-`resize()` an `std::string`, pass its `data()` (and `size()`
) to a C function, determine the resultant length based on zero
terminator's location, and then `resize()` it to the new length.

Done.

> The idea is to return std::array<char,10> or something like that. This
> causes no allocation and the compiler should be able to optimize the
> return value to emplace the result into the callers storage.
>
> Is it reasonable to return small strings a std::array to avoid copying?

If you are sure that they will always fit into the pre-determined (at
the compile time) size, then perhaps it might be reasonable. Basically,
the main benefit I can see here is that it allows one to extend SSO-like
behavior beyond the boundary used by the existing `std::string`
implementation. I.e to avoid involving dynamic memory for a greater
range of string lengths.

--
Best regards,
Andrey

Re: Returning small strings as std::array

<usvjvu$1lnpt$1@gwaiyur.mb-net.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3361&group=comp.lang.c%2B%2B#3361

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: news.5.maazl@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.c++
Subject: Re: Returning small strings as std::array
Date: Thu, 14 Mar 2024 20:42:54 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <usvjvu$1lnpt$1@gwaiyur.mb-net.net>
References: <ushcg8$3ekm$1@gwaiyur.mb-net.net> <usoqsp$4p88$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 14 Mar 2024 19:42:54 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="1761085"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:c9xNsWu4ChIUDI0DvQl6oH73aQY= sha256:r+kRNLmPAD797S9SNwnk980KXd9BlMdbUy1CUvzwGLg=
sha1:gOl0GrJUsb1/OkTTTHL6Cvm//CU= sha256:gRpMlj75RE4m9peDVh5SF06aySOU7nwxu0jA3rQNBbM=
Content-Language: de-DE, en-US
In-Reply-To: <usoqsp$4p88$1@dont-email.me>
 by: Marcel Mueller - Thu, 14 Mar 2024 19:42 UTC

Am 12.03.24 um 06:57 schrieb Andrey Tarasevich:
> On 03/09/24 2:09 AM, Marcel Mueller wrote:
>> Furthermore it cannot be initialized from old C style APIs that
>> require char* buffer and size_t buffer_size.
>
> That is not true.
>
> A non-const version of `std::string::data()` was added in C++17
> specifically to support this usage model. (But even before that you
> could gain non-const access to its internal buffer through `&str[0]`).

Thanks, I was not aware of the latter!

The application is restricted to C++11. (I forgot to mention.)

>> Is it reasonable to return small strings a std::array to avoid copying?
>
> If you are sure that they will always fit into the pre-determined (at
> the compile time) size, then perhaps it might be reasonable.

Yes, if the C-API has a size parameter, I am quite sure. ;-)

Marcel

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor