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

Add MemCx<'mcx> for List<'mcx, T> #1342

Conversation

workingjubilee
Copy link
Member

@workingjubilee workingjubilee commented Oct 18, 2023

List requires reallocation in order to push to it with safe Rust, but it is defined as allocated via palloc, which has problems with lifetimes. In order to head off these problems, and begin work on related ones, introduce a type describing a MemoryContext with a lifetime, including scoped (therefore lifetime-bound) access to the current context.

Copy link
Member Author

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

UX thoughts: I probably didn't write the interfaces correctly. Might need redesign.

Wondered about maybe instead using something like List<T, M>, where M might be the thing infected with the lifetime? idk.

Comment on lines 18 to 24
mem::current_context(|cx| {
crate::list::List::<*mut core::ffi::c_void>::downcast_ptr_in_memcx(range_table, cx)
.expect("rt_fetch used on non-ptr List")
.get((index - 1) as _)
.expect("rt_fetch used out-of-bounds")
.cast()
})
Copy link
Member Author

Choose a reason for hiding this comment

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

This diff suggests to me my approach is kinda incorrect, since I don't really "use" the context.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is probably going to be the most annoying part here. Maybe this would be better captured as another parameter to List, so non-allocating but owning uses don't have to worry about this.

pgrx/src/mem.rs Outdated
Comment on lines 31 to 39
/// Acquire the current context and operate inside it.
pub fn current_context<'curr, F, T>(f: F) -> T
where
F: for<'clos> FnOnce(&'clos MemCx<'curr>) -> T,
{
let memcx = unsafe { MemCx::from_ptr(pg_sys::CurrentMemoryContext) };
let ret = { f(&memcx) };
ret
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Definitely not sure about soundness, but something similar to this is probably required, for ergonomics reasons.

iter: RawCellIter<T>,
}

/// A list being drained.
#[derive(Debug)]
pub struct Drain<'a, T> {
pub struct Drain<'a, 'cx, T> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Really need to move Drain itself "up".

Also: two lifetimes! spooky.

@eeeebbbbrrrr
Copy link
Contributor

Just a note to say I've skimmed over this. I don't have any feedback yet other than it's exciting to see some code demonstrating the ideas.

pgrx-tests/src/tests/list_tests.rs Outdated Show resolved Hide resolved
Comment on lines 18 to 24
mem::current_context(|cx| {
crate::list::List::<*mut core::ffi::c_void>::downcast_ptr_in_memcx(range_table, cx)
.expect("rt_fetch used on non-ptr List")
.get((index - 1) as _)
.expect("rt_fetch used out-of-bounds")
.cast()
})
Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is probably going to be the most annoying part here. Maybe this would be better captured as another parameter to List, so non-allocating but owning uses don't have to worry about this.

pgrx/src/fn_call.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@thomcc thomcc left a comment

Choose a reason for hiding this comment

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

This is a good improvement.

@workingjubilee
Copy link
Member Author

Rebased and added some (~300 words) of documentation on memory contexts. I think I will land this now, so that it's easier to build off of and mutate as-we-go.

@workingjubilee workingjubilee changed the title Experimental use of MemCx + List + FnCall Use MemCx<'mcx> with List<'mcx, T> Dec 7, 2023
@workingjubilee workingjubilee changed the title Use MemCx<'mcx> with List<'mcx, T> Add MemCx<'mcx> for List<'mcx, T> Dec 7, 2023
@workingjubilee workingjubilee merged commit bc611d4 into pgcentralfoundation:develop Dec 7, 2023
8 checks passed
@workingjubilee workingjubilee deleted the experimental-memcx-list branch December 7, 2023 01:27
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.

3 participants