Skip to content

Commit

Permalink
perf: improve performance by skip unuseful loops
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-prontera committed Dec 14, 2023
1 parent 58df666 commit 4be6f99
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions modules/core/src/encoder/field/PurposeRestrictionVectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ export class PurposeRestrictionVectorEncoder {

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

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

const firstIndex = gvlVendorIds.indexOf(vendorId);
const lastIndex = gvlVendorIds.indexOf(lastVendorId);

if (lastIndex - firstIndex > 0) {

const nextIndex = gvlVendorIds.indexOf(vendorId + 1);
return gvlVendorIds[nextIndex];

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

}

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

};

Expand Down Expand Up @@ -62,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) {

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

0 comments on commit 4be6f99

Please sign in to comment.