Skip to content

Commit fb34e6a

Browse files
committed
introduce LruCache for transpilation
1 parent 3994cb8 commit fb34e6a

File tree

8 files changed

+114
-58
lines changed

8 files changed

+114
-58
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cubejs-backend-native/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ tokio = { version = "1", features = ["full", "rt"] }
4848
uuid = { version = "1", features = ["v4"] }
4949
bytes = "1.5.0"
5050
regex = "1.10.2"
51+
lru = "0.13.0"
5152

5253
[dependencies.neon]
5354
version = "=1"

packages/cubejs-backend-native/src/transpilers.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ use crate::node_obj_deserializer::JsValueDeserializer;
22
use crate::node_obj_serializer::NodeObjSerializer;
33
use anyhow::anyhow;
44
use cubetranspilers::{run_transpilers, TransformConfig, Transpilers};
5+
use lru::LruCache;
56
use neon::context::{Context, FunctionContext, ModuleContext};
67
use neon::prelude::{JsPromise, JsResult, JsValue, NeonResult};
78
use neon::types::JsString;
89
use serde::Deserialize;
910
use std::collections::{HashMap, HashSet};
10-
use std::sync::{LazyLock, RwLock};
11+
use std::env;
12+
use std::num::NonZeroUsize;
13+
use std::sync::{LazyLock, Mutex};
1114

1215
#[derive(Deserialize, Default, Clone, Debug)]
1316
#[serde(rename_all = "camelCase")]
@@ -26,8 +29,21 @@ pub struct TransformRequestConfig {
2629
pub meta_data: Option<TransformMetaData>,
2730
}
2831

29-
static METADATA_CACHE: LazyLock<RwLock<HashMap<String, TransformMetaData>>> =
30-
LazyLock::new(|| RwLock::new(HashMap::new()));
32+
/// It should be equal or more then number of internal libuv threads used by Neon
33+
/// By 01.2025 it defaults to 4. But maybe changed via `UV_THREADPOOL_SIZE` env var.
34+
/// `CUBEJS_TRANSPILER_METADATA_CACHE_SIZE` env var is provided for fine tuning.
35+
/// @see https://docs.libuv.org/en/v1.x/threadpool.html
36+
/// @see https://nodejs.org/api/cli.html#cli_uv_threadpool_size_size
37+
static DEFAULT_CACHE_SIZE: usize = 16;
38+
39+
static METADATA_CACHE: LazyLock<Mutex<LruCache<String, TransformMetaData>>> = LazyLock::new(|| {
40+
let cache_size = env::var("CUBEJS_TRANSPILER_METADATA_CACHE_SIZE")
41+
.ok()
42+
.and_then(|s| s.parse::<usize>().ok())
43+
.and_then(NonZeroUsize::new)
44+
.unwrap_or(NonZeroUsize::new(DEFAULT_CACHE_SIZE).unwrap());
45+
Mutex::new(LruCache::new(cache_size))
46+
});
3147

3248
pub fn register_module(cx: &mut ModuleContext) -> NeonResult<()> {
3349
cx.export_function("transpileJs", transpile_js)?;
@@ -46,7 +62,7 @@ pub fn transpile_js(mut cx: FunctionContext) -> JsResult<JsPromise> {
4662
let transform_config: TransformConfig = match transform_request_config {
4763
Ok(data) => match data.meta_data {
4864
Some(meta_data) => {
49-
let mut config_lock = METADATA_CACHE.write().unwrap();
65+
let mut config_lock = METADATA_CACHE.lock().unwrap();
5066
let cache = TransformMetaData {
5167
cube_names: meta_data.cube_names,
5268
cube_symbols: meta_data.cube_symbols,
@@ -59,13 +75,13 @@ pub fn transpile_js(mut cx: FunctionContext) -> JsResult<JsPromise> {
5975
cube_symbols: cache.cube_symbols.clone(),
6076
context_symbols: cache.context_symbols.clone(),
6177
};
62-
config_lock.insert(data.compiler_id.clone(), cache);
78+
config_lock.put(data.compiler_id.clone(), cache);
6379
cfg
6480
}
6581
None => {
66-
let cache = METADATA_CACHE.read().unwrap();
82+
let mut config_lock = METADATA_CACHE.lock().unwrap();
6783

68-
match cache.get(&data.compiler_id) {
84+
match config_lock.get(&data.compiler_id) {
6985
Some(cached) => TransformConfig {
7086
file_name: data.file_name,
7187
transpilers: data.transpilers,

rust/cubenativeutils/Cargo.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/Cargo.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ homepage = "https://cube.dev"
1010

1111
[dependencies]
1212
arc-swap = "1"
13-
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "dcf3e4aa26fd112043ef26fa4a78db5dbd443c86", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
13+
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "dcf3e4aa26fd112043ef26fa4a78db5dbd443c86", default-features = false, features = [
14+
"regex_expressions",
15+
"unicode_expressions",
16+
] }
1417
thiserror = "2"
1518
cubeclient = { path = "../cubeclient" }
1619
pg-srv = { path = "../pg-srv" }
@@ -25,7 +28,7 @@ futures = "0.3.23"
2528
rand = "0.8.3"
2629
hashbrown = "0.14.3"
2730
log = "0.4.21"
28-
rust_decimal = { version = "1.25", features = ["c-repr", "db-postgres"]}
31+
rust_decimal = { version = "1.25", features = ["c-repr", "db-postgres"] }
2932
postgres-types = "0.2.3"
3033
# Locked, because starting from 1.15 this crate switch from chrono to time
3134
# which panic with Could not determine the UTC offset on this system.
@@ -38,10 +41,12 @@ uuid = { version = "1", features = ["serde", "v4"] }
3841
bincode = "1.3.1"
3942
chrono = "0.4.31"
4043
chrono-tz = "0.6"
41-
tokio-util = { version = "0.7", features=["compat"] }
44+
tokio-util = { version = "0.7", features = ["compat"] }
4245
comfy-table = "7.1.0"
4346
bitflags = "1.3.2"
44-
egg = { rev = "952f8c2a1033e5da097d23c523b0d8e392eb532b", git = "https://github.com/cube-js/egg.git", features = ["serde-1"] }
47+
egg = { rev = "952f8c2a1033e5da097d23c523b0d8e392eb532b", git = "https://github.com/cube-js/egg.git", features = [
48+
"serde-1",
49+
] }
4550
paste = "1.0.6"
4651
tracing = { version = "0.1.40", features = ["async-await"] }
4752
async-stream = "0.3.3"
@@ -50,7 +55,7 @@ futures-util = "0.3.23"
5055
sha1_smol = "1.0.0"
5156
tera = { version = "1", default-features = false }
5257
minijinja = { version = "1", features = ["json", "loader"] }
53-
lru = "0.12.1"
58+
lru = "0.13.0"
5459
sha2 = "0.10.8"
5560
bigdecimal = "0.4.2"
5661
indexmap = "1.9.3"
@@ -60,7 +65,10 @@ indexmap = "1.9.3"
6065
pretty_assertions = "1.0.0"
6166
insta = "1.12"
6267
portpicker = "0.1.1"
63-
tokio-postgres = { version = "0.7.7", features = ["with-chrono-0_4", "runtime"] }
68+
tokio-postgres = { version = "0.7.7", features = [
69+
"with-chrono-0_4",
70+
"runtime",
71+
] }
6472
rust_decimal = { version = "1.23", features = ["db-tokio-postgres"] }
6573
pg_interval = "0.4.1"
6674
criterion = { version = "0.4.0", features = ["html_reports"] }

rust/cubesqlplanner/Cargo.lock

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yarn.lock

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5306,18 +5306,6 @@
53065306
uuid "9.0.0"
53075307
winston "3.8.2"
53085308

5309-
"@cubejs-backend/[email protected]":
5310-
version "1.2.4"
5311-
resolved "https://registry.yarnpkg.com/@cubejs-backend/linter/-/linter-1.2.4.tgz#b38551ada107d94a3a12bb3f82e582ef6e184096"
5312-
integrity sha512-V150xWxBgRptCO4GAdZTuCwY7+FoxlJ1rBYM379LLEX8sLFC4NG0AN8S3vy0YSaUaC3P07ne3CrEILIJLt0JAg==
5313-
dependencies:
5314-
"@typescript-eslint/eslint-plugin" "^6.12.0"
5315-
"@typescript-eslint/parser" "^6.12.0"
5316-
eslint "^8.54.0"
5317-
eslint-config-airbnb-base "^14.2.1"
5318-
eslint-plugin-import "^2.22.1"
5319-
eslint-plugin-node "^9.2.0"
5320-
53215309
"@cubejs-backend/[email protected]":
53225310
version "0.33.20"
53235311
resolved "https://registry.yarnpkg.com/@cubejs-backend/shared/-/shared-0.33.20.tgz#3d9fa60041599cca9fe4c04df05daa4b8ab8675f"
@@ -5338,26 +5326,6 @@
53385326
throttle-debounce "^3.0.1"
53395327
uuid "^8.3.2"
53405328

5341-
"@cubejs-backend/[email protected]":
5342-
version "1.2.4"
5343-
resolved "https://registry.yarnpkg.com/@cubejs-backend/shared/-/shared-1.2.4.tgz#dcdc8a0537cfe9df97f3502bea983204634fbdfd"
5344-
integrity sha512-C/BA0ogCRhhtYwYyQkhXTTKLIUtJ1020aybbQ9tbKtlkrd3ab+a166jmtlAD5kDeoIhB08DgMTQjaQGDYh3gBg==
5345-
dependencies:
5346-
"@oclif/color" "^0.1.2"
5347-
bytes "^3.1.0"
5348-
cli-progress "^3.9.0"
5349-
cross-spawn "^7.0.3"
5350-
decompress "^4.2.1"
5351-
env-var "^6.3.0"
5352-
fs-extra "^9.1.0"
5353-
http-proxy-agent "^4.0.1"
5354-
moment-range "^4.0.1"
5355-
moment-timezone "^0.5.46"
5356-
node-fetch "^2.6.1"
5357-
shelljs "^0.8.5"
5358-
throttle-debounce "^3.0.1"
5359-
uuid "^8.3.2"
5360-
53615329
"@cubejs-infra/post-installer@^0.0.7":
53625330
version "0.0.7"
53635331
resolved "https://registry.yarnpkg.com/@cubejs-infra/post-installer/-/post-installer-0.0.7.tgz#a28d2d03e5b7b69a64020d75194a7078cf911d2d"

0 commit comments

Comments
 (0)