16
General discussions / Re: Shader uniform API -- part II
« on: August 29, 2015, 02:38:53 pm »If you have a const char* originally and pass it to this function, a std::string is constructed to match the const reference std::string required, then it is destroyed. Depending on the size of the string content and the STL implementation, you do pay more than if you just wanted the function to read your original const char* (in particular if the small-buffer-optimization can't be used, the string will allocate and deallocate, which you want to avoid most of the time in a critical loop).If you're using std::string instead of char* to begin with, this would avoid this scenario.
Of course. I said so in the later paragraph. If you look at the example too, there is a common case where you think you use std::string but you don't
Quote from: klaim
Anyway I don't have enough experience with shader to know if it there will be performance concerns here. It just feel to me that the values will be passed often (once per frame?) and if someone do
setUniformFloat( "my_value_have_a_long_name", 42.0f );
each frame, they are paying something each frame which they might think is not a lot. To escape this issue one would make sure to store these names in exactly const std::string (not a sfml::String nor any other type to avoid the copy construction).
Also, even if you use static const std::string for fixed names, you also pay the cost of std::string for a compile-time value. So you still pay for something (allocation). And if your string values are stored in a big string buffer, then you have to build std::strings for that.
It really depend on what kidn of code this is about of course, so it might not be important here, I'm not sure, but so far I thought this kind of api was called a lot.
Though again it's a minor concern for now I guess?