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

Find bid duration in linear time #2805

Merged
merged 8 commits into from
Jun 15, 2023
Merged

Find bid duration in linear time #2805

merged 8 commits into from
Jun 15, 2023

Conversation

guscarreon
Copy link
Contributor

We currently sort the targeting.DurationRange array in order to pick a duration that better resembles the bid.BidVideo.Duration. This results in O(nlogn) time and O(n) space because we copy the targeting.DurationRange slice before sorting. This pull requests modifies to get the same result in O(n) time and O(1) space.

assert.Equal(t, tc.expected.dur, newDur, tc.desc)
assert.Equal(t, tc.expected.err, err, tc.desc)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good and this change makes sense!
May you please modify existing applyCategoryMapping unit test to test this change in the main flow, for instance:
In TestCategoryMappingBidderNameNoCategories change next lines:
L3185: requestExt.Prebid.Targeting.DurationRangeSec = []int{30, 10, 25, 5, 20, 50}
then input bids (L3194 and 3195):

bid1_1 :=  ...  &openrtb_ext.ExtBidPrebidVideo{Duration: 17} ...
bid1_2 :=  ... &openrtb_ext.ExtBidPrebidVideo{Duration: 8}, ...

And then result asserts should be (L3217 and 3218):

assert.Equal(t, "10.00_20s_bidder1", bidCategory["bid_id1"], "Category mapping doesn't match")
assert.Equal(t, "12.00_10s_bidder2", bidCategory["bid_id2"], "Category mapping doesn't match")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@AlexBVolcy AlexBVolcy left a comment

Choose a reason for hiding this comment

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

A couple of minor comments. Looks good though!

}
}
if !madeSelection && len(a) > 0 {
err = errors.New("Bid duration exceeds maximum allowed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Very minor nitpick: I think error messages are supposed to be entirely lowercase? I could be wrong on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified

// findDurationRange returns the element in the array 'a' that is both greater than 'dur' and closest
// in value to 'dur' unless a value equal to 'dur' is found. Returns an error if all elements in 'a'
// are less than 'dur'.
func findDurationRange(dur int, a []int) (int, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could the array variable a have a more descriptive name? Or are you keeping it more general in case this function is re-used in another place? If that's the case, could this function be moved to utils.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to durationRanges

@bsardo bsardo merged commit d9a31e7 into master Jun 15, 2023
3 checks passed
@SyntaxNode SyntaxNode deleted the durationRange branch October 2, 2023 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants