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

chore: Use new unique_ptr based SetEventHeader #1423

Merged

Conversation

ChristianTackeGSI
Copy link
Member

@ChristianTackeGSI ChristianTackeGSI commented Jul 7, 2023

Also update examples to current standards and compile check
them.

Also update codemeta.json.


Checklist:

Run meta_update.py
@ChristianTackeGSI ChristianTackeGSI marked this pull request as ready for review July 7, 2023 09:46
@ChristianTackeGSI
Copy link
Member Author

@YanzhaoW does that look good to you?

Copy link

@YanzhaoW YanzhaoW left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR. There is no problem concerning the event header. It's ok to be merged.

However I would like to urge that we still need to specify the ownership transferring for other objects, like FileSource, FileSink, FairTask. Maybe we could have some PRs to address this issue?

fRun->SetSink(new FairRootFileSink(outFile));
FairRunAna run{};
run.SetEventHeader(std::make_unique<PixelEventHeader>());
run.SetSink(new FairRootFileSink(outFile));
Copy link

Choose a reason for hiding this comment

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

I think we should indicate the ownership transferring in our example code:

Suggested change
run.SetSink(new FairRootFileSink(outFile));
auto outFile = std::make_unique<FairRootFileSink>();
run.SetSink(outFile.release());

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have a SetSink(unique_ptr). So you're right, I should use that!

We don't have that for SetSource yet, because SetSource is a virtual thing which we need to understand first.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have updated the commit to use unique_ptr for FairSink. But not yet for FairSource.

Let's please postpone the SetSource thing until we understand that mess a bit better.

Copy link

Choose a reason for hiding this comment

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

But fSource in the FairRun is already a unqiue_ptr:

std::unique_ptr<FairSource> fSource{}; //!

Why can we not have a setter taking a unique_ptr if the FairSource is an abstract class?

Copy link
Member Author

Choose a reason for hiding this comment

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

Take a look at FairRunAnaProof::SetSource which overrides SetSource. I haven't even started to think on how to fix this.
And TBH I just hope for it to go away sooner than later, because we deprecated Proof in FairRoot. Then this problem is just gone.

Copy link

Choose a reason for hiding this comment

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

Oh, sorry, I understood incorrectly. I didn't see SetSource is a virtual function.

This is bad. Setters and getters should not be a virtual function.

Copy link
Member Author

Choose a reason for hiding this comment

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

The original SetSink is virtual too, but luckily noone (in FairRoot) overrides it. We probably should remove the virtual. But as we want to deprecate it anyway…

Copy link
Collaborator

@karabowi karabowi Jul 10, 2023

Choose a reason for hiding this comment

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

I think the FairRun::SetSource has been made virtual only to implement FairRunAnaProof::SetSource.
I vote for removing virtual. I can make a corresponding PR.


PixelFindHits* hitFinderTask = new PixelFindHits();
fRun->AddTask(hitFinderTask);
run.AddTask(hitFinderTask);
Copy link

Choose a reason for hiding this comment

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

Same here

Suggested change
run.AddTask(hitFinderTask);
auto task = std::make_unique<PixelFindHits>();
run.AddTask(task.release());

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added a new commit which adds a unique_ptr based FairRun::AddTask(). It's only a new API "for the public", not yet fixing any internals (because that looks like a bigger thing).

And it only uses the new API in the examples that I touch in this PR anyway…

Copy link

@YanzhaoW YanzhaoW Jul 7, 2023

Choose a reason for hiding this comment

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

Maybe add another thing that came to my mind:

Instead of using make_unique (and move) every time, there is a technique, called type erasure, that can be particularly useful in this case.

Instead of

run.AddTask(std::make_unique<PixelFindHits>());

With type erasure, it's much clearer:

run.AddTask(PixelFindHits());

Copy link
Member

@dennisklein dennisklein Jul 7, 2023

Choose a reason for hiding this comment

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

Can you elaborate on this? I cannot follow tbh.

  • Are you proposing AddTask(void*)?
  • How is AddTask(PixelFindHits()) keeping the temporary object alive? (And what is the function signature?)

Copy link

@YanzhaoW YanzhaoW Jul 7, 2023

Choose a reason for hiding this comment

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

The signature of AddTask could be:

void AddTask(Task task);

and the Task class could be defined as:

class Task{

public:
   template<typename T>
   Task(T&& obj) {}

  class Concept{ ... };

   template <typename T>
   class Model : Concept{ ...};

private:
   std::unique_ptr<Concept> obj;

};

Yeah, it's complicated for developers to implement it. But it's super easy for users to use it. For more information you can check this website or this video

Copy link
Member

@dennisklein dennisklein Jul 7, 2023

Choose a reason for hiding this comment

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

The top two issues in FairRoot/ROOT(/Geant4) - from my point of view - are

  • unclear/messy object ownership, and
  • tons of tight coupling and global state (which renders testability very hard, unit testing is non-existent (in FairRoot)).

Virtual functions are really not a big issue compared to the above, but I am happy to learn and am open to arguments and evidence to the contrary :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I have tried to understand some of this (watched first half or of the video–quite good one!).

For example I don't know, what external polymorphism really buys us? And we're currently somewhat forced by ROOT to use classic (runtime) polymorphism anyway.

As Dennis already pointed out: Our current problem is the unclear ownership. Which is a much bigger problem then using sophisticated tools ("Yeah, it's complicated for developers to implement it.") that some of our developers are not accustomed at using. And for fixing the ownership stuff, it currently feels better to be quite explicit about it.

And to be honest: I like the verbose make_unique. It's more transparent, more explicit. (So I actually do understand, why you don't like "my" "facade like" helpers. And this time I don't like the hiding of things.)

Copy link

Choose a reason for hiding this comment

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

Sorry, I didn't mean we need adopt this idiom as soon as possible, but rather just want to give an information that type erasure could be used in this situation.

@ChristianTackeGSI
Actually the external polymorphism is just for making unrelated objects behave like objects with polymorphism. And type erasure idiom wraps a class around it and add more operations to make the object of that class like a normal value (copiable and movable). The biggest advantage of the type erasure (or some calls it value semantics) could exactly solve our first problem: unclear ownership. Users only need to think about the ownership when a pointer (or unique_ptr) is involved. But if they just use values, like a "type erasure" value, then the whole concept of ownership is non-existent and they don't even need to think about that. Of course, whenever it's easier for users, it's always harder for developers.

Yeah, like I said, I don't mean we should start working on this now without solving other major issues first. ;D

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool!

So let's first work on getting the ownership checked around and fix this stuff. Then later on we can take a look at this again and improve on the then existing structure?
(And maybe a unique_ptr is type erased enough? 😉)

So that said:

Can we resolve this thread? Anything else I should fix for this PR?

Copy link

Choose a reason for hiding this comment

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

@ChristianTackeGSI Yeah, I think so.

Also update examples to current standards and compile check
them.
So that the callers can be more explicit about ownership,
add an alternative FairRun::AddTask that takes a
unique_ptr.

Do not yet deprecate the old API, because it's used in way
too many places. But we should update all our code to use
the new API so that the new style ist adopted.
@ChristianTackeGSI ChristianTackeGSI merged commit 0701da3 into FairRootGroup:dev Jul 11, 2023
@ChristianTackeGSI ChristianTackeGSI deleted the pr/up_setevthead branch July 11, 2023 11:08
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.

4 participants