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

perf: improve performance of purpose restriction encoder [encode] #424

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions modules/core/src/encoder/field/PurposeRestrictionVectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ export class PurposeRestrictionVectorEncoder {
// if the vector is empty we'll just return a string with just the numRestricitons being 0
if (!prVector.isEmpty()) {

const nextGvlVendor = (vendorId, lastVendorId): number => {
const gvlVendorIds = Array.from(prVector.gvl.vendorIds);

for (let nextId = vendorId + 1; nextId <= lastVendorId; nextId++) {
const nextGvlVendor = (vendorId, lastVendorId) => {

if (prVector.gvl.vendorIds.has(nextId)) {
const firstIndex = gvlVendorIds.indexOf(vendorId);
const lastIndex = gvlVendorIds.indexOf(lastVendorId);

return nextId;
if (lastIndex - firstIndex > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could this one be true if first index is equal -1 -> not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that condition is required only to check if we are at the "end" of the used GVL vendor list in fact in line 26 you see we are taking the nextIndex the one that is specified in the GVL vendor list.
This makes possible to skip every extra loop in the purpose restriction encode process.


}
const nextIndex = gvlVendorIds.indexOf(vendorId + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

vendorId + 1 could be out of range if vendorId is the last vendor in the GVL, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the vendorId is the last vendor in the GVL this block won't be executed, in fact the vendorId is the latest this function will return itself as "special case". See return statement at line 35


return {
nextVendorId: gvlVendorIds[nextIndex],
index: gvlVendorIds[firstIndex + 1],
};

}

return vendorId;
return {
nextVendorId: vendorId,
index: vendorId,
};

};

Expand Down Expand Up @@ -60,10 +69,28 @@ export class PurposeRestrictionVectorEncoder {

}

let isRangeEncodeRequired = i === len - 1;

if (!isRangeEncodeRequired) {

const {nextVendorId, index} = nextGvlVendor(vendorId, vendors[len - 1]);

if (vendors[i + 1] > nextVendorId) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

vendors[i+1] is always defined ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, in fact if it is not defined the relevant block of code will not be executed. See the condition at line 74 and the condition at line 61


isRangeEncodeRequired = true;

} else if (index > i && index < len) {

i = index;

}

}

/**
* either end of the loop or there are GVL vendor IDs before the next one
*/
if (i === len - 1 || vendors[i + 1] > nextGvlVendor(vendorId, vendors[len - 1])) {
if (isRangeEncodeRequired) {

/**
* it's a range entry if we've got something other than the start
Expand Down
Loading