Boost logo

Boost :

From: Bo Persson (bop_at_[hidden])
Date: 2006-03-05 02:34:53


"Ion Gaztañaga" <igaztanaga_at_[hidden]> skrev i meddelandet
news:4409CCA4.6050107_at_gmail.com...
> Hi to all,
>
> While revising a bit the boost::filesystem error/exception use for
> looking a model for interprocess library and after reading the N1934
> "Filesystem Library Proposal for TR2 (Revision 2)" I'm a bit
> concerned
> about the heavy use of std::string/string_type in the library.
>
> The following functions return some kind of path as value type:
>
> // observers
> const string_type string() const;
> const string_type file_string() const;
> const string_type directory_string() const;
>
> const external_string_type external_file_string() const;
> const external_string_type external_directory_string() const;
>
> string_type root_name() const;
> string_type root_directory() const;
> basic_path root_path() const;
> basic_path relative_path() const;
> string_type leaf() const;
> basic_path branch_path() const;
>
> I know that syntactically this is nicer and we can have RVO:
>
> string_type str = path.string();
>
> But when iterating through directories I find that that returning a
> temporary object that must allocate/copy/deallocate hurts my
> performance
> paranoia. Even with move semantics we have a an overhead:

On the other hand, I would guess that the actual OS and hard drive /
network level operations take a lot more time than copying a few
strings.

>
> std::vector<std::path> paths;
> //fill with paths
>
> std::path::iterator beg = paths.begin(), end = paths.end();
>
> for(; beg != end; ++it){
> std::path::string_type str = it->root_name();//temporary created
> str += "append some data";
> std::cout << str;
> }

or

std::cout << it->root_name() << "append some data";

Who says you want to store the result? A temporary might be just fine.

>
> Couldn't be better (although uglier) to take a reference to a string
> that will be filled?
>
> void fill_root_name(string_type &root_name) const;

Really ugly! :-)

It also assumes that the caller has somewhere to store the result. And
the intention to keep it.

> ...
>
> ////////////////////////////////////////////////////////////////////
>
> std::vector<std::path> paths;
> //fill with paths
>
> std::path::string_type root_name;
>
> root_name.reserve(PATH_LENGTH);

So now you are wasting space instead. :-)

>
> This way we only allocate memory if we don't have enough place in
> our
> string. We can also reserve it beforehand to speed up code.

How do we know that speed is gained (and important), while space is
cheaper?

Bo Persson


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk