Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions opt/runtime.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// keep compatibility with old imports
// since this is used by benchmarks,
// its very cheap.

export { createRuntimeOptimizer } from "../src/opt/runtime.js";
15 changes: 14 additions & 1 deletion rust-native/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct DynamicValueSource {
pub enum DynamicValueSourceKind {
Param,
Query,
QueryObject,
Header,
}

Expand Down Expand Up @@ -480,7 +481,7 @@ fn parse_json_object_fields(payload: &str) -> Option<Vec<JsonObjectField>> {
let separator = find_top_level(trimmed, ':')?;
let key = parse_object_key(trimmed[..separator].trim())?;
let value_source = trimmed[separator + 1..].trim();
let value = if let Some(source) = parse_dynamic_value_source(value_source) {
let value = if let Some(source) = parse_dynamic_json_value_source(value_source) {
JsonValueTemplate::Dynamic(source)
} else {
let literal = parse_literal(value_source)?;
Expand Down Expand Up @@ -686,6 +687,18 @@ fn parse_dynamic_value_source(raw: &str) -> Option<DynamicValueSource> {
None
}

fn parse_dynamic_json_value_source(raw: &str) -> Option<DynamicValueSource> {
let source = raw.trim();
if source == "req.query" {
return Some(DynamicValueSource {
kind: DynamicValueSourceKind::QueryObject,
key: Box::<str>::from(""),
});
}

parse_dynamic_value_source(source)
}

fn parse_function_call_string_arg(source: &str, prefix: &str) -> Option<String> {
if !source.starts_with(prefix) || !source.ends_with(')') {
return None;
Expand Down
75 changes: 75 additions & 0 deletions rust-native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ enum ResolvedDynamicValue {
Missing,
Single(String),
Multi(Vec<String>),
RawJson(Vec<u8>),
}

fn build_dynamic_fast_path_response(
Expand Down Expand Up @@ -1334,6 +1335,14 @@ fn render_dynamic_json_body(
output.push(b']');
wrote_field = true;
}
ResolvedDynamicValue::RawJson(raw) => {
if wrote_field {
output.push(b',');
}
output.extend_from_slice(field.key_prefix.as_ref());
output.extend_from_slice(raw.as_slice());
wrote_field = true;
}
}
}
}
Expand Down Expand Up @@ -1373,6 +1382,9 @@ fn render_dynamic_text_body(
output.push_str(value.as_str());
}
}
ResolvedDynamicValue::RawJson(bytes) => {
output.push_str(String::from_utf8_lossy(bytes.as_slice()).as_ref());
}
},
}
}
Expand Down Expand Up @@ -1404,6 +1416,10 @@ fn resolve_dynamic_value(
let entries = query_entries(url, query_cache);
lookup_query_value(entries.as_slice(), source.key.as_ref())
}
DynamicValueSourceKind::QueryObject => {
let entries = query_entries(url, query_cache);
ResolvedDynamicValue::RawJson(serialize_query_object_json(entries.as_slice()))
}
}
}

Expand Down Expand Up @@ -1459,6 +1475,65 @@ fn lookup_query_value(entries: &[(String, String)], key: &str) -> ResolvedDynami
}
}

fn serialize_query_object_json(entries: &[(String, String)]) -> Vec<u8> {
let mut buckets: Vec<(&str, Vec<&str>)> = Vec::new();

for (entry_key, entry_value) in entries.iter() {
if is_dangerous_query_key(entry_key.as_str()) {
continue;
}

if let Some((_, values)) = buckets
.iter_mut()
.find(|(key, _)| *key == entry_key.as_str())
{
values.push(entry_value.as_str());
} else {
buckets.push((entry_key.as_str(), vec![entry_value.as_str()]));
}
}

let mut output = Vec::with_capacity(entries.len() * 24 + 16);
output.push(b'{');

for (index, (key, values)) in buckets.iter().enumerate() {
if index > 0 {
output.push(b',');
}

append_json_string(&mut output, key);
output.push(b':');
if values.len() == 1 {
append_json_string(&mut output, values[0]);
} else {
output.push(b'[');
for (value_index, value) in values.iter().enumerate() {
if value_index > 0 {
output.push(b',');
}
append_json_string(&mut output, value);
}
output.push(b']');
}
}

output.push(b'}');
output
}

fn is_dangerous_query_key(key: &str) -> bool {
matches!(
key,
"__proto__"
| "constructor"
| "prototype"
| "__defineGetter__"
| "__defineSetter__"
| "__lookupGetter__"
| "__lookupSetter__"
)
}

fn append_json_string(output: &mut Vec<u8>, value: &str) {
output.push(b'"');
for ch in value.chars() {
Expand Down
3 changes: 3 additions & 0 deletions src/opt/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HOT_HIT_THRESHOLD = parseInt(process.env.HOT_HIT_THRESHOLD) || 128;
export const STABLE_RESPONSE_THRESHOLD = parseInt(process.env.STABLE_RESPONSE_THRESHOLD) || 32;
export const DEFAULT_NOTIFY_INTERVAL_MS = parseInt(process.env.DEFAULT_NOTIFY_INTERVAL_MS) || 1000;
159 changes: 159 additions & 0 deletions src/opt/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
export function buildRouteEntry(route, middlewares) {
const hasParams = route.path.includes(":");
const hasMiddleware = middlewares.some((middleware) =>
pathPrefixMatches(middleware.pathPrefix, route.path),
);
const source = route.handlerSource ?? "";
const staticFastPath = isStaticFastPathCandidate(route, hasMiddleware, source);
const cacheCandidate =
!staticFastPath &&
route.method === "GET" &&
!hasParams &&
!hasMiddleware &&
!source.includes("await") &&
!/req\.(params|query|body|headers|url|path|method)\b/.test(source) &&
!/Date\.now|new Date|Math\.random|crypto\./.test(source);

const reasons = [];
if (staticFastPath) {
reasons.push("served by static fast path");
} else {
reasons.push("served through bridge dispatch");
}
if (hasMiddleware) {
reasons.push("middleware blocks static promotion");
}
if (hasParams) {
reasons.push("route params require dynamic dispatch");
}
if (cacheCandidate) {
reasons.push("runtime-stable responses can be cached later");
}

return {
handlerId: route.handlerId,
method: route.method,
path: route.path,
label: `${route.method} ${route.path}`,
stage: "cold",
hits: 0,
lastHitAt: null,
staticFastPath,
binaryBridge: true,
dispatchKind: route.dispatchKind ?? "generic_fallback",
jsonFastPath: route.jsonFastPath ?? "fallback",
bridgeObserved: false,
cacheCandidate,
recommendation: null,
reasons,
stableResponses: 0,
lastResponseKey: null,
settled: false,
};
}

function isStaticFastPathCandidate(route, hasMiddleware, source) {
if (route.method !== "GET" || route.path.includes(":") || hasMiddleware) {
return false;
}

if (source.includes("await")) {
return false;
}

const body = trimReturnAndSemicolon(extractFunctionBody(source));
if (!body) {
return false;
}

return (
isDirectLiteralCall(body, "res.json(") ||
isDirectLiteralCall(body, "res.send(") ||
isDirectStatusLiteralCall(body, "json") ||
isDirectStatusLiteralCall(body, "send")
);
}

function extractFunctionBody(source) {
const arrowIndex = source.indexOf("=>");
if (arrowIndex >= 0) {
const right = source.slice(arrowIndex + 2).trim();
if (right.startsWith("{") && right.endsWith("}")) {
return right.slice(1, -1).trim();
}
return right;
}

const blockStart = source.indexOf("{");
const blockEnd = source.lastIndexOf("}");
if (blockStart >= 0 && blockEnd > blockStart) {
return source.slice(blockStart + 1, blockEnd).trim();
}

return source.trim();
}

function trimReturnAndSemicolon(body) {
let value = body.trim();
if (value.startsWith("return ")) {
value = value.slice("return ".length).trim();
}
if (value.endsWith(";")) {
value = value.slice(0, -1).trim();
}
return value;
}

function isDirectLiteralCall(body, prefix) {
if (!body.startsWith(prefix) || !body.endsWith(")")) {
return false;
}

const payload = body.slice(prefix.length, -1).trim();
return looksLiteralPayload(payload);
}

function isDirectStatusLiteralCall(body, method) {
if (!body.startsWith("res.status(") || !body.endsWith(")")) {
return false;
}

const separator = `).${method}(`;
const separatorIndex = body.indexOf(separator);
if (separatorIndex < 0) {
return false;
}

const payload = body.slice(separatorIndex + separator.length, -1).trim();
return looksLiteralPayload(payload);
}

function looksLiteralPayload(payload) {
if (!payload) {
return false;
}

if (
payload.startsWith("{") ||
payload.startsWith("[") ||
payload.startsWith('"') ||
payload.startsWith("'") ||
payload.startsWith("`")
) {
return true;
}

if (/^-?\d/.test(payload)) {
return true;
}

return payload === "true" || payload === "false" || payload === "null";
}

function pathPrefixMatches(pathPrefix, requestPath) {
if (pathPrefix === "/") {
return true;
}

return requestPath === pathPrefix || requestPath.startsWith(`${pathPrefix}/`);
}
Loading
Loading