An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

Duck Typing is not about Performance

July 2, 2006 · 13 Comments

I've seen a few people focus on performance after my duck typing session. I'd like to publicly say: if you think duck typing is about performance, you are completely missing the point! That's a small side-effect, nothing more. Do NOT start duck typing because you think it will improve performance! That's just "premature optimization", a developer malady that needs medical treatment. Go back and look at the slides and see what else you can pull out of it. Don't be a premature optimizer!

Tags: coldfusion

13 responses so far ↓

  • 1 Tony Petruzzi // Jul 3, 2006 at 6:05 AM

    Sean,

    I'm curious. I looked over the pdf for "duck-typing" and yes, the thing that hit me the most was the fact that you and Joe claimed that you got a 2-3 fold increase in speed by using it. As the title of your post suggest, a lot of people are focusing mainly on this factor and nothing else.

    I will admit that I'm not a fan of this new concept. I strongly believe that we should all be typing our objects and methods within CF.

    What I'm curious about is how these tests were performed? Were the frameworsks tested under load or by just using the famous "looping test". Were the objects created and cached into memory or were they being created on the fly? We all know that it is very expensive in CF to create objects.

    The thing that really bothers me is that I see a lot of claims in the community about what increases performace of CF applications but very rarly do I see a thorough, well documented write up about how these tests were performed. And I'm sorry to say but I'm seeing that with this claim.
  • 2 Terrence Ryan // Jul 3, 2006 at 6:10 AM

    Sean, I wasn't at CFunited, so I didn't have the oppertunity to misundertand you directly. I've only had online presenations and blog entries to go on.

    The gist that I got was: duck typing was about performance, and that one should look at using it when you needed to squeeze some extra performance out of something, like say... a framework that is run on every page call. Therefore do it where it made sense. But according to this, I clearly missed the point.

    So what is duck typing about then? Is there another one word answer other than performance you would like us to take away from your presentations?
  • 3 Peter Bell // Jul 3, 2006 at 7:07 AM

    Hi Terrence,

    See the comments to his previous posts.

    Bottom line: ColdFusion is NOT Java - look to Ruby instead. Lets take advantage of the strengths of dynamic typing and use them to write simpler code.

    With dynamic typing we don't need nulls and interfaces and we can do cool things like mixins to solve specific problems.

    For me the breakthrough idea was that you can treat methods of an instantiated object as first order variables - moving them around, renaming them, etc. I think the reason people haven't blogged about that is that it may take us a while to get our heads around what to do with that information and the possibilities (good and bad) that it will raise.

    It's liek AOP. Everyone says it is about workflow and logging, but it can do so much more.

    Best Wishes,
    Peter
  • 4 Peter Bell // Jul 3, 2006 at 7:13 AM

    Sean,

    I will say just one thing - you don't KNOW whether using Duck typing for performance is premature optimization or not. As a wise man once told me: "it depends"!!!

    Are you and Joe premature optimizers? How do you know definitively that no other CF developer has hit into a genuine performance bottleneck that matches the possible performance benefits of Duck Typing.

    I don't think it is up to you to "prove" that duck typing provides performance benefits in some cases - if someone wants to see if it improves their performance they can run some real world tests themselves.

    At the same time I don't think it is reasonable to blow off the potential performance benefits. If there is never any reason to use duck typing for performance under any circumstances, why did smart people like Joe and yourself waste your time on it?!

    My take away is exactly what I posted - performance is the least interesting part of the talk, but it is easy to post on and understand and if someone has the proven pain point they should feel free to consider the technique as long as they understand the testing and documentation implications. I don't need the performance part of duck typing, but there's nothing wrong with people using it if they have a genuine need.

    Best Wishes,
    Peter
  • 5 Terrence Ryan // Jul 3, 2006 at 7:43 AM

    Thanks Peter, that does help. So if I paraphrase you for my own comprehension...

    Treating ColdFusion like a strongly typed language reduces our options. Duck typing is about using the features of dynamic typing to solve problems. This increases our options. And as a small aside you've seen some slight performance benefits from doing so.
  • 6 Peter Bell // Jul 3, 2006 at 7:55 AM

    Exactly. The only point I'd make is that the use cases for the performance vs. the flexibility pieces are different. There will be times duck typing is extremely useful for solving an architectural problem and will provide no discernable performance improvement.

    I'd also reiterate the "methods are first order variables". Mixins are just the start of what you can do with this. It is incredibly powerful, but whether it is a blessing or a curse (anyone remember "GOTO" from Basic?!) is yet to be seen.

    Best Wishes,
    Peter
  • 7 Zach Loch // Jul 3, 2006 at 9:32 AM

    I think the reason most people jumped on the performance idea is because it is the most immediate benefit for most people. Duck typing is a niche solution for a niche problem. I haven't encountered too many problems that can only be solved cleanly with duck typing. However, performance gains I can use today...right this minute.

    After hearing the session I've concluded that I will use duck typing only when necessary and will apply it to only those functions/arguments that are immediately involved with the solution. Otherwise, I will continue to type functions/arguments in development and stip out the typing information when I deploy (for performance).

    My only gripe with duck typing is that I only work on large applications with many developers and I'm not going to force someone to look through my comments or code to figure out what my functions return or what my arguments accept. If someone has a good solution for that problem I would consider taking out all typing information......until then I'm going to 'type away'.

    As a side note, Michael Dinowitz (in his session at cfunited) stated major performance gains on his client sites just by removing type information.

    -Zach
  • 8 Sean Corfield // Jul 3, 2006 at 10:42 AM

    Zach, yes, I heard during the conference that Michael had also discovered that removing ColdFusion's type checking had performance benefits.

    Joe was doing full-blown load testing to see what he could tune. I suggested removing type checking, based my experience with Fusebox 5, and his load tests confirmed that it did, indeed, speed his code up.

    My tests were slightly different. I was trying to solve the one-off, straight-line "initial compile" performance issue in Fusebox 5. So I was profiling for code that was executed repeatedly. I removed type checking from the most commonly called methods and saw a speed up.

    Since ColdFusion performs type checking at runtime, it should be obvious that if you remove type checking, ColdFusion will do less work at runtime and therefore will be faster to some degree. It's such a "duh!" concept that I'm rather surprised lots of us hadn't figured it out earlier. I think it shows how deeply in bed with Java concepts we have become.

    Tony, duck typing has *nothing* to do with any perceived overhead of creating CFCs. Duck typing is all about *using* CFCs.

    Enough about performance!

    Duck typing means you don't need to concoct some trick to try to implement interfaces in ColdFusion. Duck typing let me remove nearly half a dozen components from the Fusebox 5 core files as I refactored code and started to adopt duck typing. That was my primary interest in the technique at first. The performance was very much a side-effect.

    Duck typing means you don't need null because it's OK to pass a non-object value out of a CFC and use isObject() to test whether you got an object or you non-object value (in Java you pass back null or an object so you still have to do the test, it's just syntactic sugar really).

    The presentation's main focus was on the type system of ColdFusion in comparison to Java. It was intended to show how rigid Java's system is and why it needs some of the constructs it has - including constructs ColdFusion does not have that people clamor for (null, interfaces, strict typing). But the reality is that we have a powerful language that shares a lot with classic languages like Smalltalk and popular, modern languages like Ruby - and we should leverage that.
  • 9 Peter Bell // Jul 3, 2006 at 3:44 PM

    Hi Sean,

    I get the fact that we don't need nulls and interfaces and more.

    I get what mixins are and the two broad ways of implementing (cfincluding or adding the methods at runtime by treating them as first order variables).

    I have a feeling there are a million other interesting things that could be done with the ability to treat methods are first order functions. For instance, you could implement AOP by renaming all methods as _original and copying "wrapper" functions with before and after code that would call the original code - kind of an "in class decorator".

    Do you have any ideas of any other things we could be doing with treating methods as first order variables? I'm just trying to find someone with a handle on some of the things we could do with this technology . . .

    Best Wishes,
    Peter

  • 10 Sean Corfield // Jul 3, 2006 at 3:59 PM

    The first class citizen status of functions isn't strictly speaking something that comes from the dynamic typing of ColdFusion. ColdFusion components compile to Java classes. ColdFusion functions also compile to Java classes. A component instance has references to an instance of each method declared in the component. A user-defined function in any page is also compiled to a Java class. Within the page, a reference to that function is actually a reference to an instance of the class that the function compiled down to. That means you can write a UDF and assign it to the public scope of a component - and the late binding of variable names means that name lookup will be done in the context of the call, i.e., inside the componenet (even tho' the function was written outside the component).

    Like I say, don't confuse that with duck typing.

    However, you can take advantage of the late binding of variable name lookup and the first class citizen nature of functions to turn any component into one that implementation any interface necessary, so you can use these techniques to help leverage the power of duck typing.
  • 11 Peter Bell // Jul 3, 2006 at 4:06 PM

    Hi Sean,

    Thanks for the clarification!

    Best Wishes,
    Peter
  • 12 jim // Jul 9, 2006 at 8:27 AM

    Can't we come up with a better name than Duck Typing?! I understand the, "if it walks like a duck...", but we're developers, people - lets come up with a better name. How about, interfaceless?
  • 13 devon // Feb 9, 2009 at 10:46 AM

    I just got a huge boost in performance by duck typing - thanks Sean!

Leave a Comment

Leave this field empty: