Narrowing protocols with reflection

Abierto
#98 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Activo
Stack tecnológico
cpp
Área
compilers

Línea de trabajo

Comienza con el modelo de reducción de múltiples traits duck del issue y con los diseños de vtable protocol propuestos. Investiga enfoques alternativos basados en reflexión y técnicas relevantes de implementadores de STL, tal como se solicita en la conclusión. Se considerará terminado cuando se documente un enfoque viable para la reducción de protocolos y el overhead o las concesiones que introduce.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

The draft notes that narrowing should be allowed for protocols:

// Narrowing constructor from any compatible mutable protocol_view.
template <typename Other>
protocol_view(const protocol_view<Other>& other) noexcept;

This is an idea worth exploring with reflection. I would like to provide a brief overview of duck's approach to this problem, and the issues we may encounter when implementing this for protocol.

duck's model

Rather than accepting a single trait (same as protocol's interface), duck accepts a variadic number of traits. A duck<A, B, C> can then be converted to a duck<A>, duck<B>, etc.

// where A, B, C are interfaces
rjk::duck<A, B, C> d{...};
rjk::duck_view<A> dv1 = d;
rjk::duck_view<B> dv2 = d;
rjk::duck_view<const C> dv3 = d;

The approach we take for this looks like:

struct vtable<A, B, C> {
    const vtable<A>* to_trait_0;
    const vtable<B>* to_trait_1;
    const vtable<C>* to_trait_2;

    // actual dispatch functions, lifecycle, etc.
};

// for single-trait vtables
struct vtable<A> {
    const vtable<const A>* to_const;
};

I am currently working on a feature for the library I call "multi-trait" narrowing, or converting from a duck<A, B, C> to a duck<A, B>, duck<A, C>, or any other subset of the traits.

With the approach I'm using currently (and the best one I could think of so far) this means that a vtable<A, B, C> will now need to store a vtable<A, B>*, vtable<A, C>*, vtable<B, C>*, in addition to the single-trait vtable pointers. As you imagine, this could become a problem for a lot of traits. It is not, however, an issue for a large number of interface members, since duck does not allow granular narrowing to each method.

protocol's model

Bringing this info to protocol, if I understood the specification correctly, the goal is to support the following:

struct A {
    int foo();
    int bar();
    int baz();
};

struct B {
    int foo();
    int bar();
};

protocol<A> p{...};
protocol_view<B> pv1 = p;
protocol_view<const B> pv2 = p;

My first thought for implementing this would be to reuse the approach from duck, but treating each interface member as a "trait" instead. So you would make three vtables: one for int foo(), one for int bar(), and one for int baz(). Then each one would need to store a pointer to the vtable for its constituent members. So a vtable for A might contain:

struct vtable<foo, bar, baz> {
    const vtable<foo>* to_func_0;
    const vtable<bar>* to_func_1;
    const vtable<baz>* to_func_2;
    const vtable<foo, bar>* to_func_0_1;
    const vtable<foo, baz>* to_func_0_2;
    const vtable<bar, baz>* to_func_1_2;

    // actual dispatch functions, lifecycle, etc.
};

If we are anticipating interfaces with a large number of methods, this could slow down compile time significantly. Defining one interface with many members, even if the user does not intend to narrow it at all, would cause significant overhead.

Conclusion

I am not creating this to discourage narrowing from being included in protocol, since I think it is a high-value feature. Some additional research into alternate methods using reflection would be helpful, so as to potentially avoid the overhead described above. Additionally, it would be valuable to discuss this with STL implementers to see if they have access to some methods that we don't.

Lenguaje dominante
C++
Estrellas
12
Forks
4
Merge medio
1 d 11 h
PR fusionados (30 d)
15

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de jbcoe/cc-protocol

Todos los issues de jbcoe/cc-protocol

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.