Skip to content

Performance improvements#1337

Merged
chrisrink10 merged 3 commits into
mainfrom
refactor/performance-tweaks
May 10, 2026
Merged

Performance improvements#1337
chrisrink10 merged 3 commits into
mainfrom
refactor/performance-tweaks

Conversation

@chrisrink10
Copy link
Copy Markdown
Member

@chrisrink10 chrisrink10 commented May 10, 2026

  • Take advantage of itertools.batched (which is written in C) for Python 3.12+ replacing basilisp.lang.util.partition.
  • Stop creating Maybe objects in the constructor of Cons cells, since a simple if expression check would suffice.
  • Make NodeEnv types mutable, reducing the amount of new object creation via attrs.evolve that needs to happen at compile time for new AST nodes.

Comment thread src/basilisp/util.py
from itertools import batched

def partition(coll: Sequence[T], n: int) -> Iterable[tuple[T, ...]]:
return batched(coll, n)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Batched is written in C, so should be much faster. I noticed partition dropped out of the top own-time function calls once I switched this so 🤞 this should help improve performance for versions of Python 3.12+.

Comment thread src/basilisp/lang/seq.py
) -> None:
self._first = first
self._rest = Maybe(seq).or_else_get(EMPTY)
self._rest = EMPTY if seq is None else seq
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this extra allocation and method call was adding a lot of overhead to just creating a new Cons cell, which happens quite frequently. This simple check should be a bit faster.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Creating new NodeEnv nodes recursively is expensive. Making the type mutable and mutating it should vastly reduce the performance impact of this method.

@chrisrink10 chrisrink10 merged commit 7aa670d into main May 10, 2026
12 checks passed
@chrisrink10 chrisrink10 deleted the refactor/performance-tweaks branch May 10, 2026 17:37
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.

1 participant