SamYuan1990 commented on PR #2911:
URL: https://github.com/apache/brpc/pull/2911#issuecomment-2705705399

   > ```
   > const std::string& AdaptiveMaxConcurrency::UNLIMITED() {
   >     static const std::string s = "unlimited";
   >     return s;
   > }
   > 
   > const std::string& AdaptiveMaxConcurrency::CONSTANT() {
   >     static const std::string s = "constant";
   >     return s;
   > }
   > ```
   > 
   > 1.This way of initialization is much more common,uncontrolled use of 
global variables may cause dependency order problems(UB). 2.These short strings 
can be optimized to be stored on the stack using the SSO(Small String 
Optimization) mechanism of std::string. 3.The original lazy initialization 
mechanism is retained to reduce unnecessary memory usage. 4.It is thread-safe.
   
   Hi @zhangqiongyu , 
   
   I hope you're doing well! I wanted to check if this is a maintainer change 
request, as it doesn't seem to be in a CR. I also tried searching via [this 
link](https://github.com/search?q=repo%3Aapache%2Fbrpc+zhangqiongyu&type=commits)
 but didn't find anything relevant.
   
   I'm relatively new to C++, and I'm here to learn from everyone. If I make 
any mistakes, please feel free to correct me—I really appreciate it!
   
   I believe we both agree on using `const std::string` instead of `static 
std::string* s = new` to ensure thread safety. However, I'm curious about 
whether we should use lazy initialization or not. When would be the best time 
to initialize this variable?
   
   Initially, I considered using the following approach:
   ```cpp
   const std::string& AdaptiveMaxConcurrency::UNLIMITED() {
   static const std::string s = "unlimited";
   return s;
   }
   
   const std::string& AdaptiveMaxConcurrency::CONSTANT() {
   static const std::string s = "constant";
   return s;
   }
   ```
   But when I realized that the function simply returns a constant string, I 
thought about making a broader change.
   
   From what I understand, the differences are:
   - Defining a `const` in a class affects the scope of the class and its 
instances (and possibly all child classes and their instances). It’s unique 
across all instances.
   
   Regarding memory usage:
   As far as I know, and as mentioned in the PR description, compiler 
optimization is a bit beyond my current knowledge. Since it's a short string, 
it might benefit from SSO (Short String Optimization). However, I'm not 
entirely sure how the compiler will handle it.
   
   I’m aware of the concept of dependency order problems (UB), but I lack 
practical experience in C++. Could you provide an example in this context?
   
   Regarding performance, when invoking this value from other parts of the 
code, previously it involved a function call. After changing it to a `const`, 
it becomes a direct access to a constant. From my understanding, if the 
compiler optimizes a function call that returns a static value, it should be 
equivalent to accessing a constant. If the function isn't loaded onto the 
stack, it might involve a page change/reload. If SSO is in play, the string 
would be on the stack in either case (function call or `const`).
   
   ```cpp
   const std::string& AdaptiveMaxConcurrency::UNLIMITED() {
   static const std::string s = "unlimited";
   return s;
   }
   ```
   
   I’d love to hear your thoughts on this! Thank you for your time and 
guidance. 😊


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to