Skip to content

Add a memoizedAcquire method to Resource#3890

Closed
Daenyth wants to merge 3 commits into
typelevel:series/3.xfrom
Daenyth:gb-lazy-resource
Closed

Add a memoizedAcquire method to Resource#3890
Daenyth wants to merge 3 commits into
typelevel:series/3.xfrom
Daenyth:gb-lazy-resource

Conversation

@Daenyth

@Daenyth Daenyth commented Nov 8, 2023

Copy link
Copy Markdown
Contributor

Ref #3513

I'm not sure how to unit test this exactly, I'm open to guidance or commits for that

Comment on lines +824 to +829
Concurrent[Resource[F, *]].memoize[A](self).map {
case Resource.Eval(fa) => fa
case unexpected =>
throw new IllegalStateException(
s"Memoized Resource is not Resource.Eval: $unexpected")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, we can avoid this silliness if we go the other way: put the juicy implementation in here, and then memoize can delegate to memoizedAcquire and wrap it up in Resource.eval.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also then, the existing memoize tests seem like sufficient coverage to me.

@armanbilge armanbilge linked an issue Nov 8, 2023 that may be closed by this pull request
@armanbilge armanbilge changed the title Add a memoizedAcquire method to Resource Add a memoizedAcquire method to Resource Nov 8, 2023
@SystemFw

Copy link
Copy Markdown
Contributor

To test this I would write to Refs in your acquire and use, and assert on the state of those refs to check that it's behaving as you expect. I wouldnt worry about concurrent testing since memoize is pulling most of the weight

@armanbilge

armanbilge commented Nov 14, 2023

Copy link
Copy Markdown
Member

I don't think we need to add any new tests specifically because of this PR, unless there are existing gaps in the existing memoize tests.

To be clear, what I'm proposing is that we refactor so it looks like this:

def memoizedAcquire(ra: Resource[F, A]): Resource[F, F[A]] =
  /* move existing memoize implementation here */

def memoize(ra: Resource[F, A]): Resource[F, Resource[F, A]] = 
  memoizedAcquire.map(Resource.eval(_))

Then all the existing memoize tests can just be in terms of memoizedAcquire.

@SystemFw

Copy link
Copy Markdown
Contributor

Yeah fair enough, with that refactor I would agree that we don't need any new tests

@Daenyth

Daenyth commented Nov 14, 2023

Copy link
Copy Markdown
Contributor Author

I'm a huge fan of not having to add new tests. I will get to that refactor when/as I have time but if anyone wants to beat me to it, the branch is open for maintainer commits ;)

val fa2 = F.uncancelable { poll =>
poll(allocatedCase).flatMap { case (a, r) => release.update(r :: _).as(a) }
}
Resource.makeCaseFull[F, F[B]](poll => poll(F.memoize(fa2).map(_.widen))) { (_, exit) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
Resource.makeCaseFull[F, F[B]](poll => poll(F.memoize(fa2).map(_.widen))) { (_, exit) =>
Resource.makeCaseFull[F, F[B]](poll => poll(F.memoize(fa2)).map(_.widen)) { (_, exit) =>

Otherwise I think it can get cancelled right after F.memoize(fa2), and I think that would be not good.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh I see, good catch. Probably we should add a test case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I will look into adding a test case for this, let's see what I can come up with.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tried to write a test for this but failed to find a reliable way to ensure acquisition is canceled right after the call to F.memoize(fa2) and before the end of .map(_,widen). Is there some tool in the testkit that would allow me to do so and that I am not aware of?

@Daenyth

Daenyth commented Jul 1, 2024

Copy link
Copy Markdown
Contributor Author

I don't have the time to update this branch with the changes but I encourage anyone motivated to take over

@satabin

satabin commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

Hi @Daenyth, I would be interested in taking this over if that's ok. I'd love to see this feature land in the 3.6 release :-)

@djspiewak

Copy link
Copy Markdown
Member

@satabin Go for it! I think you'll need to pull this branch locally and open a new pull request so you can push to it. Please be sure to refrain from rebasing the commits; we can carry the conversation more easily from this PR to that one if the shas are not rewritten.

@satabin

satabin commented Jul 18, 2024

Copy link
Copy Markdown
Contributor

I opened #4105 to continue the work on this PR.

@djspiewak djspiewak closed this Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add specialized signature of memoize to Resource

6 participants