Skip to content

Rewrite egg package with Cython for performance optimization - #165

Closed
hzhangxyz with Copilot wants to merge 4 commits into
mainfrom
copilot/optimize-egg-library-with-cython
Closed

Rewrite egg package with Cython for performance optimization#165
hzhangxyz with Copilot wants to merge 4 commits into
mainfrom
copilot/optimize-egg-library-with-cython

Conversation

Copilot AI commented Dec 22, 2025

Copy link
Copy Markdown
Contributor

Converts the egg package from pure Python to Cython for performance optimization while preserving the public API and external interfaces.

Changes

  • Core implementation: Converted __init__.py to __init__.pyx with C extension types

    • UnionFind, ENode, EGraph now use cdef class for C-level performance
    • Public methods use cpdef for Python/C interface
    • Internal methods and fields use cdef for C-only execution (no Python overhead)
    • Preserved Python's default hashing for ENode.children tuple
  • Build system: Updated to support Cython compilation

    • Added Cython ~3.0.11 to build dependencies in pyproject.toml
    • Created setup.py with Cython extension configuration
    • Added .gitignore for Cython-generated C/C++ artifacts
  • API surface: Maintained public API compatibility

    • Only functions without underscore prefix remain exposed
    • Member fields properly hidden via cdef declarations
    • apyds.Term integration unchanged (external interface)

All 20 existing tests pass without modification. The implementation compiles to a native C extension (~895KB .so file).

# Usage remains identical
from apyds_egg import EGraph
import apyds

eg = EGraph()
a = eg.add(apyds.Term("a"))
b = eg.add(apyds.Term("b"))
eg.merge(a, b)
eg.rebuild()
Original prompt

This section details on the original issue you should resolve

<issue_title>使用cython改写/egg中的库</issue_title>
<issue_description>尽可能的优化,除了apyds的Term这个外部部分。

几个类型的member field其实并不是外部接口,只有那几个没有下划线开头的函数是外部接口,其他并不需要暴露。

enode的tuple的那个children实际上内容都是ClassId,只是要用Python默认的hash,所以设置成tuple的,写成cython时可能需要注意。

注意build system需要更新以支持cython。</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Dec 22, 2025
Copilot AI and others added 3 commits December 22, 2025 12:48
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize library in egg using Cython Rewrite egg package with Cython for performance optimization Dec 22, 2025
Copilot AI requested a review from hzhangxyz December 22, 2025 12:54
@hzhangxyz hzhangxyz closed this Dec 23, 2025
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.

使用cython改写/egg中的库

2 participants