Final makes no difference?
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Thu Aug 24 12:43:41 PDT 2006
BCS wrote:
> John Demme wrote:
>
>> BCS wrote:
>>
>>
>>> John Demme wrote:
>>>
>>>> I'm working on fixing and optimizing my XML parser. It uses some
>>>> classes, and lots of virtual method calls, so I figured I could really
>>>> increase my
>>>> speed by making everything final. I made the classes final, and I put
>>>> all of the methods in a big final{} block, but it made no appreciable
>>>> difference in my time trials.
>>>>
>>>> Am I doing something wrong? Does DMD currently not optimize for
>>>> final? Or are virtual method calls not nearly as bad as I thought?
>>>> I'm making a
>>>> ton of calls into small functions, so I was also hoping final methods
>>>> would be candidates for inlining...
>>>>
>>>> I'm using DMD 0.163 and compiling with -O and -inline.
>>>>
>>>> Thanks
>>>
>>>
>>> if you aren't using inheritance, try using structs.
>>
>>
>>
>> Yeah, I know... I'm probably going to switch several of the classes
>> over to
>> structs, but the big main class implements an interface. I don't
>> absolultely need the speed right now- I'm just wondering if any changes
>> will be made in the future, and if I'm expecting the right thing from
>> final.
>>
>
> That brings up another bone of mine. why can't struct implement an
> interface? For that matter why not a function?
>
>
>
> interface Foo
> {
> char fig(int);
> int bar(char);
> }
>
> void fun()
> {
> char[] cmap;
> int[] imap;
>
> char first(int i){return cmap[i];}
> int second(char c){return imap[c];}
>
>
> Foo f = interface : Foo // making up a syntax...
> {
> alias first fig;
> alias second bar;
> }
>
> FnTakingFoo(f);
> }
Tested with DMD 0.165, no errors, runs perfectly:
# module funcface0;
#
# import std .stdio ;
#
# interface Foo {
# char[] str () ;
# int num () ;
# }
#
# void bar (Foo f) {
# writefln("str('%s') num(%d)", f.str(), f.num());
# }
#
# void func (char[] s, int n) {
# Foo f = new class Foo {
# char[] str () { return s ; }
# int num () { return n ; }
# };
# bar(f);
# }
#
# void main () {
# func("The answer is", 42);
# }
I did try your trick of aliasing nested functions into the anonymous class' scope...
Unfortunatley it didn't work; DMD complained the interface was left unimplemented. Not a
really big deal, as anonymous classes apparently get access to the frame they were created
in, the same as a delegate does I'd imagine.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d-bugs
mailing list