Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(stats): improve get_line_charts for ReadService #1037

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions stats/stats-server/src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,20 @@ impl LineChartCategory {
/// If the settings are not present - remove the chart (i.e. remove disabled
/// or nonexistent charts)
pub fn intersect_info(
self,
&self,
info: &BTreeMap<String, EnabledChartEntry>,
) -> proto_v1::LineChartSection {
let charts: Vec<_> = self
.charts_order
.into_iter()
.flat_map(|c: String| info.get(&c).map(|e| e.build_proto_line_chart_info(c)))
.iter()
.flat_map(|chart| {
info.get(chart)
.map(|e| e.build_proto_line_chart_info(chart.clone()))
})
.collect();
proto_v1::LineChartSection {
id: self.id,
title: self.title,
id: self.id.clone(),
title: self.title.clone(),
charts,
}
}
Expand Down
15 changes: 8 additions & 7 deletions stats/stats-server/src/read_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ fn map_read_error(err: ReadError) -> Status {
///
/// Returns `None` if info were not found for some chart.
fn add_chart_info_to_layout(
layout: Vec<types::LineChartCategory>,
chart_info: BTreeMap<String, EnabledChartEntry>,
layout: &[types::LineChartCategory],
Copy link
Contributor

@bragov4ik bragov4ik Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with chart_info, but I think it should be cheaper to perform 1 big clone instead of multiple small (equivalent) clones for layout

Copy link
Contributor Author

@yjhmelody yjhmelody Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are in heap,so i think its no difference,but only parts of fields need to be cloned,so i think it will be cheaper

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, probably there's no difference because of strings.

However, intersect_info effectively clones whole LineChartCategory, so I don't see a reason to use reference where move semantics fits ideally. I think it's a good practice to leave decision to clone or not for the caller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e.

  • id - cloned directly
  • title - cloned directly
  • charts_order - .iter() then clone on each element

chart_info: &BTreeMap<String, EnabledChartEntry>,
) -> Vec<proto_v1::LineChartSection> {
layout
.into_iter()
.map(|cat| cat.intersect_info(&chart_info))
.iter()
.map(|cat| cat.intersect_info(chart_info))
.collect()
}

Expand Down Expand Up @@ -280,9 +280,10 @@ impl StatsService for ReadService {
&self,
_request: Request<proto_v1::GetLineChartsRequest>,
) -> Result<Response<proto_v1::LineCharts>, Status> {
let layout = self.charts.lines_layout.clone();
let info = self.charts.charts_info.clone();
let sections = add_chart_info_to_layout(layout, info);
let sections = add_chart_info_to_layout(
self.charts.lines_layout.as_slice(),
&self.charts.charts_info,
);

Ok(Response::new(proto_v1::LineCharts { sections }))
}
Expand Down