Skip to content

Rewrite string info#903

Merged
eeeebbbbrrrr merged 3 commits into
developfrom
rewrite-string-info
Dec 10, 2022
Merged

Rewrite string info#903
eeeebbbbrrrr merged 3 commits into
developfrom
rewrite-string-info

Conversation

@eeeebbbbrrrr

@eeeebbbbrrrr eeeebbbbrrrr commented Nov 30, 2022

Copy link
Copy Markdown
Contributor

Rejigger our StringInfo wrapper to use a PgBox and honor the various "WhoAllocated" traits.

I think it's pretty important for the compiler to have an understanding of who is going to drop a StringInfo, as it's used in the "inoutfuncs" portions of PostgresType, and that's going to get an overhaul soon.

This wants #901 to be merged first.

@eeeebbbbrrrr eeeebbbbrrrr requested a review from thomcc November 30, 2022 19:36
@eeeebbbbrrrr eeeebbbbrrrr changed the base branch from develop to whoallocated-no-T November 30, 2022 19:38
@eeeebbbbrrrr eeeebbbbrrrr changed the base branch from whoallocated-no-T to develop December 5, 2022 17:48
…s "WhoAllocated" traits.

Also adds SAFETY docs.

I think it's pretty important for the compiler to have an understanding of who is going to drop a StringInfo, as it's used in the "inoutfuncs" portions of PostgresType, and that's going to get an overhaul soon.
@eeeebbbbrrrr

Copy link
Copy Markdown
Contributor Author

I've rebased this PR. @thomcc, could I get a review? I think this is good cleanup for StringInfo

Comment thread pgx/src/stringinfo.rs
Comment thread pgx/src/stringinfo.rs Outdated
Comment thread pgx/src/stringinfo.rs
/// Unless `.into_pg()` or `.into_char_ptr()` are called, memory management of
/// this `StringInfo` follow Rust's drop semantics.
pub fn with_capacity(len: usize) -> Self {
pub fn with_capacity(len: i32) -> Self {

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.

Hm, why the change to the i32 type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I argued with myself over this. Postgres' StringInfo length is an i32, so I thought rather than us deal with an usize that overflows an i32 and have to return a Result, just let the user sort it out ahead of time.

Thoughts?

@workingjubilee workingjubilee Dec 7, 2022

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.

We discussed this on Discord and my sentiment is broadly "usize is fine and indicates what the input is 'for' to Rust programmers (morally it's actually pg_size_t), and Postgres is running so many checks that it will throw a fit if we throw a bad input into Postgres, don't worry about it". with_capacity is really more of an "optimization hint" to Postgres, as Postgres will, before pushing anything on StringInfo, always calculate something that would look like

let needed: c_int = requested_capacity - (sinfo.capacity - sinfo.len);

However, there's an argument that we should either document # Panics or make anything that throws these numbers directly into Postgres an unsafe fn.

Comment thread pgx/src/stringinfo.rs

@workingjubilee workingjubilee left a comment

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.

Despite my stated inclination, I think the type thing can ultimately go either way so I am good with this now.

@eeeebbbbrrrr eeeebbbbrrrr merged commit 67879fa into develop Dec 10, 2022
@eeeebbbbrrrr eeeebbbbrrrr deleted the rewrite-string-info branch June 20, 2023 18:00
usamoi pushed a commit to tensorchord/pgrx that referenced this pull request Mar 6, 2025
Rejigger `StringInfo` wrapper to use a PgBox and honor the various "WhoAllocated" traits.
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