Giter Club home page Giter Club logo

lumac0 / compiled Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pycompiled/compiled

0.0 0.0 0.0 464.47 MB

Compiled variants of the Python standard library.

Home Page: https://pypi.org/p/compiled

License: Other

Shell 0.12% JavaScript 0.01% C++ 0.71% Python 65.55% C 32.16% Objective-C 0.05% Common Lisp 0.05% Assembly 0.10% PowerShell 0.04% XSLT 0.01% CSS 0.01% Makefile 0.07% HTML 0.41% PLSQL 0.05% DTrace 0.01% Batchfile 0.15% M4 0.44% Roff 0.08% Rich Text Format 0.01% VBScript 0.01%

compiled's Introduction

compiled

Compiled variants of the Python standard library.

What is this project exactly?

Pure Python modules in the standard library can be a speed bottleneck sometimes, this package aims to provide "compiled" variants of the pure Python standard library modules, which are somewhere between 2-4x faster than the builtin ones.

Installation

pip install compiled

✨ This will install the pycompile CLI script as well.

Usage

Say your program asd.py looks like this:

import tomllib
from difflib import get_close_matches

# ... rest of the code

You can use the bundled pycompile script to turn those imports into the "compiled" variants:

$ pycompile asd.pyRewrote asd.py with compiled imports.

$ cat asd.py
import compiled.tomllib as tomllib
from compiled.difflib import get_close_matches

# ... rest of the code

With a real world program using tomllib and difflib, we get the following difference in speed:

Program Pure Python time compiled time Speedup %
hashes.py 1.907 seconds 1.028 seconds 85.5% faster

How does it work?

It uses this awesome piece of software called mypyc which takes type-checked Python code and compiles it to C code, which is generally faster.

Simply making sure that the Python module is statically type checked is 95% of the work needed to be done to make the project work. The rest is just glue code to test the compiled code and package it.

Supported libraries

Library Supported
tomllib
difflib
asyncio ⬜️
urllib ⬜️
zipfile ⬜️
argparse ⬜️
uuid ⬜️

PRs type checking other Python modules are welcome! Check the How to add a library section on the exact steps.

Local Development / Testing

  • Create and activate a virtual environment.

  • Run pip install mypy, as mypy[c] is the only dependency.

  • Scripts to test, build and package standard libraries are present in build.py:

    $ ./build.py test tomllib
    .............
    ----------------------------------------------------------------------
    Ran 13 tests in 0.006s
    OK
    
    $ ./build.py mypy tomllib
    Success: no issues found in 4 source files
    
    $ ./build.py package
    [...]
    ✨Built ./build/dist/compiled-0.2.1-cp311-cp311-macosx_13_0_arm64.whl
  • Run pytest compiled_tests to run tests.

How to add a library

Say you wanted to add the heapq library to the project. The steps will be as follows:

  • verify ./build.py test heapq passes. This will run the CPython test suite on the existing code.

  • Tweak the code and add annotations until ./build.py mypy heapq passes.

    Verify ./build.py test heapq still passes.

  • Tweak the code until ./build.py mypyc heapq passes.

    Verify ./build.py test heapq still passes.

  • Run ./build.py test_compiled heapq to ensure the compiled version still passes all tests.

compiled's People

Contributors

gvanrossum avatar benjaminp avatar birkenfeld avatar freddrake avatar vstinner avatar rhettinger avatar serhiy-storchaka avatar pitrou avatar jackjansen avatar loewis avatar tim-one avatar brettcannon avatar akuchling avatar warsaw avatar bitdancer avatar ezio-melotti avatar mdickinson avatar nnorwitz avatar tiran avatar miss-islington avatar terryjreedy avatar gpshead avatar orsenthil avatar vsajip avatar merwok avatar jeremyhylton avatar 1st1 avatar ned-deily avatar berkerpeksag avatar zooba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.