Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Memory Optimizations for ErrorOr<TValue> #99

Merged

Conversation

feO2x
Copy link
Contributor

@feO2x feO2x commented Apr 27, 2024

This PR fixes #98 and introduces a new class called 'KnownErrors' which contains the NoFirstError and NoErrors instances that previously resided in ErrorOr<TValue>. With this change, they will not reside in memory several times during runtime (once for each type TValue is resolved to). Furthermore, I cached two lists so that they won't be instantiated every time the Errors and ErrorsOrEmptyList property is called.

This commit introduces a new class called 'KnownErrors' which
contains the NoFirstError and NoErrors instances that
previously resided in ErrorOr<TValue>. With this change, they
will not reside in memory several times during runtime (once
for each type TValue is resolved to). Furthermore, I cached
two lists so that they won't be instantiated every time the
Errors and ErrorsOrEmptyList property is called.

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
@MGREMY
Copy link

MGREMY commented Apr 29, 2024

Why not having KnownErrors as a struct instead of a class ?

@feO2x
Copy link
Contributor Author

feO2x commented Apr 29, 2024

Because a struct cannot be declared with the static modifier. When a struct is defined, you can always instantiate it with its default constructor. I don't want any callers to instantiate KnownErrors. I just want to cache instances and make them available, which is why I went for a static class.

@MGREMY
Copy link

MGREMY commented Apr 29, 2024

Because a struct cannot be declared with the static modifier. When a struct is defined, you can always instantiate it with its default constructor. I don't want any callers to instantiate KnownErrors. I just want to cache instances and make them available, which is why I went for a static class.

Oh okay nice one, and can't have a singleton tho for the same reason :)

@amantinband amantinband merged commit 9a1310b into amantinband:main May 9, 2024
1 check passed
zbyszekprasak added a commit to zbyszekprasak/error-or that referenced this pull request Dec 3, 2024
zbyszekprasak added a commit to zbyszekprasak/error-or that referenced this pull request Dec 4, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] Avoid duplicated runtime memory by moving NoFirstError and NoErrors into non-generic type
3 participants