Advice on optimizing a Python data driven rules engine

Chris Angelico rosuav at gmail.com
Thu Aug 11 12:27:45 EDT 2016


On Fri, Aug 12, 2016 at 12:56 AM, Malcolm Greene <python at bdurham.com> wrote:
> Looking for some advice on how to optimize the BOILERPLATE portions of
> the following type of code. There's an awful lot of dot dereferencing
> going on. One thought was to pass in the values being dereferenced as
> parameters and return value. But this would just move the dereferencing
> to another point in my program and would add the overhead of parameter
> passage. Is there a technique where I could store a reference to these
> values that would make their access more efficient?

Until you find that the dereferencing of dots is actually hurting you,
don't change anything. Most likely, it won't be a performance problem,
so the only question is: Does this hurt code readability? And
optimizing for code readability by adding levels of indirection is
often a bad idea.

What you MAY be able to do, though, if (as I'm guessing) the
BOILERPLATE sections are repeated across lots of functions, is to
refactor the functions to put those bits somewhere else. If nothing
else, you could make a function decorator that surrounds the code with
the appropriate boilerplate. But that's still indirection, and only
worthwhile if there's a lot of duplication. Remember, every time you
read this code, you'll need to go look elsewhere for the other half of
the guts.

ChrisA



More information about the Python-list mailing list