Uploaded image for project: 'Mesos'
  1. Mesos
  2. MESOS-1008

Reduce copying in stout / libprocess primitives {Try, Option, Result, Future}.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • None
    • None
    • None

    Description

      The following will discuss Try, but the same applies to Option and Result.

      Currently retrieving the value from a Try requires a copy:

        T get() const { ... return *t; }
      

      Instead, we can return a const&:

        const T& get() const { ... return *t; }
      

      For existing callers, this should be fairly seamless:

        const T& t = try.get(); // No change needed.
        T t = try.get(); // No change needed, T is already required to be copyable.
        try.get().mutator(); // Will no longer compile, but we should not allow this anyway!
      
        const T& t = try.get();
        try = T(); // t is now garbage!
        t.foo(); // No longer works.
      

      The last case is the most concerning as this mistake cannot be caught at compile-time. We could remove the assignment operators, but this seems overly restrictive. We could also guard (via CHECK) the assignment operator after a get() operation, but of course, we're also preventing valid Try usage if we go this route. The best path may simply be to leave the assignment operators as is (many callers already make copies).

      We can also add C++11 support for move to pilfer the Try, to avoid copies in the caller entirely:

        T&& move() const { ... } // After a move(), we must guard Try operations.
      

      Attachments

        Issue Links

          Activity

            People

              dhamon Dominic Hamon
              bmahler Benjamin Mahler
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: