Skip to content

Commit

Permalink
Merge pull request #407 from marco-prontera/perf/#406
Browse files Browse the repository at this point in the history
[#406] Improve library performance when creating publisher restrictions
  • Loading branch information
sevriugin authored Aug 31, 2023
2 parents bb94e0b + 046f72d commit 89a93da
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 683 deletions.
38 changes: 18 additions & 20 deletions modules/core/src/encoder/field/PurposeRestrictionVectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export class PurposeRestrictionVectorEncoder {
// if the vector is empty we'll just return a string with just the numRestricitons being 0
if (!prVector.isEmpty()) {

const gvlVendorIds = Array.from(prVector.gvl.vendorIds);

const gvlHasVendorBetween = (vendorId: number, nextVendorId: number): boolean => {

const firstIndex = gvlVendorIds.indexOf(vendorId);
const nextIndex = gvlVendorIds.indexOf(nextVendorId);

const res = nextIndex - firstIndex;

return res > 1;

};

// create each restriction group
prVector.getRestrictions().forEach((purpRestriction: PurposeRestriction): void => {

Expand Down Expand Up @@ -44,23 +57,10 @@ export class PurposeRestrictionVectorEncoder {

}

// we know that `len` is greater than zero because we entered the loop
const lastVendorId = vendors[len - 1];
const gvlVendorIds = prVector.gvl.vendorIds;

const nextGvlVendor = (vendorId: number): number => {

while (++vendorId <= lastVendorId && !gvlVendorIds.has(vendorId)) {
}

return vendorId;

};

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

/**
* it's a range entry if we've got something other than the start
Expand Down Expand Up @@ -141,15 +141,13 @@ export class PurposeRestrictionVectorEncoder {

}

for ( let k: number = startOrOnlyVendorId; k <= endVendorId; k++) {

vector.add(k, purposeRestriction);

}
// required to preserve the default behavior (includes also vendors ids that doesn't exist)
const vendorIds = Array.from({length: endVendorId - startOrOnlyVendorId + 1}, (_, index) => startOrOnlyVendorId + index);
vector.restrictPurposeToLegalBasis(purposeRestriction, vendorIds);

} else {

vector.add(startOrOnlyVendorId, purposeRestriction);
vector.restrictPurposeToLegalBasis(purposeRestriction, [startOrOnlyVendorId]);

}

Expand Down
Loading

0 comments on commit 89a93da

Please sign in to comment.