alibaba / Sentinel

A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)
https://sentinelguard.io/
Apache License 2.0
22.45k stars 8.04k forks source link

[BUG] 避免字符串和MetricNode对象的转换,减少内存占用 #3464

Open brucelwl opened 1 month ago

brucelwl commented 1 month ago

https://github.com/alibaba/Sentinel/blob/195150bc745927429e9f14f501907310b46d702f/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/metric/MetricsReader.java#L54-L56

55行没必要为了获取时间戳而将字符串转成MetricNode对象, 直接通过字符串截取就行,

int timestampIndex = s.indexOf("|");
String timestamp = Long.parseLong(s.substring(0, timestampIndex));

https://github.com/alibaba/Sentinel/blob/195150bc745927429e9f14f501907310b46d702f/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/SendMetricCommandHandler.java#L97-L100 SendMetricCommandHandler中为了得到ThinString, 又将MetricNode转成字符串, MetricNode成了冗余转换, 会增加内存占用 不如直接采用FatString字符串分割

int timestampIndex = s.indexOf("|");
String timestamp = s.substring(0, timestampIndex);

int i = s.indexOf("|", timestampIndex + 20);
String metric = s.substring(i);

System.out.println(timestamp + metric);