{ "@context":[ "https://www.w3.org/ns/activitystreams", {"Hashtag":"as:Hashtag"} ], "published":"2024-03-05T13:56:37.163Z", "attributedTo":"https://epiktistes.com/actors/toddsundsted", "replies":"https://epiktistes.com/objects/q7SRdDeNG58/replies", "to":["https://www.w3.org/ns/activitystreams#Public"], "cc":["https://epiktistes.com/actors/toddsundsted/followers"], "content":"
The Cost of Small Methods
Ktistec uses a template engine† for it's views.
View templates are transformed into Crystal code that generates HTML when executed. As you'd expect, the template language allows you to use string interpolation syntax (e.g. #{expression}
) for dynamic values.
To ensure expression is only evaluated once, and to limit the scope of the temporary variable holding the evaluated value of expression, I originally bound the value to the variable using Object#tap
(commit 5e1bf19e). The generated code looked something like:
(expression).tap do |__value__|\r\n <template code that uses expression>\r\nend
Blocks in Crystal are always inlined, so the code above should be equivalent to the following (sacrificing local scope):
__value__ = (expression)\r\n<template code that uses expression>
Functionally, they are equivalent. But operationally, not so much! With Object#tap
, the Ktistec executable is about 1% larger (36823603 bytes vs. 36526307 bytes) and build times take 20% longer (23 seconds vs. 19 seconds, generally).
In total, view templates represent about 6% of the Ktistec executable by size, so it doesn't surprise me that there's a measurable impact when I make changes to the template engine, but wow...! I can almost live with the size of the executable, but the build time...!
The cost has to be the method call.
What I'm looking for is something like let
in Scheme. The following macro comes close, but doesn't limit scope quite the same way:
macro let(expr, &block)\r\n {{block.args.first}} = ({{expr}})\r\n {{block.body}}\r\nend
I maybe have to live with the macro—I tried to implement let as a method with the annotation @[AlwaysInline]
but there was no improvement over the original.
†The template engine is a fork of Slang—which I've been evolving to be more Slim-compatible.
", "mediaType":"text/html", "attachment":[], "tag":[ {"type":"Hashtag","name":"#ktistec","href":"https://epiktistes.com/tags/ktistec"}, {"type":"Hashtag","name":"#crystallang","href":"https://epiktistes.com/tags/crystallang"} ], "type":"Note", "id":"https://epiktistes.com/objects/3_Eeanlgujo" }