Skip to content

Commit b08d533

Browse files
committed
drivers: video: common: optimizations for video_closest_frmival()
In video_closest_frmival(), immediately stop searching when an exact match is found, as a small performance optimization. Variables that could be computed only once were moved further outside. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 426cefd commit b08d533

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

boards/arduino/nicla_vision/arduino_nicla_vision_stm32h747xx_m7.dts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ zephyr_udc0: &usbotg_hs {
225225

226226
port {
227227
gc2145_ep_out: endpoint {
228-
remote-endpoint = <&dcmi_ep_in>;
228+
remote-endpoint-label = "dcmi_ep_in";
229229
};
230230
};
231231

@@ -252,7 +252,7 @@ zephyr_udc0: &usbotg_hs {
252252

253253
port {
254254
dcmi_ep_in: endpoint {
255-
remote-endpoint = <&gc2145_ep_out>;
255+
remote-endpoint-label = "gc2145_ep_out";
256256
};
257257
};
258258
};

drivers/video/video_common.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,18 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
128128
void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
129129
struct video_frmival_enum *match)
130130
{
131-
uint64_t best_diff_nsec = INT32_MAX;
132131
struct video_frmival desired = match->discrete;
133132
struct video_frmival_enum fie = {.format = match->format};
133+
uint64_t best_diff_nsec = INT32_MAX;
134+
uint64_t goal_nsec = video_frmival_nsec(&desired);
134135

135136
__ASSERT(match->type != VIDEO_FRMIVAL_TYPE_STEPWISE,
136137
"cannot find range matching the range, only a value matching the range");
137138

138139
for (fie.index = 0; video_enum_frmival(dev, ep, &fie) == 0; fie.index++) {
139140
struct video_frmival tmp = {0};
140-
uint64_t diff_nsec = 0, a, b;
141+
uint64_t diff_nsec = 0;
142+
uint64_t tmp_nsec;
141143

142144
switch (fie.type) {
143145
case VIDEO_FRMIVAL_TYPE_DISCRETE:
@@ -150,13 +152,18 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
150152
__ASSERT(false, "invalid answer from the queried video device");
151153
}
152154

153-
a = video_frmival_nsec(&desired);
154-
b = video_frmival_nsec(&tmp);
155-
diff_nsec = a > b ? a - b : b - a;
155+
tmp_nsec = video_frmival_nsec(&tmp);
156+
diff_nsec = tmp_nsec > goal_nsec ? tmp_nsec - goal_nsec : goal_nsec - tmp_nsec;
157+
156158
if (diff_nsec < best_diff_nsec) {
157159
best_diff_nsec = diff_nsec;
158160
match->index = fie.index;
159-
memcpy(&match->discrete, &tmp, sizeof(tmp));
161+
match->discrete = tmp;
162+
}
163+
164+
if (diff_nsec == 0) {
165+
/* Exact match, stop searching a better match */
166+
break;
160167
}
161168
}
162169
}

0 commit comments

Comments
 (0)