OpenCageData / perl-Geo-Address-Formatter

Perl module to take structured address data and format it.
https://metacpan.org/pod/Geo::Address::Formatter
8 stars 4 forks source link

replace template lambda subroutines with simpler regular expressions #8

Closed mtmail closed 3 years ago

mtmail commented 3 years ago

The templates we use contains blocks like {{#first}} {{{city}}} || {{{town}}} {{/first}}. In Mustache template systems one can supply custom lambdas (code/subroutine) which will evaluate those blocks. We used to set a lambda called first in _render_template(). The lambda required access to the %components hash to evaluate the remaining template variables.

Turns out the Text::Hogan template engine does some caching of lambdas (irregardless if the compiled template was already cached). Calling template->render with lambdas that accessed address data consumed memory for each call. There's no option in Text::Hogan to control the behaviour.

I added a t/unit/memory_use.t test file. It's switched off by default. Before this PR the tests would consume 300 megabyte extra RAM.

This PR changes the logic: In the template we replace FIRSTSTART_9349813734 {{{city}}} || {{{town}}} FIRSTEND_9349813734. After the rendering we inspect, e.g. FIRSTSTART_9349813734 || Berlin FIRSTEND_9349813734 and return the first value Berlin.

I hope the start and end identifiers are unique enough. It's possible to come up with a different syntax later. There were no changes needed to the address-formatting templates we already use. Unclear if address formatters in other programming languages are effected similarly, I suspect it's Perl specific.