Todd Sundsted
Todd Sundsted
toddsundsted@epiktistes.com
Better dead than bored.
Introductionepiktistes.com/introduction
GitHubgithub.com/toddsundsted/ktistec
Pronounshe/him
🌎Sector 001
Todd Sundsted

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.)

#ktistec #crystallang #optimization

Todd Sundsted
Release v2.4.5 of Ktistec

Ktistec release v2.4.5 rolls out the build time and executable size optimizations I've been blogging about here. It also fixes a few small bugs.

Fixed

  • Handle @-mentions with hosts in new posts.
  • Handle HEAD requests for pages with pretty URLs.
  • Destroy session after running scripts.

Changed

  • Delete old authenticated sessions.

I've started a branch full of query optimizations. My general rule—as highlighted in the server logs—is if a query takes longer than 50msec, it takes too long. It's time to address some problems...

#ktistec #fediverse #activitypub #crystallang

Todd Sundstedalexanderadam
Serdar's post on Twitter saying:
Great news everyone

I've updated the Kemal Crystalkemal Cookbook with more recipes (Cookies, Databases, Redis e.g)

What else would you like to see in Kemal Cookbook?

Serdar updated the #crystalkemal cookbook with more recipes (i.e. #Cookies, #Databases, #redis ).

If you're looking for a #sinatra like framework for @CrystalLanguage, then #kemalcr is the best way to go.

#CrystalLang #CrystalLanguage#kemal

Todd SundstedCrystalLanguage

Happy New Year, happy new release! 🎇
1.15.0 is out with a new, efficient event loop, support for MinGW-W64 and MSYS2, improvements for BSD platforms, and many more features.
Watch out for the formatter changes, they'll likely affect your codebase!

crystal-lang.org/2025/01/09/1.

Todd Sundsted

@jayvii i just discovered your introduction page. i liked it so much i copied the idea!

Todd Sundstedmiry

Migrated a simple API endpoint from #Rails to #Crystal (#CrystalLang) using the #Marten web framework. It’s incredible to see a web application running on just 2MB of memory—hard to imagine that’s even possible!

PS: Congratulations on the release of Crystal 1.15!

Todd Sundsted

The prologue to this post, and other posts in the series, is here.

Investigating commit b65d292f was fruitful but not for obvious reasons.

Dumping the symbols (nm -j server) before and after the commit showed large number of new equality (==) methods. From the diff:

1765a1772
> _*ActivityPub::Activity::Accept#==<Translation>:Bool
1920a1928
> _*ActivityPub::Activity::Add#==<Translation>:Bool
2062a2071
> _*ActivityPub::Activity::Announce#==<Translation>:Bool
2237a2247
> _*ActivityPub::Activity::Block#==<Translation>:Bool
    ...

The use, in a controller action, of the new Translation model seemingly triggered their generation. What was going on?

A long time ago I implemented a MVC model framework in the style of ActiveRecord (2de4a4b3) and it included a method for testing for equality. Note the method signature.

# Returns true if all properties are equal.
#
def ==(other : self)
  {% begin %}
    {% vs = @type.instance_vars.select(&.annotation(Persistent)) %}
    if
      {% for v in vs %}
        self.{{v}} == other.{{v}} &&
      {% end %}
      self.id == other.id
      true
    else
      false
    end
  {% end %}
end

The Reference class—the default parent for classes—defines two base implementations of this method:  one that tests for identity (not equality), with the signature def ==(other : self), and another that returns false, with the signature def ==(other). When I implemented my method, my assumption was: redefine the former for model classes and let the latter take care of everything else. This assumption was incorrect.

In circumstances that I still don't completely understand, the compiler will generate calls to the latter (the method that just returned false) when it "should have" been calling the former, and comparisons failed when they should have succeeded. I "fixed this" with commit effeaa26 that removed the type restriction and explicitly handled the type check. Everything worked!

The problem is Crystal creates a version of this method for every possible model comparison, specialized by both self and other. Most of the time the type check fails and the method returns false. But the rest of the code is still present.

The fix (re)adds a method specialization that returns false and lets the compiler handle the type check.

# Returns `false`.
#
def ==(other)
  false
end

Because this method just returns a constant value, the compiler gets rid of the method call, as well.

Interestingly, this change reduced the size of the Ktistec server executable by 4.0% when building without the --release flag but only 0.2% when building with it, so optimization does a good job at cleaning this up even without the change. 

#ktistec #crystallang

Todd Sundsted

my content filters are getting a workout today...!

Todd Sundsted

what's the union of all errors that a call like HTTP::Client.get(...) (in Crystal) might raise?

i typically rescue IO::Error (which gets hostname lookup and socket connection problems), OpenSSL::Error (which gets a few edge-case problems with SSL configuration on the other end), Compress::Deflate::Error and Compress::Gzip::Error (which gets a few even more edge-case configuration problems on the other end), and URI::Error.

what am i missing?

#crystallang

Todd SundstedJamie Gaskins

I've been wanting to start up a blog again for a while. So I finally did, using Ktistec by @toddsundsted.

Since Ktistec uses ActivityPub, you can follow @jamie@jgaskins.blog if you want to read it.

jgaskins.blog