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

Implement a native C++ macro API + use it to replace manual macro locking #3408

Merged
merged 3 commits into from
Nov 4, 2024

Conversation

pmatilai
Copy link
Member

@pmatilai pmatilai commented Oct 29, 2024

This started with the innocent intention of making a couple of C++ string expand variants available, but then I got carried away...

There are no added tests because this is all covered by existing functionality testing - the last commit effectively adds a test for the expand variant accepting a vector.

This is all a such a hysterical mess but one of the preliminaries to
be able to return C++ strings to callers is to have them in the first
place. This doesn't make any real difference as such, just makes
the result one step closer.
@pmatilai pmatilai requested a review from a team as a code owner October 29, 2024 09:37
@pmatilai pmatilai requested review from ffesti and removed request for a team October 29, 2024 09:37
@pmatilai
Copy link
Member Author

pmatilai commented Oct 29, 2024

Got carried away enough that I forgot to explain the key benefits:
This gives the caller the ability to control locking, so if you need to do 1000 expands in a loop you no longer need to go through lock+unlock cycles for each as it is with the C versions. Gaining those benefits of course requires changing callers to the new API, I've only converted a couple of calls here for examples and to provide tests for the string/vector variants.

And of course, you don't need to manually free those C++ strings these expands return, which is a death by thousand cuts with rpmExpand() and friends.

These need to go to the main commit message of course, but I'll wait to see if there are other adjustments to make.

@ffesti
Copy link
Contributor

ffesti commented Oct 30, 2024

The macro class needs a bit more doc strings. It is kinda weird that it is basically just a lock around the macro context. Not that there is anything wrong with that - it's just weird

@pmatilai
Copy link
Member Author

The macro class needs a bit more doc strings.

I don't disagree, but we don't traditionally have much in the way of docs for internal APIs. What do you want to see documented, the locking scheme?

It is kinda weird that it is basically just a lock around the macro context. Not that there is anything wrong with that - it's just weird

This is basically how you want a locked resource to be handled in C++. It's a different paradigm from what we've had, sure. The various fooAcquire() things were a kind of emulation of this though.

@ffesti
Copy link
Contributor

ffesti commented Oct 30, 2024

May be just add a two line comments before or just in the class declaration stating that this si for getting a lock on a macro context and use it to do macro stuff with it.

Force all macro access through a C++ class which locks the context
in constructor, convert all access to go through it and voila. This
lets caller do multiple macro operations on one lock/unlock cycle,
whereas the C API is forced to do one on each and every call.

Generally the APIs with their C strings have been left alone, but
the expand variants always return an actual result code, and take and
return C++ strings to free callers, which is the other big benefit here.

This looks big but is actually rather straightforward. New tests are not
really needed because the C API internally now uses this API, and that
gets thoroughly exercised.
@pmatilai
Copy link
Member Author

Added a bunch of notes on the overall design + notes on API differences where they exist.
Also added native expand_numeric() variants which I had somehow missed in the first round.

@ffesti
Copy link
Contributor

ffesti commented Oct 31, 2024

This looks do to me now. May be @dmnks can have a look and second opinion before merging.

@ffesti ffesti requested a review from dmnks October 31, 2024 15:50
@dmnks
Copy link
Contributor

dmnks commented Nov 4, 2024

The first commit seems to have forgotten to update the doExpandMacros() call in rpmExpandMacros() where it still passes a C string (despite the function expecting a C++ string reference instead). The call is later refactored in a followup commit in this PR but maybe it should still be fixed for bisectability.

Copy link
Contributor

@dmnks dmnks left a comment

Choose a reason for hiding this comment

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

Other than the note above, looks good to me.

@pmatilai
Copy link
Member Author

pmatilai commented Nov 4, 2024

Oh, thanks for spotting, I'll fix it up.

@dmnks
Copy link
Contributor

dmnks commented Nov 4, 2024

The call is later refactored in a followup commit

Ehm, I take that back, it's not fixed in the latest commit either, it still passes on a C string 😅

@pmatilai
Copy link
Member Author

pmatilai commented Nov 4, 2024

Right, I initially thought it was something else.

This isn't a problem at all, C++ will merrily construct temporary string objects in a case like this. The other direction wont work though 😅

@dmnks
Copy link
Contributor

dmnks commented Nov 4, 2024

Ack, I wondered if that was the case, thanks 😄

@dmnks dmnks merged commit ac25837 into rpm-software-management:master Nov 4, 2024
1 check passed
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