Skip to content

Handle tempita templed cython files - #1924

Merged
emmanuelle merged 3 commits into
scikit-image:masterfrom
ev-br:tempita
Feb 16, 2016
Merged

emmanuelle merged 3 commits into
scikit-image:masterfrom
ev-br:tempita

Conversation

@ev-br

@ev-br ev-br commented Feb 2, 2016

Copy link
Copy Markdown
Contributor

@soupault soupault added the ⏩ type: Enhancement Improve existing features label Feb 2, 2016
@emmanuelle

Copy link
Copy Markdown
Member

@ev-br just a naive question: what's the interest of using templates instead of global variables?

@stefanv

stefanv commented Feb 14, 2016

Copy link
Copy Markdown
Member

@emmanuelle If you take a look at the PR referenced, you'll find a section of code that is generate by a for loop:

+ {{for _octant in range(8)}}
+ # octant {{_octant}}:
+    n = 1
+    {{for _j in range(7):}}
+    {{py: _idx = _neighb_idx[_octant][_j]}}
+    if neighbors[{{_idx}}] == 1:
+        n |= {{1 << (7 - _j)}}
+
+    {{endfor}}
+    euler_char += lut[n]
+    {{endfor}}

While one could potentially use variables to switch out between copied and slightly modified code sections, this is much cleaner.

Comment thread skimage/_build.py Outdated
from_filename = tempita.Template.from_filename
template = from_filename(fromfile, encoding=sys.getdefaultencoding())
pyxcontent = template.substitute()
assert fromfile.endswith('.pyx.in')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert is probably best replaced with a raise ValueError

@stefanv

stefanv commented Feb 14, 2016

Copy link
Copy Markdown
Member

Some minor style comments, but looks OK to me.

@ev-br

ev-br commented Feb 16, 2016

Copy link
Copy Markdown
Contributor Author

@emmanuelle To add to what Stefan said: in this context, a dozen lines of a template helps to avoid either ~100 lines of repetetive boilerplate (check the .pyx file generated from .pyx.in during the build) or a large perf hit due to looping at run time (this is most useful when the loop is over a python data structure). In gh-1923 there are two templated routines, and together they account for about an order of magnitude speedup.

@emmanuelle

Copy link
Copy Markdown
Member

@ev-br @stefanv many thanks for the useful explanations, I think I get it now :-). I hadn't understood at first sight that one benefit was to get rid of Python objects and to generate code without such objects.

@emmanuelle

Copy link
Copy Markdown
Member

LGTM, and I think for Stéfan as well, therefore merging.

emmanuelle added a commit that referenced this pull request Feb 16, 2016
Handle tempita templed cython files
@emmanuelle
emmanuelle merged commit 6614e13 into scikit-image:master Feb 16, 2016
@ev-br
ev-br deleted the tempita branch February 17, 2016 09:35
ev-br added a commit to ev-br/scikit-image that referenced this pull request Feb 22, 2016
It got botched sometime around reviewing/merging/rebasing of scikit-imagegh-1924
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⏩ type: Enhancement Improve existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants