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

feat: update loops and add publish workflows #2

Merged
merged 5 commits into from
Jun 3, 2024
Merged

Conversation

adithyaakrishna
Copy link
Collaborator

Description:

  • Added npm publish workflow (Needs NPM Secret to be added to settings)
  • Use .env for API Keys/Secrets
  • Use for loops instead of forEach for improved efficiency and other teeny tiny tidbits

Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
@adithyaakrishna adithyaakrishna self-assigned this Jun 3, 2024
Comment on lines -80 to +90
passages.forEach((item) => {
const match = item.match(/\[(\d+)\]/g);
for (let i = 0; i < passages.length; i++) {
const match = passages[i].match(/\[(\d+)\]/g);
if (match) {
const index = parseInt(match[0].replace(/\[|\]/g, ""));
result.push(list[index - 1][idKey]);
const index = parseInt(match[0].replace(/\[|\]/g, ""), 10);
if (index > 0 && index <= list.length) {
// Check to ensure index is within bounds to prevent index -1/out of bounds error.
result.push(list[index - 1][idKey]);
}
}
});
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • Added a check to ensure that the index derived from the match is within the bounds of the list array
  • Using for as it recdces the overhead of calling a function for each item in passages.

Comment on lines -11 to +20
rankLists.forEach((list) => {
list.forEach((item, index) => {
const rrfScore = 1 / (c + index + 1); // index is zero-based, add 1 for correct rank
const currentScore = scores.get(item[idKey] as string) || 0;
scores.set(item[idKey] as string, currentScore + rrfScore);
});
});
for (let i = 0; i < rankLists.length; i++) {
const list = rankLists[i];
for (let index = 0; index < list.length; index++) {
const item = list[index];
const itemId = item[idKey] as string; // Cast once and reuse
const rrfScore = 1 / (c + index + 1); // Compute RRF score
const currentScore = scores.get(itemId) || 0; // Get current score, default to 0
scores.set(itemId, currentScore + rrfScore); // Update score
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • The conversion item[idKey] as string is done twice per item, which is redundant, so IMO we can compute it once and reuse it 😶
  • Minimize map accesses as reducing the amount of ops can give more perf. benefits
  • Used for loops here as well instead of forEach

Comment on lines -127 to -132
ranks.forEach((rank) => {
let match = batch.filter((item) => item.startsWith(rank));
if (match.length > 0) {
result.push(match[0]);
for (let i = 0; i < ranks.length; i++) {
const rank = ranks[i];
let matchFound = false;
for (let j = 0; j < batch.length; j++) {
if (batch[j].startsWith(rank)) {
result.push(batch[j]);
matchFound = true;
break;
}
}
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  • We can avoid using filter inside a for loop as it iterates over the entire batch for each rank
  • Used break to exit early in case so that we prevent unnecessary iterations

Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
@lucasjacks0n lucasjacks0n merged commit 7f95208 into main Jun 3, 2024
@adithyaakrishna
Copy link
Collaborator Author

Thanks @lucasjacks0n :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants