Skip to content

Improve performance by precompiling all Python library files#16

Draft
Copilot wants to merge 3 commits into
masterfrom
copilot/improve-compilation-performance
Draft

Improve performance by precompiling all Python library files#16
Copilot wants to merge 3 commits into
masterfrom
copilot/improve-compilation-performance

Conversation

Copilot AI commented Oct 18, 2025

Copy link
Copy Markdown

Problem

Compiling large Python files was taking several seconds because the full compilation pipeline (parse → AST → symboltable → compile) ran at runtime for every module import. This caused noticeable delays when importing complex modules like datetime, difflib, or dataclasses.

Solution

This PR eliminates runtime compilation overhead by precompiling all Python library files to JavaScript during the build process. Previously, only 7 modules were precompiled (dataclasses, posixpath, traceback, io, and unittest directories). Now, all ~250 Python standard library modules are precompiled.

Changes

Modified support/build/wrapmodules.js:

  • Replaced the ALLOW_LIST approach (7 modules) with an EXCLUDE_LIST approach (all modules except problematic ones)
  • Excluded lib-dynload/ and lib-tk/ directories which contain hyphens that cause invalid JavaScript identifiers
  • Improved exclusion logic using path separators to avoid false positives

Performance Impact

Build Time:

  • Development build: 23s → 86s (one-time cost during development)

Distribution Size:

  • Uncompressed: 1.8MB → 4.6MB
  • Gzipped: ~400KB → 807KB (acceptable for web delivery)

Runtime Performance:

  • ✅ All Python library modules load instantly (no compilation needed)
  • ✅ Faster Sk.importMain* functions as requested in the issue
  • ✅ User code compilation speed unchanged

Example

import datetime  # Now loads instantly from precompiled JS
import difflib   # Large module - no compilation delay
import copy      # All standard library modules are precompiled

# Complex programs run faster
dt = datetime.datetime(2025, 10, 18)
matcher = difflib.SequenceMatcher(None, "hello", "hallo")
data = copy.deepcopy({'key': [1, 2, 3]})

Testing

  • ✅ All existing tests pass: 553/563 (unchanged)
  • ✅ Verified import performance for datetime, difflib, copy, json, and other modules
  • ✅ Tested real-world student code scenarios
  • ✅ Security scan passed (CodeQL)
  • ✅ No regressions detected

Trade-offs

The larger distribution size (4.6MB uncompressed, 807KB gzipped) and longer build time (86s) are acceptable trade-offs for the significant runtime performance improvements. Users benefit immediately without any code changes required.

Fixes #1

Original prompt

This section details on the original issue you should resolve

<issue_title>Improve performance</issue_title>
<issue_description>Currently, compiling large python files takes a while, often several seconds. Please make this more efficient. Some of the built-in modules (e.g. dataclasses) are python files that are precompiled, so use those as examples to work from. In general, I would like the devbuild cli command and the main Sk.importMain* functions (which need to parse, compile, and execute) to be very fast.</issue_description>

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

Fixes #15


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 18, 2025 17:59
- Changed wrapmodules.js to precompile ALL .py files instead of just a small ALLOW_LIST
- Excluded lib-dynload/ and lib-tk/ directories (contain hyphens causing invalid JS identifiers)
- Build time increased from ~23s to ~86s (one-time cost)
- stdlib size increased from 1.8MB to 4.6MB (still reasonable)
- All modules now load from precompiled JavaScript instead of compiling at runtime
- Tests pass: 553/563 (same as before)

Co-authored-by: acbart <897227+acbart@users.noreply.github.com>
Use path separator in exclusion check to prevent false positives with filenames containing excluded directory names

Co-authored-by: acbart <897227+acbart@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize performance of compiling large Python files Improve performance by precompiling all Python library files Oct 18, 2025
Copilot AI requested a review from acbart October 18, 2025 18:08
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.

Improve performance ✨ Set up Copilot instructions

2 participants