Skip to content

feat: improve performance and type-safety #83

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

michaeladler
Copy link
Contributor

Summary

This may seem like a mundane merge request, but it delivers significant performance improvements. Benchmarks show that this library now outperforms jsonld.js.

Note: While the changes may appear straightforward, caution is required when implementing them. It is sometimes necessary to differentiate between a value explicitly set to null in the context and one that is simply absent.

PS: I don't have any other changes lined up (I couldn't submit this PR earlier because it builds upon #82 to avoid merge conflicts).

Details:

  • Replace maps with structs to achieve significant performance gains.
  • Improve type-safety. Previously, type confusion resulted in unnecessary checks, such as comparing a known string value against a boolean.
  • Ensure defaultLanguage always contains a language. Previously, a missing if-check could result in defaultLanguage being set to "_%s", with %s being the direction.
  • Remove GetKeysString since it's not used anywhere and is equivalent to the generic GetKeys now.

Basic example

No change in behavior.

Motivation

Performance! Additionally, the struct-based design enhances code navigation/reasoning (especially when using an LSP).

Checks

  • Passes make test

- Replace maps with structs to achieve significant performance gains.
  (Benchmarks show that this implementation is now faster than
  jsonld.js.)
- Improve type-safety. Previously, type confusion resulted in
  unnecessary checks, such as comparing a known string value against a
  boolean.
- Ensure defaultLanguage always contains a language. Previously,
  a missing if-check could result in defaultLanguage being set to
  "_%s", with %s being the direction.
- Remove GetKeysString since it's not used anywhere and is equivalent to
  the generic GetKeys now.

Signed-off-by: Michael Adler <michael.adler@siemens.com>
@kazarena
Copy link
Member

@michaeladler, the idea behind this PR makes sense. I can see why it improves performance.

The changes are substantial, and I'm still going through it line by line to ensure it does the right things. Apologies for the delay. I should finish the review this week.

@michaeladler
Copy link
Contributor Author

No problem, I completely understand. Thank you for your thorough review. The test suite is excellent though: it caught every mistake I made (such as using bool instead of *bool for ternary semantics) and really helped me stay on track. A great exercise in refactoring :)

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.

2 participants