Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why RenderStates is passed by value in draw function?  (Read 8497 times)

0 Members and 1 Guest are viewing this topic.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #15 on: December 26, 2014, 10:00:42 pm »
What is the difference between

void func(State s)
{
        // s not changed
        func2(s);
}

void func(State s)
{
        // s needs to be changed
        s.a = 5;
        func2(s);
}

and

void func(const State& s)
{
        // s not changed
        func2(s);
}

void func(const State& s)
{
        // s needs to be changed
        State s2 = s;
        s2.a = 5;
        func2(s2);
}

Except that in the former, the state is always copied, and in the latter, the state is only copied if it needs to be modified?

My question is why the latter is not considered the better solution.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #16 on: December 26, 2014, 10:05:14 pm »
More code for no justifyable reason? As I keep saying, it is trivial to copy RenderStates, it doesnt need to be passed by reference, I dont know how else to say it.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #17 on: December 26, 2014, 10:11:24 pm »
You didn't appear to understand what I was saying so that is why I put the code. But you are very rude so I'm not going to talk to you any more.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Why RenderStates is passed by value in draw function?
« Reply #18 on: December 26, 2014, 10:22:21 pm »
I'm not near a compiler for a couple of days so I can't check, but modern optimizing compilers are pretty smart, and I'd guess that if you checked the disassembly from an optimized build you'd see that the compiler probably elides/removes the copy completely in rhe case where it is not modified (as already mentioned).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Why RenderStates is passed by value in draw function?
« Reply #19 on: December 26, 2014, 10:54:02 pm »
Sorry guys but... This whole discussion looks really stupid, you should stop it :P

Neil, in case it was not clear enough, yes you're right, from a pure technical point of view your code is more "optimized" (*). But nobody cares because in the end it will make no difference. So I pass by value to save one extra line of code because 80% of SFML classes need to do that copy. That's just it...

(*) compilers are smart, I'm pretty sure it doesn't even make any difference at all
« Last Edit: December 26, 2014, 10:55:36 pm by Laurent »
Laurent Gomila - SFML developer

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #20 on: December 26, 2014, 10:55:16 pm »
I can believe that the cost of passing render states by value is trivial and so there's no point worrying about it.

I am reluctant to believe the compiler can optimize away the copy in a pass by value in this case. Draw is a virtual function and it may not be known until run-time whether the states parameter is modified or not by the resolved function.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #21 on: December 27, 2014, 12:53:37 am »
I can believe that the cost of passing render states by value is trivial and so there's no point worrying about it.

I am reluctant to believe the compiler can optimize away the copy in a pass by value in this case. Draw is a virtual function and it may not be known until run-time whether the states parameter is modified or not by the resolved function.

It comes down to the IDE you are using and the other parts like the compiler.  If I remember right both Code::Blocks and Visual Studio do a lot of the optimization that we'd have to have done manually years back.  It most likely can optimize things you wouldn't think of.
I have many ideas but need the help of others to find way to make use of them.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Why RenderStates is passed by value in draw function?
« Reply #22 on: December 27, 2014, 01:17:43 am »
StormWingDelta: What you said makes no sense and what you meant to say has already been said multiple times. An IDE (Integrated Development Environment) does absolutely not optimize anything, it doesn't even produce any kind of binary code. The compiler creates the binary code and does the optimization! By now you should really be able to tell the difference between an IDE and a compiler.

I am reluctant to believe the compiler can optimize away the copy in a pass by value in this case. Draw is a virtual function and it may not be known until run-time whether the states parameter is modified or not by the resolved function.
Programming is never about any kind of believe. Study, try, test, profile. The easiest way is really to go write reliable test cases. The harder way is to go study what the standard allows and what the compilers actually do (of course that only works for open source compilers). ;)

As for the discussion in general: Stop trying to optimize stuff which hasn't been proven to be a bottleneck in anyone's code.
« Last Edit: December 27, 2014, 01:19:24 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Why RenderStates is passed by value in draw function?
« Reply #23 on: December 30, 2014, 02:55:34 am »
I understand Nei's argument because the general rule is: use const reference for bigger objects, value for built-ins and small classes. The reason why SFML deviates here has been mentioned: the states are mostly modified, so code is simpler.

And Gambit, don't be rude just because you're on the same side as SFML team members. When you make a (bad) argument and are asked for clarification, then explain your point.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Why RenderStates is passed by value in draw function?
« Reply #24 on: December 30, 2014, 07:05:16 am »
I wasnt trying to be rude, I was just trying to get a point across.

 

anything