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

Prefer signals over memos with first unused argument when possible #76

Merged
merged 2 commits into from
Feb 6, 2024

Conversation

mondeja
Copy link
Contributor

@mondeja mondeja commented Feb 5, 2024

@mondeja mondeja changed the title Use signals instead of memos with first unused argument when possible Prefer signals over memos with first unused argument when possible Feb 5, 2024
@mondeja mondeja force-pushed the signals-over-memos branch from 477f624 to 16e67c2 Compare February 5, 2024 22:43
@maccesch
Copy link
Contributor

maccesch commented Feb 6, 2024

Hey thanks for this. I don't understand the motivation behind this change though. What's the reasoning here?

@mondeja
Copy link
Contributor Author

mondeja commented Feb 6, 2024

See the documentation about Memos in Leptos: https://docs.rs/leptos_reactive/0.6.5/leptos_reactive/signal_prelude/prelude/struct.Memo.html

Memos have a certain overhead compared to derived signals. In most cases, you should create a derived signal. But if the derivation calculation is expensive, you should create a memo.

@maccesch
Copy link
Contributor

maccesch commented Feb 6, 2024

😄 I know about this documentation. But if you read carefully you see that there are cases where memos are preferred.

I think you're definitely correct about changing to derived signals in the sorting functions I'm not so sure about the other ones. While thinking about it I'm actually considering changing other "from" conversions to memos as well because usually the stuff that depends on these reactive elements is rather expensive. Although I'm not sure how that compares to the memo overhead.

What do you think?

Copy link
Contributor Author

@mondeja mondeja left a comment

Choose a reason for hiding this comment

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

Thanks. I've reverted three cases, let me know what do you think.

@@ -208,8 +208,7 @@ macro_rules! impl_from_signal_string {
Self::Dynamic(Signal::derive(|| None))
} else {
Self::Dynamic(
create_memo(move |_| document().query_selector(&signal.get()).unwrap_or_default())
.into(),
Signal::derive(move || document().query_selector(&signal.get()).unwrap_or_default()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps here only Memo<...>s conversions should use create_memo() to maintain the same behaviour in conversions? I was thinking that as signal.get() is used here inside the memo/signal, it would react as a memo only if the underlying signal is a memo.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand what you mean, though in Memo conversions it would be unnecessary really, because it's already memoized. In that case it would actually only create overhead.

But in the other cases... I'm not sure. We could make the argument that the code that uses these (which is a lot of functions in leptos-use) should create their own memo if they think they need it.
I'm torn...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could make the argument that the code that uses these (which is a lot of functions in leptos-use) should create their own memo if they think they need it.

Note that the current behaviour could create a lot of headaches when trying to debug why a signal is not being retriggered.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right! Sorry about flipflopping here, but I think you were right with your first PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem 👍🏼 Reverted to the original state.

src/core/elements_maybe_signal.rs Show resolved Hide resolved
@@ -86,7 +86,7 @@ where
let index = {
let list = list.clone();

create_memo(move |_| {
Signal::derive(move || {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to me that this should be a derived signal because is supposed to always throw a new value.

@mondeja mondeja force-pushed the signals-over-memos branch from 9ea0170 to d366909 Compare February 6, 2024 18:48
@maccesch maccesch merged commit a8cef0d into Synphonyte:main Feb 6, 2024
2 checks passed
@mondeja mondeja deleted the signals-over-memos branch February 6, 2024 18:52
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