Skip to content

fix: nested ctype loading #931

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

Merged
merged 17 commits into from
Mar 5, 2025
Merged

fix: nested ctype loading #931

merged 17 commits into from
Mar 5, 2025

Conversation

rflechtner
Copy link
Contributor

@rflechtner rflechtner commented Jan 16, 2025

Fixes https://github.com/KILTprotocol/ticket/issues/3632

fork of #919

Integration tests are not currently working (#944)

Checklist:

  • I have verified that the code works
  • I have verified that the code is easy to understand
    • If not, I have left a well-balanced amount of inline comments
  • I have left the code in a better state
  • I have documented the changes (where applicable)
    • Either PR or Ticket to update the Docs
    • Link the PR/Ticket here

@rflechtner rflechtner marked this pull request as ready for review February 17, 2025 18:06
@rflechtner rflechtner requested a review from abdulmth February 17, 2025 18:06
@rflechtner
Copy link
Contributor Author

@aybarsayan I moved nested CType loading to CTypeLoader.ts and saw an opportunity to reuse the caching CType loader for combining the CType definitions in validateSubjects cTypes parameter with its loadCTypes parameter.

@rflechtner rflechtner mentioned this pull request Feb 18, 2025
5 tasks
Copy link
Member

@Dudleyneedham Dudleyneedham left a comment

Choose a reason for hiding this comment

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

Minor comments and request to revert a name change.

}

// Process all values in the object. Also works for arrays
await Promise.all(Object.values(value).map(extractRefsFrom))
Copy link
Member

Choose a reason for hiding this comment

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

I had asked chat about this.

Not sure if we care to much about proformance. However, it gave me an interesting insight into this Promise.all and map.

await Promise.all(Object.values(value).map(extractRefsFrom))
  • The issue is that extractRefsFrom is async, but map() does not handle async functions well—it creates a list of Promise<void>, which Promise.all then waits for.
  • While this works in many cases, it may cause performance issues if many deep CType references are being processed simultaneously.
  • Solution: You may consider sequential processing for deeply nested structures using for...of:
for (const subValue of Object.values(value)) {
  await extractRefsFrom(subValue)
}

This prevents too many concurrent fetches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bizarre that chad thinks performance would be better if we process them sequentially. Async code exists exactly for this use case, where it doesn't make much sense to block execution while waiting for the full node to return historic blocks. In case there are issues with making too many requests at the same time, I'd recommend rate limiting on the ctype loader side, but not here.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it is strange. There wasn't any issues on my side, but I like to run it through chat afterwards. Seeing what interesting things it comes up with. I sent the test results. It basically a pointless comment without the context of what we are doing in the project.

@@ -32,9 +37,69 @@ export function newCachingCTypeLoader(
})

async function getCType(id: ICType['$id']): Promise<ICType> {
Copy link
Member

Choose a reason for hiding this comment

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

Randomly adding more soundness checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved them from the code for loading nested ctype definitions, as they seemed misplaced there.

Copy link
Member

Choose a reason for hiding this comment

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

Ok fair enough.

Copy link
Member

@Dudleyneedham Dudleyneedham left a comment

Choose a reason for hiding this comment

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

LGTM

}

// Process all values in the object. Also works for arrays
await Promise.all(Object.values(value).map(extractRefsFrom))
Copy link
Member

Choose a reason for hiding this comment

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

Yes, it is strange. There wasn't any issues on my side, but I like to run it through chat afterwards. Seeing what interesting things it comes up with. I sent the test results. It basically a pointless comment without the context of what we are doing in the project.

@@ -32,9 +37,69 @@ export function newCachingCTypeLoader(
})

async function getCType(id: ICType['$id']): Promise<ICType> {
Copy link
Member

Choose a reason for hiding this comment

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

Ok fair enough.

@rflechtner rflechtner merged commit 459dbe6 into develop Mar 5, 2025
13 of 15 checks passed
@rflechtner rflechtner deleted the fix-nested-ctype-loading branch March 5, 2025 14:18
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.

3 participants