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

Extract FPD Merge To Separate Package #3533

Merged
merged 4 commits into from
Mar 11, 2024

Conversation

SyntaxNode
Copy link
Contributor

Pulling out this code for potential reuse and to isolate this behavior from FPD logic. Changed the function signature to be a pure method, but is functionality identical.

Realized some of the clone methods are expensive and most are likely not necessary for the merge. This will get worse as I look to introduce an imp merge function as the imp structure is much deeper.

I'm going to investigate a new merge approach which will only clone data present in the json override. Need to benchmark if the lazy cloner would be overall more efficient. Certainly would produce a lot less allocations, just need to verify the CPU cost isn't much higher.

}

// user memory protect test
func TestMergeUserMemoryProtection(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the memory protection tests entirely in favor of updating the main tests.

"github.com/prebid/prebid-server/v2/util/jsonutil"
)

func App(v *openrtb2.App, overrideJSON json.RawMessage) (*openrtb2.App, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since Go requires a package name in the function invocation, this will always be used as merge.App which looks nice IMHO. Thoughts?

originalApp := ortb.CloneApp(&test.givenApp)
merged, err := App(&test.givenApp, test.givenJson)

assert.Equal(t, &test.givenApp, originalApp)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is my attempt at rolling in the memory protection tests. Clones the given request and verifies it hasn't changed after the call to the merge methods. Need a sanity check on this approach by those reviewing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Checked, app site and user look right!

@bsardo bsardo assigned guscarreon and unassigned bsardo Feb 28, 2024
return nil, err
} else {
newUser = mergedUser
Copy link
Contributor

Choose a reason for hiding this comment

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

When err != nil evaluates to true we end up returning nil in both branches, do we really need mergedUser as a separate variable? We could even spare the else clause:

201     if fpdConfigUser != nil {
202 -       if mergedUser, err := merge.User(newUser, fpdConfigUser); err != nil {
    +       if newUser, err := merge.User(newUser, fpdConfigUser); err != nil {
203             if err == merge.ErrBadOverride {
204                 return nil, ErrBadFPD
205             }
206             return nil, err
207 -       } else {
208 -           newUser = mergedUser
209         }
210     }
firstpartydata/first_party_data.go

But I see this pattern repeated in other places below (mergedSite and mergedApp) should we modify to make it leaner? Also, why do merge.User, merge.Site and merge.App are now returning pointers? Is it to check for shared memory issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I see this pattern repeated in other places below (mergedSite and mergedApp) should we modify to make it leaner?

Yes. My original intent was to not assign mergedUser if there is an error, but if there is an error there is no return path for newUser. Updated.

Also, why do merge.User, merge.Site and merge.App are now returning pointers?

They always did this. Before, they changed the memory address of the input pointer. Now it returns a separate pointer and leaves the input alone.

Copy link
Contributor

@guscarreon guscarreon left a comment

Choose a reason for hiding this comment

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

LGTM

@SyntaxNode SyntaxNode merged commit 40fd4ff into prebid:master Mar 11, 2024
3 checks passed
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