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>
This commit is contained in:
parent
84f0eec62e
commit
ee21d397d2
1 changed files with 13 additions and 6 deletions
|
@ -128,16 +128,18 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
|
||||||
void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
|
void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
|
||||||
struct video_frmival_enum *match)
|
struct video_frmival_enum *match)
|
||||||
{
|
{
|
||||||
uint64_t best_diff_nsec = INT32_MAX;
|
|
||||||
struct video_frmival desired = match->discrete;
|
struct video_frmival desired = match->discrete;
|
||||||
struct video_frmival_enum fie = {.format = match->format};
|
struct video_frmival_enum fie = {.format = match->format};
|
||||||
|
uint64_t best_diff_nsec = INT32_MAX;
|
||||||
|
uint64_t goal_nsec = video_frmival_nsec(&desired);
|
||||||
|
|
||||||
__ASSERT(match->type != VIDEO_FRMIVAL_TYPE_STEPWISE,
|
__ASSERT(match->type != VIDEO_FRMIVAL_TYPE_STEPWISE,
|
||||||
"cannot find range matching the range, only a value matching the range");
|
"cannot find range matching the range, only a value matching the range");
|
||||||
|
|
||||||
for (fie.index = 0; video_enum_frmival(dev, ep, &fie) == 0; fie.index++) {
|
for (fie.index = 0; video_enum_frmival(dev, ep, &fie) == 0; fie.index++) {
|
||||||
struct video_frmival tmp = {0};
|
struct video_frmival tmp = {0};
|
||||||
uint64_t diff_nsec = 0, a, b;
|
uint64_t diff_nsec = 0;
|
||||||
|
uint64_t tmp_nsec;
|
||||||
|
|
||||||
switch (fie.type) {
|
switch (fie.type) {
|
||||||
case VIDEO_FRMIVAL_TYPE_DISCRETE:
|
case VIDEO_FRMIVAL_TYPE_DISCRETE:
|
||||||
|
@ -150,13 +152,18 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
|
||||||
__ASSERT(false, "invalid answer from the queried video device");
|
__ASSERT(false, "invalid answer from the queried video device");
|
||||||
}
|
}
|
||||||
|
|
||||||
a = video_frmival_nsec(&desired);
|
tmp_nsec = video_frmival_nsec(&tmp);
|
||||||
b = video_frmival_nsec(&tmp);
|
diff_nsec = tmp_nsec > goal_nsec ? tmp_nsec - goal_nsec : goal_nsec - tmp_nsec;
|
||||||
diff_nsec = a > b ? a - b : b - a;
|
|
||||||
if (diff_nsec < best_diff_nsec) {
|
if (diff_nsec < best_diff_nsec) {
|
||||||
best_diff_nsec = diff_nsec;
|
best_diff_nsec = diff_nsec;
|
||||||
match->index = fie.index;
|
match->index = fie.index;
|
||||||
memcpy(&match->discrete, &tmp, sizeof(tmp));
|
match->discrete = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diff_nsec == 0) {
|
||||||
|
/* Exact match, stop searching a better match */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue