typelevel / typelevel/cats-effect

Consolidate `IOFiber`'s `objectState` and `conts` stacks on Native

Open
#3,661 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

:mushroom: enhancement
Dominant language
Scala
Stars
2.2k
Forks
576
Avg merge
2d 11h
Merged PRs (30d)
18

Description

This should enable a few wins:

  1. Reduce IOFiber's memory footprint. We would now only need a pointer to one stack, not two.

  2. Elide allocations and a pointer chase. Currently we have a pointer to ArrayStack, which itself has a pointer to an Array. Now we would store the stack size as the first element of the array and thus remove the intermediate allocation.

  3. (Pointers to) objects and conts would be interleaved in a contiguous array e.g. in the following code, the popped cont and the popped pointer to the lambda would be consecutive in memory. This is a much more favorable access pattern.

https://github.com/typelevel/cats-effect/blob/6ae23b7dbda34f6761b734d6020b8a7d9dc04650/core/shared/src/main/scala/cats/effect/IOFiber.scala#L1164-L1166

One possible caveat is that the memory footprint of the stack itself will increase since the cont bytes can no longer be packed when interspersed with object pointers due to alignment requirements.

Making this change will require support from Scala Native to allocate memory that can be accessed unsafely as a Ptr[Byte] but is conservatively traced by the GC. I will shortly open an issue upstream about that and link it here (update: https://github.com/scala-native/scala-native/issues/3312).

Another issue is whether we can implement this change with the usual Platform techniques, or if it would require us to duplicate IOFiber for Native 😕

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced IOFiber.scala code around lines 1164-1166 and review the linked Scala Native issue about conservatively traced unsafe memory. Determine whether the two Native stacks can be consolidated without duplicating IOFiber or violating the alignment and tracing constraints. Done means the Native implementation uses one interleaved stack with the stated memory and allocation benefits.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.