Suzhou-Tongyuan / jnumpy

Writing Python C extensions in Julia within 5 minutes.
MIT License
234 stars 8 forks source link

further improvement to loading time #27

Open thautwarm opened 2 years ago

thautwarm commented 2 years ago

As a Python user, one second loading time for a small project is painful.

I'm testing a very small example as follows:

module mylib
using TyPython
using TyPython.CPython

@export_py function sum_along_indices(array::Vector{Cdouble}, indices::Vector{Int32})::Cdouble
    s = zero(Cdouble)
    for i in indices
        s += array[i + 1]
    end
    return s
end

@export_py function add(a::Int, b::Int)::Int
    a + b
end

function init()
    @export_pymodule _mylib begin
        sum_along_indices = Pyfunc(sum_along_indices)
        add = Pyfunc(add)
    end
end

precompile(init, ())
end

This seems to be the theoretically fastest loading time:

julia> @time begin import Pkg, TyPython; TyPython.CPython.init()
       Pkg.activate("mylib"); import mylib; TyPython.CPython.init();mylib.init()
       end
  Activating project at `C:\Users\TR\Desktop\demo\mylib\mylib`
  0.973526 seconds (1.72 M allocations: 100.042 MiB, 2.27% gc time, 70.02% compilation time)
Py(namespace(sum_along_indices=<built-in function sum_along_indices>, add=<built-in function add>))

So far the extra overhead comes from the following parts, which can be optimized in the future:

  1. calling interpreted julia to get configuration:

    https://github.com/Suzhou-Tongyuan/jnumpy/blob/669e046ca6448e8c06e39d0f98cf9d124a541c11/jnumpy/init.py#L112-L117

  2. calling interpreted julia to get the module name:

    https://github.com/Suzhou-Tongyuan/jnumpy/blob/669e046ca6448e8c06e39d0f98cf9d124a541c11/jnumpy/apis.py#L92

songjhaha commented 2 years ago

Using python's toml to parse a Project.toml would be better here: https://github.com/Suzhou-Tongyuan/jnumpy/blob/58d3a292d266223e5cdc137d42db2b7e8f5189c0/jnumpy/apis.py#L80

It cost 0.1s to get project name while init_project() cost 0.9s on my computer.

thautwarm commented 2 years ago

Fine, we could use toml at this stage.