This is an automated email from the ASF dual-hosted git repository.

Alanxtl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/develop by this push:
     new a7b6ee632 perf(common): optimize URL.String() locking and string 
building (#3404)
a7b6ee632 is described below

commit a7b6ee632a2d2e4c908cd5c10434d062c5040fe8
Author: wm_03 <[email protected]>
AuthorDate: Sat Jun 13 14:25:15 2026 +0800

    perf(common): optimize URL.String() locking and string building (#3404)
---
 common/url.go | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/common/url.go b/common/url.go
index 462f4ee36..30ac192df 100644
--- a/common/url.go
+++ b/common/url.go
@@ -395,17 +395,36 @@ func isMatchCategory(category1 string, category2 string) 
bool {
 }
 
 func (c *URL) String() string {
-       c.paramsLock.Lock()
-       defer c.paramsLock.Unlock()
+       c.paramsLock.RLock()
+       defer c.paramsLock.RUnlock()
+
+       encodedParams := c.params.Encode()
        var buf strings.Builder
-       if len(c.Username) == 0 && len(c.Password) == 0 {
-               buf.WriteString(fmt.Sprintf("%s://%s:%s%s", c.Protocol, c.Ip, 
c.Port, c.Path))
-       } else {
-               buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s", c.Protocol, 
c.Username, c.Password, c.Ip, c.Port, c.Path))
+
+       size := len(c.Protocol) + len("://") + len(c.Ip) + len(":") + 
len(c.Port) + len(c.Path)
+       if len(c.Username) != 0 || len(c.Password) != 0 {
+               size += len(c.Username) + len(":") + len(c.Password) + len("@")
+       }
+       if encodedParams != "" {
+               size += len("?") + len(encodedParams)
+       }
+
+       buf.Grow(size)
+       buf.WriteString(c.Protocol)
+       buf.WriteString("://")
+       if len(c.Username) != 0 || len(c.Password) != 0 {
+               buf.WriteString(c.Username)
+               buf.WriteString(":")
+               buf.WriteString(c.Password)
+               buf.WriteString("@")
        }
-       if encoded := c.params.Encode(); encoded != "" {
+       buf.WriteString(c.Ip)
+       buf.WriteString(":")
+       buf.WriteString(c.Port)
+       buf.WriteString(c.Path)
+       if encodedParams != "" {
                buf.WriteByte('?')
-               buf.WriteString(encoded)
+               buf.WriteString(encodedParams)
        }
        return buf.String()
 }

Reply via email to