Skip to content

Improved Chain.build() performance by 1/3#190

Open
egormanga wants to merge 1 commit intojsvine:masterfrom
egormanga:fix/setdefault
Open

Improved Chain.build() performance by 1/3#190
egormanga wants to merge 1 commit intojsvine:masterfrom
egormanga:fix/setdefault

Conversation

@egormanga
Copy link
Copy Markdown

  • Using dict.setdefault() is much faster than doing a separate dict.__contains__() check each iteration. This gives us ~1/6× better performance.
  • But keeping the in-place addition and falling back by catching KeyError gives another 1/6 over using dict.get().

Comment thread markovify/chain.py Outdated
try:
state[follow] += 1
except KeyError:
state[follow] = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should be 1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're perfectly right, thank you :)

@S0yKaf
Copy link
Copy Markdown

S0yKaf commented Apr 13, 2026

Hopefully this gets merged, it's a good change! I've been using your branch because of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants