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

Tthread-local geos context #164

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

vladaburian
Copy link
Contributor

This PR is follow-up of my previous issue #141 and also reaction to #160. (I don't think there is a memory leak, but this PR does lower memory footprint of every geometry by little more than 1kB as that is roughly the size of geos context, which would be reused with my PR and not allocated for every geometry.)

I see geos context as just an implementation detail of propagating error messages from c++ geos exceptions through geos c api in reentrant way. To address questions in #141: I dont't think the context is useful from user's point of view and I would say it is better/simpler to not have it in public api at all. I also think that context should not be a property of geometry for the same reason (and because unnecessarily used memory). And also I don't see it useful to be able to set own error handler as the context is already used to propagate geos errors to GResult. The original c++ geos api has nothing like the context and also python geos wrapper shapely is using geos context only as an internal implementation detail and is not accessible to users.

Another reason for this PR is that geos context is not thread-safe (it's not Sync), so this PR makes things correct. Although right now it seems that the worst case is possibly corrupted error message which is not severe problem really, but still.

So this is my suggestion :)

@vladaburian
Copy link
Contributor Author

To the actual code change: instead of allocating new geos contexts I'm just reusing the same thread-local contexts via with_context function. The context is used only to make c api call and is not passed around or shared in any other way.

@vladaburian vladaburian mentioned this pull request Dec 3, 2024
let mut cy = 0.;

let ret = GEOSSegmentIntersection_r(
ctx.as_raw(),
Copy link
Member

Choose a reason for hiding this comment

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

Is it needed here? Couldn't with_context be called inside GEOSSegmentIntersection_r methods directly?

Copy link
Member

Choose a reason for hiding this comment

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

GEOSSegmentIntersection_r is a GEOS C API, how would that work?

Copy link
Member

Choose a reason for hiding this comment

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

Ah it's a function. My bad, then that answers my question.

@GuillaumeGomez
Copy link
Member

This is super cool! Just one question and then seems ready for me to go.

@GuillaumeGomez
Copy link
Member

Again, great improvement, thanks!

@GuillaumeGomez GuillaumeGomez merged commit 12aa1a0 into georust:master Dec 3, 2024
8 of 15 checks passed
@vladaburian
Copy link
Contributor Author

Thank you :)

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.

3 participants