"DCI" in Ruby is completely broken
tonyarcieri.com
tonyarcieri.com
(using ruby 1.9.3p194)
Calculating -------------------------------------
without dci 68507 i/100ms
with dci 24409 i/100ms
with delegator 46945 i/100ms
-------------------------------------------------
without dci 2240202.4 (±3.2%) i/s - 11235148 in 5.020463s
with dci 412445.3 (±3.7%) i/s - 2074765 in 5.037113s
with delegator 1018928.4 (±1.6%) i/s - 5117005 in 5.023310s
RUBY: require 'rubygems'
require 'benchmark/ips'
require 'delegate'
class ExampleClass
def foo; 42; end
end
module ExampleMixin
def foo; 43; end
end
class ExampleProvisioner < SimpleDelegator
def foo; 44; end
Benchmark.ips do |bm|
bm.report("without dci") { ExampleClass.new.foo }
bm.report("with dci") do
obj = ExampleClass.new
obj.extend(ExampleMixin)
obj.foo
end
bm.report("with delegator") do
ExampleProvisioner.new(ExampleClass.new).foo
end
endSubtracting the results of different runs like this is pretty questionable, though (the errors accumulate), so the above is to be taken with a pinch of salt.
One thing I've learned never to do is to "customize" short-lived objects at runtime with new methods. If you create an object in response to user input, it should be born with all the methods it's going to need.
Imagine a language where a call looked like this:
user.doSomething(1, 2, 3)
But it's just syntax sugar for this function application: doSomething(user, 1, 2, 3)
("user" would be just the data..like a struct type thing)That way you get the flexibility of loose functions but you get the (imo) superior aesthetics of oop languages.
>>> class Foo(object):
... def __init__(self):
... self.x = 1
... self.y = 2
... def bar(self):
... return self.x + self.y
...
>>> Foo().bar()
3
>>> f = Foo()
>>> Foo.bar(f)
3
>>> def foo(bar, baz):
print bar.baz + baz
f = Foo()
f.foo(3) # error package Foo {
sub new {
my ($class, $bar) = @_;
bless {bar => $bar}, $class
}
sub bar {
my ($self, $x) = @_;
say $self->{bar} . $x
}
}
my $foo = Foo->new( 'Hello...' );
# method call
$foo->bar( 'baz' ); # => Hello...baz
# function call
Foo::bar( $foo, 'baz' ); # => Hello...bazThe difference is that (usually) methods are looked up where the classes are defined, but subroutines are looked up where they are called. So visibility is different in the case of method invocation.
prefix(arg) // for function-like things
arg postfix // for "getters"
arg infix(otherarg) // for method-like things
It's purely a syntactic distinction. Semantically, methods are always multimethods and are not tied to any receiver class. You can freely define new methods that dispatch on existing classes.I find it works really well, though it brings in some unanticipated complexity. You'll really want multimethods or some form of argument-based polymorphism. Also, scoping get a bit more confusing if you want "overriding" to work like you expect across modules.
I see very little reason for the dogma behind the way role injection is typically handled, yet everyone seems to be attached to doing this in Ruby. This is odd because there are DCI examples in almost every language, each one has it's awkwardness when it comes to roles, but they all manage to get it done with some workaround. I don't see why Ruby should be any different, if it wasn't possible to call #extend, we'd just do it another way.
Either way, I wouldn't dismiss DCI just because the canonical approach some academics cooked up is completely senseless in practice. In fact the author seems to agree in the end so the title is a bit exaggerated.
DCI is a powerful design concept in any class-based language. When behavior gets complex, DCI is the cure for the "kingdom of nouns" that results from a beginner-level understanding of how to apply OOP. In short, DCI is a way to elevate business tasks to first-class citizens.
The singleton method business is purely an aesthetic concern. It definitely makes for more appealing code, but at the cost of an order of magnitude performance hit. All else being equal I love elegant code as much as the next rubyist, but this is simply too high a price to pay and I'm shocked that so many people find this acceptable.
At GameBoxed (http://gameboxed.com/) we've got a Rails app that uses a lot of .extend on every request. It's in production for 1.5 year now.
It's a backend for multiple social games (one backend app for many CoffeeScript frontends).
It's not huge in terms of traffic and performance needs. We implemented it with a DCI-like architecture in mind. It was helpful at the beginning. DCI wasn't the only way to implement it, but it was the simplest way for us at that time.
We've had some performance problems, but they were never related to the usage of .extend. If it was a performance problem, we would probably consider removing the .extends (not a big deal), switch to Java, do more caching, there are so many options.
Again, we don't have huge performance needs, we've maybe 40 req/s at peak times.
DCI is much more than .extend, it's a huge shift in thinking about the OOP architecture.
If we (as Ruby community) agree that it's an architecture worth trying, then maybe it makes sense to introduce better optimisations techniques to the current Ruby implementations?
Anyway, it's great to see the discussion happening. DCI may not be ready to use right now, but it's more of a change in thinking than in the implementation.
I definitely wouldn't call it completely broken in Ruby.
Here are some of my posts on DCI in Ruby:
http://andrzejonsoftware.blogspot.com/2011/02/dci-and-rails....
http://andrzejonsoftware.blogspot.com/2012/01/dci-and-rails-...
http://andrzejonsoftware.blogspot.com/2011/08/dci-patterns-h...
At the design level - flat hierarchy
At an abstract level - thinking in terms of objects, not classes.
You don't have to. They're built into SimpleDelegator. Syntactically the difference is you'd write:
class MyThing < SimpleDelegator
instead of: module MyThing
And do: MyThing.new(obj)
instead of: obj.extend(myThing)
> At an abstract level - thinking in terms of objects, not classes.It seems to me like you're trying to use a module to solve a problem which can be solved with an object. I don't think that's thinking in objects at all... it's thinking in modules.
class Account; …; end
class Withdrawer < SimpleDelegator; …; end
class Depositor < SimpleDelegator; …; end
acc = Depositor.new(Withdrawer.new(Account.new))
acc.withdraw(40)
acc.deposit(100)
At least that's how I understand it. I'm not convinced that DCI is worth it, but…I don't yet have a need for it in the application that I'm working with.What's the benefit of this kind of typing, though? I'm pretty much just a C hacker these days, so I'm pretty ignorant about Ruby, but does this apply to the object or to the class?
It seems simpler, especially on a conceptual level, to just initialize the the duck with everything it could possibly need to be a duck, rather than adding things dynamically.
Metaprogramming like this generally is most useful for powering "DSLs" and other ways of expressing not-quite-imperative-code in Ruby.
Yes, it is simpler conceptually. But, in Rails especially where there is a tradition of moving all your application functionality into the "model" layer, you could end up with a single class that is thousands of lines of code long. For example, a User class, with a load of code about resetting passwords, that is only used in one relatively rare circumstance.
So instead, DCI says you should separate the password-reset stuff into a separate module and only add it in when needed. Both User and PasswordReset are simpler and easier to understand and only come together when needed.
(As I said elsewhere, I prefer to wrap a decorator around the User to achieve the same thing).
It is simpler, on a conceptual level. On an implementation level however, it quickly becomes non-simple.
One of the fundamental modularity concepts is 'separation of concerns' - this isn't something you always need to do, but when a concern (a scoped set of functionality) grows large, it should be implemented separately, to keep the conceptual complexity of individual abstractions and implementations minimal.
If I implement all of the logic for handling conditions, importing data, extracting reports, managing permissions, serialization, and resource handling in the same object, it's a very complicated object. I have no way without getting a full mental model of the thing in my head that changing X about it won't break Y, or what parts of code depend on the structure of the results of calling Z.
There are plenty of ways to skin that cat, and different ones are more appropriate in different places. Often it's correct to extract the logic into a generalizable mixin, which in Ruby would be a module, and then include it into the class. Sometimes it's better to extract the logic and the concept it represents into another class that has a relationship with the original one. And sometimes it's better to separate the logic and code into a 'context' as DCI describes - I generally prefer Decorators for this, but the Rails community seems to lean toward using modules here also, largely on the weight of DHH's opinion (app/concerns/).
Go is way faster than Ruby and extensively uses duck typing everywhere (but is also statically typed and not interpreted-). My point is duck typing is not the reason Ruby is so slow.
Delegation (especially with the extreme simplicity of SimpleDelegator) seems like the obvious solution here, at least to me, but some people prefer to use cache-busting mixins instead.
Is your app CPU-bound? If so, then DCI probably isn't a great idea for you. In fact, building it in Ruby may not be the best idea.
But if your app is IO-bound (like most Rails apps), huge and it takes new developers weeks to get up to speed, then the gains from having a simple, modular code-base should save you developer-time (and hence salaries), which are much more expensive than CPUs.
Having said that, I tend to use SimpleDelegator instead of dynamically injecting stuff into objects.
EDIT: added 'ruby not being the best idea if CPU bound'
There's even a protocol for notifying instances that their class has changed so they can make any necessary adjustments: http://clhs.lisp.se/Body/07_bb.htm
Can't you just have a method cache per metaclass (or class; I'm not very familiar with how Ruby factors this stuff)? Since mixing in a role into an instances creates a new metaclass anyway, there's no need to affect instances of the original class.
If DCI helps to keep your code maintainable, readable, and less buggy, is that a better tradeoff? I would think that 95-99% of the time, the answer is yes.
On a side note, if speed is mission critical, then would ruby really be the language of choice to use?
Famous last words.
Computers are much faster than they were years ago, but look at how much crap they have to deal with... So much crap in fact that we barely notice they are indeed much faster. So much crap that hardware can barely keep up.
Performance is never mission critical until it becomes mission critical, and that usually happens all of a sudden. It's that moment a few days into production when the system becomes unbearably slow and the rest of the company is breathing down your neck asking "WTF is going on?! Fix it, NOW!".
It's problematic enough when you are just creating to many objects and making processor caches irrelevant, but if your performance issues are baked directly into your coding style... good luck with that.
My question of the day: why isn't more time spent on building abstractions that aren't just convenient but that are also fast? Why does readable, maintainable code have to mean code so slow it effectively turns Core i7s into Pentium 90s?
I don't think it has to mean that at all, but that's exactly the choice a lot of programmers seem to think they're making.
s = "foo"
d = SimpleDelegator.new s
s2 = d << "bar"
s2.object_id == s # true
I think SimpleDelegator is really neat but totally dangerous. Doesn't this ring alarm bells with you? # delegate.rb L117
#
# Returns true if two objects are considered of equal value.
#
def ==(obj)
return true if obj.equal?(self)
self.__getobj__ == obj
endI do something slightly different. I create a class directly for the context and then fire an object into it.
class TheContext def initialize # ... end
def execute(the_object)
# ...
end
endDelegating is usually cheaper, just be careful how you do it.
https://gist.github.com/4441557
I guess the upside of `extend` is that you can do it at the call site, delegation has to be configured upfront.