POV-Ray : Newsgroups : povray.off-topic : New NearInfinity demo up Server Time
6 Sep 2024 19:23:36 EDT (-0400)
  New NearInfinity demo up (Message 16 to 25 of 25)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Darren New
Subject: Re: New NearInfinity demo up
Date: 15 Dec 2008 19:28:33
Message: <4946f631$1@news.povray.org>
Warp wrote:
> std::vector<hgeSprite> tile;
> 
> tile.reserve(8*8);
> for(...)
>   for(...)
>     tile.push_back(hgeSprite(...));

Not that I'd recommend micro-optimization over readability, but isn't this 
going to be more inefficient than the other way? Doesn't this allocate a 
sprite on the stack, pass it to tile.push_back(), which then has to copy it 
to dynamically allocated memory anyway?

-- 
   Darren New, San Diego CA, USA (PST)
   The NFL should go international. I'd pay to
   see the Detroit Lions vs the Roman Catholics.


Post a reply to this message

From: nemesis
Subject: Re: New NearInfinity demo up
Date: 15 Dec 2008 19:40:00
Message: <web.4946f8153f822f764d42da060@news.povray.org>
stbenge <THI### [at] hotmailcom> wrote:
> Who's going to trust my programs, now that my ignorance is known? I
> could always provide the source...

It's good to know you suck at least on something, Master.  Makes you... more
human. ;)

OTOH, it's a pretty good-looking "game" for someone still learning its C++
way...


Post a reply to this message

From: Warp
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 12:20:04
Message: <4947e344@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> > std::vector<hgeSprite> tile;
> > 
> > tile.reserve(8*8);
> > for(...)
> >   for(...)
> >     tile.push_back(hgeSprite(...));

> Not that I'd recommend micro-optimization over readability, but isn't this 
> going to be more inefficient than the other way? Doesn't this allocate a 
> sprite on the stack, pass it to tile.push_back(), which then has to copy it 
> to dynamically allocated memory anyway?

  The std::vector reserves an entire array of objects, rather than
reserving them one-by-one. (What the reserve() function does is that it
allocates space for that many objects, without actually instantiating them.)
So rather than having 64 allocations with 'new', there's only one.

  The temporary given as parameter is created only once, not 64 times.

  What is done 64 times is calling the copy constructor of hgeSprite
(for each element added to the vector). It of course depends on how
the copy constructor in question is implemented, but usually calling
a copy constructor should not be significantly slower than calling the
regular constructor.

  If we compare the amount of operations in both situations, they would be:

- 1 'new' call, vs. 64 'new' calls.
- 1 temporary instantiated, vs. none.
- 64 copy constructor calls, vs. 64 regular constructor calls.
- 1 temporary destruction, vs. none.

  Doing only 1 'new' rather than 64 might in itself make the whole thing
faster, even though there's an additional temporary creation/destruction.

  Anyways, even if there is some speed penalty, it's probably negligible.
As a reward your objects will be managed by std::vector, rather than you
having to manage them manually.

  (As an additional reward, the std::vector will require less memory than
the 64 individually allocated objects. Of course with 64 objects this is
nothing, but it might become significant with a larger number.)

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 13:05:54
Message: <4947ee02$1@news.povray.org>
Warp wrote:
>   The std::vector reserves an entire array of objects, rather than
> reserving them one-by-one.

Ok, cool. I would have guessed std::vector would allocate an array of pointers.

>   The temporary given as parameter is created only once, not 64 times.

So the declaration is essentially hoisted from out of the loop? Makes sense.

>   What is done 64 times is calling the copy constructor of hgeSprite

OK. I know the assignment operator, and the constructor, but I never heard 
of a copy constructor. <google> Oh, I see. It's a constructor with a 
particular type for the argument. OK.

> a copy constructor should not be significantly slower than calling the
> regular constructor.

It doesn't look like it, but doesn't it have to "construct" the temporary, 
then invoke the copy constructor? It's not like the compiler can construct 
directly into the array element that std::vector allocated, can it? And 
wouldn't it have to call the destructor on the temporary 64 times after it 
was copied (assuming it has a destructor)?

It looks like the code you wrote initializes a temporary, then passes it (by 
address, presumedly) to std::vector.push_back(), which then has to copy it 
into an internal array. Then the code reinitializes the same temporary for 
the next iteration (assuming the code is even a little smartly compiled). If 
the sprite has a destructor, doesn't it need to call that before freeing the 
temporary? How can the compiler know the ... Oh, is std::vector inlined 
enough that the compiler can see it's getting inserted directly into a 
particular array element? If so, that would make sense, yes.

>   If we compare the amount of operations in both situations, they would be:
> 
> - 1 'new' call, vs. 64 'new' calls.
> - 1 temporary instantiated, vs. none.
> - 64 copy constructor calls, vs. 64 regular constructor calls.
> - 1 temporary destruction, vs. none.

Hmmm... How does the temporary get constructed? Wouldn't it be 64 calls to 
the constructor (taking the various individual arguments for the sprite) and 
then 64 calls to copy that data to the one that's in std::vector?

>   Doing only 1 'new' rather than 64 might in itself make the whole thing
> faster, even though there's an additional temporary creation/destruction.
> 
>   Anyways, even if there is some speed penalty, it's probably negligible.
> As a reward your objects will be managed by std::vector, rather than you
> having to manage them manually.

As I said, I'm never (well, rarely) against clarity and idiom in favor of 
micro-performance optimizations. Especially when you're talking about 
initialization which you presumably do only once.

-- 
   Darren New, San Diego CA, USA (PST)
   The NFL should go international. I'd pay to
   see the Detroit Lions vs the Roman Catholics.


Post a reply to this message

From: Warp
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 13:40:25
Message: <4947f619@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   The temporary given as parameter is created only once, not 64 times.

> So the declaration is essentially hoisted from out of the loop? Makes sense.

  Ok, it may be possible that it is created in each loop, depending on how
you are creating it, so I may have exaggerated that bit, and it may indeed
be created 64 times.

  You could manually optimize it and create it only once outside the loops,
to make sure. (If, for example, each sprite is otherwise identical but with
different coordinates, you could simply change the coordinates and push it
into the vector.)

  OTOH in this case it probably doesn't matter.

> >   What is done 64 times is calling the copy constructor of hgeSprite

> OK. I know the assignment operator, and the constructor, but I never heard 
> of a copy constructor. <google> Oh, I see. It's a constructor with a 
> particular type for the argument. OK.

  The difference between the copy constructor and an the assignment operator
is that in the latter case the object being assigned to has already been
constructed in the past, and now it's assigned a new value. With the copy
constructor the object in question is directly initialized with the given
object.

  Copy constructors are called, for example, when you pass an object to
a function by value.

  (In some situations the compiler may be able to optimize the copy
constructor call away.)

> > a copy constructor should not be significantly slower than calling the
> > regular constructor.

> It doesn't look like it, but doesn't it have to "construct" the temporary, 
> then invoke the copy constructor? It's not like the compiler can construct 
> directly into the array element that std::vector allocated, can it?

  Actually it is perfectly possible to construct into an array element
directly. (A programmer can do that himself by using so-called placement
new.)

  Whether or not the compiler is smart enough to optimize it like that
is another question.

> And 
> wouldn't it have to call the destructor on the temporary 64 times after it 
> was copied (assuming it has a destructor)?

  If the temporary is created 64 times, then it will be destroyed 64 times
as well, of course.

> It looks like the code you wrote initializes a temporary, then passes it (by 
> address, presumedly) to std::vector.push_back(), which then has to copy it 
> into an internal array. Then the code reinitializes the same temporary for 
> the next iteration (assuming the code is even a little smartly compiled). If 
> the sprite has a destructor, doesn't it need to call that before freeing the 
> temporary? How can the compiler know the ... Oh, is std::vector inlined 
> enough that the compiler can see it's getting inserted directly into a 
> particular array element? If so, that would make sense, yes.

  Being a template class, all member functions of std::vector are inlined.
(This might change with export templates, but things like push_back() will
probably always be inline, for efficiency.)

  As I said, you may be right in that the temporary is created 64 times,
though. You could hand-optimize it by creating it once outside the loops.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 15:43:10
Message: <494812de$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>>>   The temporary given as parameter is created only once, not 64 times.
> 
>> So the declaration is essentially hoisted from out of the loop? Makes sense.
> 
>   Ok, it may be possible that it is created in each loop, depending on how
> you are creating it, so I may have exaggerated that bit, and it may indeed
> be created 64 times.

I would think if the arguments are different each time, the constructor at 
least would have to be called, even if it's overwriting the previous memory 
locations. (I.e., if the compiler allocated the space once on the stack or 
even in static memory.) And if the compiler can't tell the class's 
destructor doesn't do something like calling 'destroy', it can't just 
overwrite the constructed value without destructing it first?

>   You could manually optimize it and create it only once outside the loops,
> to make sure. (If, for example, each sprite is otherwise identical but with
> different coordinates, you could simply change the coordinates and push it
> into the vector.)

Yep.

>   OTOH in this case it probably doesn't matter.

Yep.

>   Copy constructors are called, for example, when you pass an object to
> a function by value.

Good to know. Thanks!  Still wading thru the C++ manuals and tutorials here.

>   Actually it is perfectly possible to construct into an array element
> directly. (A programmer can do that himself by using so-called placement
> new.)

Sure. I was speaking of this particular bit of code, tho.

>   Whether or not the compiler is smart enough to optimize it like that
> is another question.

OK.

>> And 
>> wouldn't it have to call the destructor on the temporary 64 times after it 
>> was copied (assuming it has a destructor)?
> 
>   If the temporary is created 64 times, then it will be destroyed 64 times
> as well, of course.

OK. So you were assuming a trivial destructor? I'm just trying to learn.

>   As I said, you may be right in that the temporary is created 64 times,
> though. You could hand-optimize it by creating it once outside the loops.

I would bet that it's easier for the compiler to create the temporary only 
once than it is for the compiler to know it can call the constructor on the 
array elements in std::vector without any temporary needed at all. :-)

-- 
   Darren New, San Diego CA, USA (PST)
   The NFL should go international. I'd pay to
   see the Detroit Lions vs the Roman Catholics.


Post a reply to this message

From: Warp
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 16:31:01
Message: <49481e15@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   Copy constructors are called, for example, when you pass an object to
> > a function by value.

> Good to know. Thanks!  Still wading thru the C++ manuals and tutorials here.

  Copy constructors and assignment operators are important to know in C++
when designing classes.

  Usually when your class only has basic types and managed classes as
members, you don't need to create a copy constructor nor assignment operator
for your class. The default ones generated by the compiler will usually
suffice. (The default ones simply copy/assign each member of the parameter
to each member of this.)

  For example:

class MyClass
{
    int x, y; // basic types
    std::string name; // managed class

 public:
    // No need to write your own copy constructor / assignment operator
    // here because the compiler-generated ones suffice.
    ...
};

  (I think the term "managed" is not generally used in pure C++, but I'll
use that term because I can't think of any better one. Not to be confused
with managed classes in .NET; I'm not talking about those at all. With
"managed" I mean a class which may allocate memory inside itself, but is
well-behaved and can be safely copied and assigned around without worries.
std::string and the STL containers are examples.)

  Problems happen when your class has, for example, raw pointers to memory
it has allocated, or other similar resources. Example:

class MyClass
{
    int* array;

 public:
    MyClass(int size)
    {
        array = new int[size];
    }

    ~MyClass() { delete[] array; }

    // If now you don't implement a copy constructor / assignment operator
    // you will have a problem!
};

  How to solve the problem depends on how you want your class to behave.

  By far the easiest (and most common) solution is to simply forbid copying
and assignment of your class. This happens by declaring the copy constructor
and assignment operator in the private part of the class and not implementing
them. Problem solved. Of course now you can't copy nor assign instances of
your class (nor can you use them in STL containers), but in many cases you
don't need to. This is very typical for classes which you instantiate only
once during the execution of the program.

  What forbidding those functionalities does is prevent accidents, so it's
a good custom, even if you are not intending to copy/assign the class around.

  Sometimes you do want to copy/assign instances of your class, though.
In that case you have to decide whether the copies should share the data
or deep-copy it. If deep-copying is enough, then by far the easiest solution
is replacing your array with a std::vector, and consequently you don't need
to write any copy constructor / assignment operator. If you want to share,
then you could use the smart pointers offered by the Boost library. (The
current C++ standard doesn't yet offer such smart pointers, except for
auto_ptr, which cannot be used for sharing data.)

  Of course this is good only if your data is an array. If your dynamic data
has a more complex geometry, it becomes a bit more complicated.

  Also the data might not be memory at all. For example this presents the
exact same problem:

class MyClass
{
    std::FILE* inputFile;

 public:
    MyClass(): inputFile(0) {}
    ~MyClass()
    {
        if(inputFile) std::fclose(inputFile);
    }

    bool openFile(const char* filename)
    {
        if(inputFile) std::fclose(inputFile);
        return (inputFile = std::fopen(filename, "rb"));
    }

    ...
};

  The data is not memory, but if you copy/assign instances of this class,
you'll get a problem (you may access or close a closed file).

  A more subtle example:

class MyClass
{
    std::list<Node> nodes;
    std::list<Node>::iterator currentNode;
    ...
};

  Assume 'currentNode' points to some node in the list, or to nodes.end().
If this class doesn't have a proper copy constructor / assignment operator
and you copy instances of it, you'll get problems.

  I suppose the morale of this long text is: If you stick to basic types
and managed classes (as I call them) as far as possible, you'll avoid
problems. Pointers and iterators as members are often problematic.

  (Note that sometimes you might want to forbid copying/assignment even
if the compiler-generated ones were safe. This may be the case eg. if
the copying would be a very heavy operation, such as if you have huge
vectors as member variables.)

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 17:21:31
Message: <494829eb@news.povray.org>
Warp wrote:
>   By far the easiest (and most common) solution is to simply forbid
>   copying
> and assignment of your class. This happens by declaring the copy
> constructor and assignment operator in the private part of the class and
> not implementing them. Problem solved. Of course now you can't copy nor
> assign instances of your class (nor can you use them in STL containers),
> but in many cases you don't need to. This is very typical for classes
> which you instantiate only once during the execution of the program.
> 
>   What forbidding those functionalities does is prevent accidents, so it's
> a good custom, even if you are not intending to copy/assign the class
> around.
> 
>   Sometimes you do want to copy/assign instances of your class, though.
> In that case you have to decide whether the copies should share the data
> or deep-copy it. If deep-copying is enough, then by far the easiest
> solution is replacing your array with a std::vector, and consequently you
> don't need to write any copy constructor / assignment operator. If you
> want to share, then you could use the smart pointers offered by the Boost
> library. (The current C++ standard doesn't yet offer such smart pointers,
> except for auto_ptr, which cannot be used for sharing data.)

More info on this is available on Warp's website :)

http://warp.povusers.org/c++test/


Post a reply to this message

From: Darren New
Subject: Re: New NearInfinity demo up
Date: 16 Dec 2008 18:53:09
Message: <49483f65$1@news.povray.org>
Warp wrote:
>   Copy constructors and assignment operators are important to know in C++
> when designing classes.

Yes. So far, that's really the only part that doesn't seem pretty obvious 
just skimming thru tutorials. Hence the wading in seriously.

>   (I think the term "managed" is not generally used in pure C++, but I'll
> use that term because I can't think of any better one.

Yeah. I wonder if MS actually defines the term "managed" clearly. :-) Other 
than "that stuff .NET does for you."


Thanks. I'll come back and re-read this again when I get past the chapter 
that talks about such stuff explicitly.


-- 
   Darren New, San Diego CA, USA (PST)
   The NFL should go international. I'd pay to
   see the Detroit Lions vs the Roman Catholics.


Post a reply to this message

From: scott
Subject: Re: New NearInfinity demo up
Date: 17 Dec 2008 03:46:32
Message: <4948bc68@news.povray.org>
> I see. The array itself was not dynamic, so there was no reason to delete 
> it. Despite my limited knowledge of the subject, I managed to do the right 
> thing by deleting them individually... They were deleted in the same 
> manner as they were created. There's more to this issue I need to explore, 
> and I'm not ashamed to admit it ;)
>
> Who's going to trust my programs, now that my ignorance is known? I could 
> always provide the source...

Hehe I had something similar where I had forgotten to type the "()" after a 
release call on a texture object (this is DirectX).  For some reason the 
compiler though that:

texture1->Release;

was a perfectly fine statement, but of course it wasn't calling the release 
function to free up the memory.  It took me ages to track this one down, 
because quickly reading that line you don't notice the () missing, and it 
just caused random crashes occasionally on exit.

Took me a while to figure out the whole delete [] palaver too.

Oh well, you live and learn :-)


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.