# solved a performance problem in host pattern enumeration

**URL:** <https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656>\
**Category:** Ansible Developer\
**Created:** [October 10, 2013, 1:56pm UTC](https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656 "2013-10-10T13:56:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 10, 2013, 1:56pm UTC](https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656/1 "2013-10-10T13:56:07Z")

</div>

Been running ansible 1.4-devel (a version from september 26, around fd101dd), I yesterday updated to latest (e87be94 22 hours ago 1.3.3 missing from changelog - Michael DeHaan (HEAD, ansible/devel, devel) and noticed a big performance difference.

Runnig ansible 20130926:

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Total elapsed time: 0:00:06.829

with ansible 20131010:

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Total elapsed time: 0:00:41.814

(this is from a playbook where tasks are all skipped, so only inventory and local playbook and runner code involved)

According to git bisect, this is the patch that introduced the change:

8fc46a3a5a286bee5e429b921ef8af9ea7ecbc31 is the first bad commit  
commit 8fc46a3a5a286bee5e429b921ef8af9ea7ecbc31  
Author: Michael DeHaan \<[michael@ansibleworks.com](mailto:michael@ansibleworks.com)\>

Return inventory objects in the order they are presented. Additionally, fix host slicing such that it works on Python terms with  
zero indexed lists and a non-inclusive final element.

I tracked this down to this method, which mentions problem & solution:

250 # TODO: cache this logic so if called a second time the result is not recalculated  
251 def \_hosts\_in\_unenumerated\_pattern(self, pattern):

Wrote a patch to implement this caching, and things are now even better than before:

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Total elapsed time: 0:00:03.566

(FYI, my inventory is around 700 hosts in ini files, so these kind of things tend to hit me soon)

PR for my patch: [https://github.com/ansible/ansible/pull/4429](https://github.com/ansible/ansible/pull/4429)

Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 10, 2013, 3:07pm UTC](https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656/2 "2013-10-10T15:07:47Z")

</div>

I went ahead and merged this, thanks.

Thinking we need to make sure this doesn’t confuse usage of the “add\_host” module.

I am thinking add\_host usage should maybe invalidate this cache?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 11, 2013, 1:59pm UTC](https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656/3 "2013-10-11T13:59:33Z")

</div>

​I'll have a look at [https://github.com/ansible/ansible/issues/4442](https://github.com/ansible/ansible/issues/4442)  
tonight.​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 11, 2013, 2:00pm UTC](https://forum.ansible.com/t/solved-a-performance-problem-in-host-pattern-enumeration/13656/4 "2013-10-11T14:00:23Z")

</div>

I’m taking a look at this right now.

I’ll let you know if I can’t resolve it.
