Skip to contentSkip to conversation

sched/topology: One function call less in build_group_from_child_sched_domain()

PATCH In review Markus Elfring 4 participants 7 messages Jul 6, 2019 – Jul 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/topology: One function call less in build_group_from_child_sched_domain()

From: Markus Elfring <[email protected]>
Date: Sat, 6 Jul 2019 16:00:13 +0200

Avoid an extra function call by using a ternary operator instead of a conditional statement.

This issue was detected by using the Coccinelle software.

---
 kernel/sched/topology.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/topology.c +1 -5
index f751ce0b783e..6190eb52c30a 100644
@@ -886,11 +886,7 @@ build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 		return NULL;
 
 	sg_span = sched_group_span(sg);
-	if (sd->child)
-		cpumask_copy(sg_span, sched_domain_span(sd->child));
-	else
-		cpumask_copy(sg_span, sched_domain_span(sd));
-
+	cpumask_copy(sg_span, sched_domain_span(sd->child ? sd->child : sd));
 	atomic_inc(&sg->ref);
 	return sg;
 }

--
2.22.0

#1 lore raw
Quoted 18 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 6 Jul 2019 16:00:13 +0200

Avoid an extra function call by using a ternary operator instead of a conditional statement.

This issue was detected by using the Coccinelle software.

---
 kernel/sched/topology.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/topology.c +0 -0
index f751ce0b783e..6190eb52c30a 100644

kernel/sched/topology.c build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) · line 889

Quoted 9 lines
kernel/sched/topology.c +1 -5
@@ -886,11 +886,7 @@ build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 		return NULL;
 
 	sg_span = sched_group_span(sg);
-	if (sd->child)
-		cpumask_copy(sg_span, sched_domain_span(sd->child));
-	else
-		cpumask_copy(sg_span, sched_domain_span(sd));
-
+	cpumask_copy(sg_span, sched_domain_span(sd->child ? sd->child : sd));

At runtime, Are we avoiding a function call?
However I think we are avoiding a branch instead of a conditional, which may be beneficial.

Quoted 6 lines
kernel/sched/topology.c +0 -1
 	atomic_inc(&sg->ref);
 	return sg;
 }
--

2.22.0

Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#2 lore raw
Quoted 22 lines · Srikar Dronamraju
Quoted 20 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 6 Jul 2019 16:00:13 +0200

Avoid an extra function call by using a ternary operator instead of a conditional statement.

This issue was detected by using the Coccinelle software.

---
 kernel/sched/topology.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/topology.c +0 -0
index f751ce0b783e..6190eb52c30a 100644
@@ -886,11 +886,7 @@ build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 		return NULL;
 
Quoted 11 lines

kernel/sched/topology.c build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) · line 889

Quoted 7 lines
kernel/sched/topology.c +1 -5
 	sg_span = sched_group_span(sg);
-	if (sd->child)
-		cpumask_copy(sg_span, sched_domain_span(sd->child));
-	else
-		cpumask_copy(sg_span, sched_domain_span(sd));
-
+	cpumask_copy(sg_span, sched_domain_span(sd->child ? sd->child : sd));

At runtime, Are we avoiding a function call?
However I think we are avoiding a branch instead of a conditional, which may be beneficial.

If you're assuming the compiler doesn't already optimize that (no idea whether gcc really does that).

@Markus: could you check what gcc is actually generating out of both the old and your new version ?

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#3 lore raw
Quoted 5 lines · Enrico Weigelt, metux IT consult
Quoted 4 lines

At runtime, Are we avoiding a function call?
However I think we are avoiding a branch instead of a conditional, which may be beneficial.

Quoted 7 lines

If you're assuming the compiler doesn't already optimize that (no idea whether gcc really does that).

@Markus: could you check what gcc is actually generating out of both the old and your new version ?

I had already tried looking at the object files both on X86 and PowerPc and in both cases (with and without patch) the generated code differs.

Quoted 7 lines

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
maintainer
#4 lore raw
In reply to Srikar Dronamraju
Quoted 22 lines · Srikar Dronamraju
Quoted 20 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 6 Jul 2019 16:00:13 +0200

Avoid an extra function call by using a ternary operator instead of a conditional statement.

This issue was detected by using the Coccinelle software.

---
 kernel/sched/topology.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/topology.c +0 -0
index f751ce0b783e..6190eb52c30a 100644
@@ -886,11 +886,7 @@ build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 		return NULL;
 
Quoted 11 lines

kernel/sched/topology.c build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) · line 889

Quoted 7 lines
kernel/sched/topology.c +1 -5
 	sg_span = sched_group_span(sg);
-	if (sd->child)
-		cpumask_copy(sg_span, sched_domain_span(sd->child));
-	else
-		cpumask_copy(sg_span, sched_domain_span(sd));
-
+	cpumask_copy(sg_span, sched_domain_span(sd->child ? sd->child : sd));

At runtime, Are we avoiding a function call?
However I think we are avoiding a branch instead of a conditional, which may be beneficial.

It all depends on what the compiler does; also this is super slow path stuff and the patch makes code less readable (IMO).

#5 lore raw
Quoted 35 lines · Peter Zijlstra
Quoted 22 lines · Srikar Dronamraju
Quoted 20 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 6 Jul 2019 16:00:13 +0200

Avoid an extra function call by using a ternary operator instead of a conditional statement.

This issue was detected by using the Coccinelle software.

---
 kernel/sched/topology.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/topology.c +0 -0
index f751ce0b783e..6190eb52c30a 100644
@@ -886,11 +886,7 @@ build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 		return NULL;
 
Quoted 11 lines

kernel/sched/topology.c build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) · line 889

Quoted 7 lines
kernel/sched/topology.c +1 -5
 	sg_span = sched_group_span(sg);
-	if (sd->child)
-		cpumask_copy(sg_span, sched_domain_span(sd->child));
-	else
-		cpumask_copy(sg_span, sched_domain_span(sd));
-
+	cpumask_copy(sg_span, sched_domain_span(sd->child ? sd->child : sd));

At runtime, Are we avoiding a function call?
However I think we are avoiding a branch instead of a conditional, which may be beneficial.

Quoted 3 lines

It all depends on what the compiler does; also this is super slow path stuff and the patch makes code less readable (IMO).

Yes, it definitely makes code readable. I was only commenting on the changelog/subject which says avoids a function call which I think it doesn't.

Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#6 lore raw
Quoted 1 line

Yes, it definitely makes code readable.

Some developers seem to get difficulties with this view.

Quoted 2 lines

I was only commenting on the changelog/subject which says avoids a function call which I think it doesn't.

Do you prefer an other wording for calling functions only once with a selected parameter value instead of calling them (unoptimised) within two branches of an if statement?

Regards,
Markus