You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucy.apache.org by rectang <gi...@git.apache.org> on 2015/05/21 02:13:14 UTC

[lucy-dev] [GitHub] lucy-clownfish pull request: CLOWNFISH-47 Fix final methods

GitHub user rectang opened a pull request:

    https://github.com/apache/lucy-clownfish/pull/20

    CLOWNFISH-47 Fix final methods

    Final methods should be implemented using static inline functions rather than macros, improving type safety. Final methods are also being optimized too eagerly, though limited use of final classes and methods in earlier releases means that this compilation bug has not manifested in live code to my knowledge.
    
    This branch also re-enables the Go build in Travis.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/rectang/lucy-clownfish CLOWNFISH-47-fix-final-methods

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/lucy-clownfish/pull/20.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #20
    
----
commit 5bb75c9dd63aab1d772bd2b7c68d2e25253fc42c
Author: Marvin Humphrey <ma...@rectangular.com>
Date:   2015-05-20T22:35:14Z

    Stop forcing invocant to "self" in codegen.
    
    Use the original invocant name in generated method invocation function
    and method typedef.

commit f1e69a0fcbf62008bfd2ce8e0235243d6b138e58
Author: Marvin Humphrey <ma...@rectangular.com>
Date:   2015-05-20T23:23:37Z

    Use static inline funcs for final methods.
    
    Instead of a macro which aliases to a specific implementing function,
    wrap a call to the implementing function in a static inline function.
    This encapsulates casting of the invocant, improving type safety.  It is
    also more compatible with CGO, which did not understand the macros.

commit 596d282770eb92281190ff7a9158a478ef8cf55a
Author: Marvin Humphrey <ma...@rectangular.com>
Date:   2015-05-20T23:40:56Z

    Don't over-optimize final methods.
    
    It is only valid to resolve a final method to a specific implementing
    function when that function resides in the same parcel as the invocant.
    A ancestor class in another parcel has the option of adding or removing
    an implementation, so the descendant class cannot know for sure what
    function it should resolve to.

commit 0daeb0d7893eec2b7763a53289a2c876ac65a427
Author: Marvin Humphrey <ma...@rectangular.com>
Date:   2015-05-21T00:01:15Z

    Fix invalid NULL argument.

commit 54373b61aad37c16ffdd124f244ad03e6a8a01cb
Author: Marvin Humphrey <ma...@rectangular.com>
Date:   2015-05-21T00:02:43Z

    Re-enable Go Travis build.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[lucy-dev] [GitHub] lucy-clownfish pull request: CLOWNFISH-47 Fix final methods

Posted by rectang <gi...@git.apache.org>.
Github user rectang closed the pull request at:

    https://github.com/apache/lucy-clownfish/pull/20


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Re: [lucy-dev] [GitHub] lucy-clownfish pull request: CLOWNFISH-47 Fix final methods

Posted by Nick Wellnhofer <we...@aevum.de>.
On 27/05/2015 03:44, Marvin Humphrey wrote:
> Okeedoke -- I'll rework the pull request to further limit the optimization.
>
> Without JIT compilation or even link-time-optimization, we're not going to see
> any kind of big speedup, anyhow.  If we could inline the code or at least
> resolve to a fixed address, things might be different.  But with standard
> linking, we're looking at a PLT lookup, which isn't that different from a
> vtable lookup.  Both involve multiple levels of indirection.  Both will cause
> a control hazard.

Right, maybe it isn't worth the hassle to implement this optimization at all.

> With Go, though, we run into a namespacing problem.  Clownfish parcels can
> have inert functions with the same name in multiple classes.  In Go, they all
> end up in the same Go package and will clash.
>
>      // Go: ordinary func (not method)
>      result := example.Add(1, 2)

I see.

>> On the other hand, we could simply postulate that a final method must never
>> be made non-final.
>
> I hadn't thought hard about this use case before.  Adding `final` to a method
> breaks compat because there could be a subclass out there overriding it.  I
> suppose that removing `final` need only break compat if the compiler performs
> optimizations in other parcels.  I'm fine with foregoing those optimizations
> and making the removal of `final` officially supported.

+1

Nick


Re: [lucy-dev] [GitHub] lucy-clownfish pull request: CLOWNFISH-47 Fix final methods

Posted by Marvin Humphrey <ma...@rectangular.com>.
On Thu, May 21, 2015 at 3:44 AM, Nick Wellnhofer <we...@aevum.de> wrote:

> There's also the issue of keeping the ABI compatible if the "final" property
> of a method is removed, and the method implementation is subsequently
> removed or overridden in a subclass. Such a change is backward compatible,
> so it should be supported in line with our strong ABI compatibility
> guarantees.
>
> To make this work, we must only use direct function calls for final methods
> implemented in a "source" parcel, not in an "included" parcel.
> Unfortunately, this change mostly defeats the performance effects of making
> core Clownfish classes like String, Vector, or Hash final. For example, a
> parcel like Lucy couldn't use a direct function call for things like Vec_Add
> anymore.

Okeedoke -- I'll rework the pull request to further limit the optimization.

Without JIT compilation or even link-time-optimization, we're not going to see
any kind of big speedup, anyhow.  If we could inline the code or at least
resolve to a fixed address, things might be different.  But with standard
linking, we're looking at a PLT lookup, which isn't that different from a
vtable lookup.  Both involve multiple levels of indirection.  Both will cause
a control hazard.

Making explicit choices about what classes and methods are final and what may
be overridden is more about good design.  "Design and document for
inheritance or else prohibit it." -- Joshua Bloch

> There's always the option to convert a method like Vec_Add into an "inert"
> function Vec_add (like we did for some Obj methods).

I don't think any inert functions should be made available as instance
methods, though.  We can't stop it from happening with Perl, but it's not
something we should strive to support across all hosts.

> But we don't
> autogenerate bindings for inert functions. It would be nice if it was
> possible to generate bindings for certain inert functions that take an
> object of their class ("self") as first argument.

For some hosts, we could autogenerate bindings for inert functions regardless
of signature, so long as all types can be mapped unambiguously.  Python, Perl
and Ruby are all pretty straightforward:

    // Clownfish header for `public class Foo` in parcel "example"
    public inert int32_t
    add(int32_t a, int32_t b);

    # Perl: ordinary sub -- i.e. not a class method.
    use Example::Foo qw( add );
    my $result = add(1, 2);

    # Python: probably @staticmethod, possibly @classmethod.
    from example import Foo
    result = Foo.add(1, 2)

    # Ruby: class method
    require "example"
    result = Foo.add(1, 2)

With Go, though, we run into a namespacing problem.  Clownfish parcels can
have inert functions with the same name in multiple classes.  In Go, they all
end up in the same Go package and will clash.

    // Go: ordinary func (not method)
    result := example.Add(1, 2)

> On the other hand, we could simply postulate that a final method must never
> be made non-final.

I hadn't thought hard about this use case before.  Adding `final` to a method
breaks compat because there could be a subclass out there overriding it.  I
suppose that removing `final` need only break compat if the compiler performs
optimizations in other parcels.  I'm fine with foregoing those optimizations
and making the removal of `final` officially supported.

Marvin Humphrey

Re: [lucy-dev] [GitHub] lucy-clownfish pull request: CLOWNFISH-47 Fix final methods

Posted by Nick Wellnhofer <we...@aevum.de>.
On 21/05/2015 02:13, rectang wrote:
> commit 596d282770eb92281190ff7a9158a478ef8cf55a
> Author: Marvin Humphrey <ma...@rectangular.com>
> Date:   2015-05-20T23:40:56Z
>
>      Don't over-optimize final methods.
>
>      It is only valid to resolve a final method to a specific implementing
>      function when that function resides in the same parcel as the invocant.
>      A ancestor class in another parcel has the option of adding or removing
>      an implementation, so the descendant class cannot know for sure what
>      function it should resolve to.

There's also the issue of keeping the ABI compatible if the "final" property 
of a method is removed, and the method implementation is subsequently removed 
or overridden in a subclass. Such a change is backward compatible, so it 
should be supported in line with our strong ABI compatibility guarantees.

To make this work, we must only use direct function calls for final methods 
implemented in a "source" parcel, not in an "included" parcel. Unfortunately, 
this change mostly defeats the performance effects of making core Clownfish 
classes like String, Vector, or Hash final. For example, a parcel like Lucy 
couldn't use a direct function call for things like Vec_Add anymore.

There's always the option to convert a method like Vec_Add into an "inert" 
function Vec_add (like we did for some Obj methods). But we don't autogenerate 
bindings for inert functions. It would be nice if it was possible to generate 
bindings for certain inert functions that take an object of their class 
("self") as first argument.

On the other hand, we could simply postulate that a final method must never be 
made non-final.

Nick