abrown opened PR #4086 from refactor-static-memory to main:
While working with the runtime
Memoryobject, it became clear that
some refactoring was needed. In order to implement shared memory from
the threads proposal, we must be able to atomically change the memory
size. Previously, the split into variants,Memory::Staticand
Memory::Dynamic, made any attempt to lock forced us to duplicate logic
in various places.This change moves
enum Memory { Static..., Dynamic... }to simply
struct Memory(Box<dyn RuntimeLinearMemory>). A new type,
ExternalMemory, takes the place ofMemory::Staticand also
implements theRuntimeLinearMemorytrait, allowingMemoryto contain
the same two options as before:MmapMemoryforMemory::Dynamicand
ExternalMemoryforMemory::Static. To interface with the
PoolingAllocator, this change also required the ability to downcast to
the internal representation.<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
abrown requested alexcrichton for a review on PR #4086.
abrown updated PR #4086 from refactor-static-memory to main.
abrown edited PR #4086 from refactor-static-memory to main:
While working with the runtime
Memoryobject, it became clear that
some refactoring was needed. In order to implement shared memory from
the threads proposal, we must be able to atomically change the memory
size. Previously, the split into variants,Memory::Staticand
Memory::Dynamic, made any attempt to lock duplicate logic
in various places.This change moves
enum Memory { Static..., Dynamic... }to simply
struct Memory(Box<dyn RuntimeLinearMemory>). A new type,
ExternalMemory, takes the place ofMemory::Staticand also
implements theRuntimeLinearMemorytrait, allowingMemoryto contain
the same two options as before:MmapMemoryforMemory::Dynamicand
ExternalMemoryforMemory::Static. To interface with the
PoolingAllocator, this change also required the ability to downcast to
the internal representation.<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton submitted PR review.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
I think this can be an assert since this should have already been checked in
Memory::grow
alexcrichton created PR review comment:
I think this may actually be historical at this point, while you're here could you try removing this (and dependants) and see if it's actually still needed?
alexcrichton created PR review comment:
I think with my above suggestion this could ideally be a private-to-this-module structure
alexcrichton created PR review comment:
(I think this was also present in the original code where this check wasn't necessary)
alexcrichton created PR review comment:
Putting the
downcasthere and dealing withExternalMemoryI think would be better inside ofmemory.rsbecause otherwise it's created viaMemory::new_staticbut then open-coded here that we're reaching into the internals.Perhaps something like
Memory::unwrap_static_imagewhich returnsOption<MemoryImage>which is used here to callclear_and_remain_readyand such?
alexcrichton created PR review comment:
If possible, like above, I think it'd be best for this to not be
pub(crate)but that may not be possible (if it's needed it's needed, can fix later)
alexcrichton created PR review comment:
Could this get folded into
ExternalMemory::new?
alexcrichton created PR review comment:
This happens below as well so I think this one can be removed
alexcrichton created PR review comment:
These last two statements are pretty good now to be a
matchperhaps? (since handling both cases is trivial)
abrown submitted PR review.
abrown created PR review comment:
I tried...
mem::takeinpooling.rsrequires aMemoryto beDefault. Is there another way to do something equivalent there?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Ah that can be replaced with
mem::takeof thememorieslist itself rather than each individual memory I thin
alexcrichton edited PR review comment.
abrown submitted PR review.
abrown created PR review comment:
I see other checks in
Memory::growbut none that are equivalent to this one. Do you mean that we should move this check back to theMemory::growfunction and duplicate it here and inMmapMemoryas adebug_assert!?
alexcrichton created PR review comment:
I think it's covered by this check since this variant always has a
maximum_byte_sizeofSome, but yeah here I think this check can be anassert!or adebug_assert!
alexcrichton submitted PR review.
abrown submitted PR review.
abrown created PR review comment:
Ok, I did it. I was hoping I could get rid of
RuntimeLinearMemory::into_anybut I don't see how: I still need to be able to convertBox<dyn RuntimeLinearMemory>toBox<dyn Any>in the newMemory::unwrap_image_slot. Let me know if there's something preferable... I do see a lot more#[cfg(feature = "pooling-allocator")]here.I also felt like I had to add back in a way to assert that only external memory is used in the pooling allocator, as was done previously; see the new
Memory::is_external.
abrown updated PR #4086 from refactor-static-memory to main.
abrown submitted PR review.
abrown created PR review comment:
Can we?
memoryis coming fromself.memories.get(instance_index)so I don't think I can just take from the iterator? Maybe I'm just not seeing it...
abrown updated PR #4086 from refactor-static-memory to main.
abrown submitted PR review.
abrown submitted PR review.
abrown created PR review comment:
I might be doing this wrong... I'm trying to get to a
&dyn Anyso I can check the downcast is of the right kind but I'm concerned that I have the wrong type. I see a failure building the benchmarks that I don't think should happen. Rust help needed!
alexcrichton submitted PR review.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Yeah I don't think this is correct, you'll need to change the trait's
into_anymethod into something likeas_anyand use that to get the&Anyto test
alexcrichton created PR review comment:
Yes with
memory: &mut PrimaryMap<K, V>you canmem::take. Only thebasecomes fromself.memories.get(instance_index)
alexcrichton submitted PR review.
alexcrichton created PR review comment:
I'd personally prefer to make "external" memory an internal implementation detail of the
memory.rsmodule if we can. Right now it has aMemory::new_staticconstructor so in theory we would have something likeMemory::unwrap_static_imagewhere we keep the "static" name and theunwrapbit implies the assertion and the method takes out the input argument toMemory::new_static
abrown updated PR #4086 from refactor-static-memory to main.
abrown submitted PR review.
abrown created PR review comment:
Ok, had to add
RuntimeLinearMemory::as_any(even more pooling-allocator-specific trait functions!) but that seems to be correct. I can't useinto_anybecause that takes aBox<Self>.
abrown submitted PR review.
abrown created PR review comment:
Oh, you're right! Sorry, wasn't looking closely enough.
abrown updated PR #4086 from refactor-static-memory to main.
abrown updated PR #4086 from refactor-static-memory to main.
abrown submitted PR review.
abrown created PR review comment:
Ok, I changed all of the
pubfunctions to use the same naming:is_staticandunwrap_static_imagenow matchnew_static. I left the struct asExternalMemory(instead ofStaticMemory, though I can change it to that if so desired) because I started to feel that this static/dynamic nomenclature here isn't actually the most accurate. Sure, the oldMemory::Statichad a static starting address, but it turns outMemory::Dynamiccould as well (e.g.,MemoryStyle::Static)! Static/dynamic are rather overloaded terms anyways, so I started thinking of this structure in terms of external/internal. External memory is managed "outside the engine" by, e.g., the pooling allocator, and internal memory is managed directly by the engine; this mental switch helped me understand more clearly why we need more than one option here and perhaps it would be the same for others. What do you think? If you agree, I can take up this renaming in a separate PR.
alexcrichton submitted PR review.
abrown updated PR #4086 from refactor-static-memory to main.
abrown updated PR #4086 from refactor-static-memory to main.
abrown merged PR #4086.
Last updated: Dec 13 2025 at 19:03 UTC