-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This moves all PyO3 modules into a single binary, reducing the overall memory footprint of Rust modules, allowing multiple modules to share .text sections. Modules are exported via a single `.so` living in `edb/server/_rust_native`. As the module loads, it places the submodules in `sys.modules` directly, allowing consumers to directly import from `edb.server._rust_native._MODULE`. ## Logging As part of this work, the custom per-project logging has been replaced with one single logger integration. This logger avoids asyncio and creates a blocking i/o thread using a unix dgram socket to send messages internally. Logging may happen around Python shutdown which PyO3 doesn't handle gracefully (unhappily failing to acquire the GIL in some cases), so a blocking thread avoids any complexity around that, as well as any async I/O shutdown issues. Log messages that are not enabled (ie: more fine-grained than `info!` in Rust) are filtered out in the Rust log subscriber, and are effectively free.
- Loading branch information
Showing
74 changed files
with
468 additions
and
238 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "rust_native" | ||
version = "0.1.0" | ||
license = "MIT/Apache-2.0" | ||
authors = ["MagicStack Inc. <[email protected]>"] | ||
edition = "2021" | ||
|
||
[lint] | ||
workspace = true | ||
|
||
[features] | ||
python_extension = ["pyo3/extension-module", "pyo3/serde"] | ||
|
||
[dependencies] | ||
pyo3 = { workspace = true } | ||
tokio.workspace = true | ||
pyo3_util.workspace = true | ||
conn_pool = { workspace = true, features = [ "python_extension" ] } | ||
pgrust = { workspace = true, features = [ "python_extension" ] } | ||
http = { workspace = true, features = [ "python_extension" ] } | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
path = "src/lib.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use pyo3::{ | ||
pymodule, | ||
types::{PyAnyMethods, PyModule, PyModuleMethods}, | ||
Bound, PyResult, Python, | ||
}; | ||
use pyo3_util::logging::{get_python_logger_level, initialize_logging_in_thread}; | ||
|
||
const MODULE_PREFIX: &str = "edb.server._rust_native"; | ||
|
||
fn add_child_module( | ||
py: Python, | ||
parent: &Bound<PyModule>, | ||
name: &str, | ||
init_fn: fn(Python, &Bound<PyModule>) -> PyResult<()>, | ||
) -> PyResult<()> { | ||
let full_name = format!("{}.{}", MODULE_PREFIX, name); | ||
let child_module = PyModule::new(py, &full_name)?; | ||
init_fn(py, &child_module)?; | ||
parent.add(name, &child_module)?; | ||
|
||
// Add the child module to the sys.modules dictionary so it can be imported | ||
// by name. | ||
let sys_modules = py.import("sys")?.getattr("modules")?; | ||
sys_modules.set_item(full_name, child_module)?; | ||
Ok(()) | ||
} | ||
|
||
#[pymodule] | ||
fn _rust_native(py: Python, m: &Bound<PyModule>) -> PyResult<()> { | ||
// Initialize any logging in this thread to route to "edb.server" | ||
let level = get_python_logger_level(py, "edb.server")?; | ||
initialize_logging_in_thread("edb.server", level); | ||
|
||
add_child_module(py, m, "_conn_pool", conn_pool::python::_conn_pool)?; | ||
add_child_module(py, m, "_pg_rust", pgrust::python::_pg_rust)?; | ||
add_child_module(py, m, "_http", http::python::_http)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "conn-pool" | ||
name = "conn_pool" | ||
version = "0.1.0" | ||
license = "MIT/Apache-2.0" | ||
authors = ["MagicStack Inc. <[email protected]>"] | ||
|
@@ -15,13 +15,13 @@ optimizer = ["genetic_algorithm", "lru", "rand", "statrs", "anyhow", "tokio/test | |
[dependencies] | ||
pyo3 = { workspace = true, optional = true } | ||
tokio.workspace = true | ||
pyo3_util.workspace = true | ||
tracing.workspace = true | ||
|
||
futures = "0" | ||
scopeguard = "1" | ||
itertools = "0" | ||
thiserror = "1" | ||
tracing = "0" | ||
tracing-subscriber = "0" | ||
strum = { version = "0.26", features = ["derive"] } | ||
consume_on_drop = "0" | ||
smart-default = "0" | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.