This action will delete this post on this instance and on all federated instances, and it cannot be undone. Are you certain you want to delete this post?
This action will delete this post on this instance and on all federated instances, and it cannot be undone. Are you certain you want to delete this post?
This action will block this actor and hide all of their past and future posts. Are you certain you want to block this actor?
This action will block this object. Are you certain you want to block this object?
Crystal is fast because methods are monomorphized at compile time. In simple terms, that means that at compile time, a polymorphic method is replaced by one or more type-specific instantiations of that method. The following polymorphic code...
def plus(x, y) x + y end
...is effectively replaced by two methods—one that does integer addition if called with two integers, and one that does string concatenation if called with two strings.
This extends to inherited methods, which are implicitly also passed self
. You can see this in action if you dump and inspect the symbols in a compiled program:
class FooBar def self.foo puts "#{self}.foo" end def bar puts "#{self}.bar" end end FooBar.foo FooBar.new.bar class Quux < FooBar end Quux.foo Quux.new.bar
Dumping the symbols, you see multiple instantiations of the methods foo
and bar
:
... _*FooBar#bar:Nil _*FooBar::foo:Nil _*FooBar@Object::to_s<String::Builder>:Nil _*FooBar@Reference#to_s<String::Builder>:Nil _*FooBar@Reference::new:FooBar _*Quux@FooBar#bar:Nil _*Quux@FooBar::foo:Nil _*Quux@Object::to_s<String::Builder>:Nil _*Quux@Reference#to_s<String::Builder>:Nil _*Quux@Reference::new:Quux ...
The optimizer in release builds is pretty good at cleaning up the obvious duplication. But during my optimization work on Ktistec, I found that a lot of duplicate code shows up anyway.
Most pernicious are weighty methods that don't depend on class or instance state (don't make explicit or implicit reference to self
). As I blogged about earlier, this commit replaced calls to the inherited method map
on subclasses with calls to the method map
defined on the base class and reduced the executable size by ~5.8%. The code was identical and the optimizer could remove the unused duplicates.
So, as a general rule, if you intend to use inheritance, put utility code that doesn't reference the state or the methods on the class or instance in an adjacent utility class—as I eventually did with this commit.
(The full thread starts here.)