Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions docs/.vitepress/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<MonacoEditor
:value="props.modelValue"
@update:value="(val: string) => $emit('update:modelValue', val)"
:language="lang ?? 'zapConfig'"
:language="lang ?? 'zapConfig'"
:theme="`${props.isCodeBlock ? 'codeblock' : 'tab'}-${isDark ? 'dark' : 'light'}`"
@beforeMount="beforeMount"
@mount="(editor) => $emit('mounted', editor)"
:options="EDITOR_OPTIONS"
/>
</template>
/>
</template>

<script setup lang="ts">
import MonacoEditor from "@guolao/vue-monaco-editor";
Expand Down Expand Up @@ -110,7 +110,7 @@ const beforeMount = (monaco: Monaco) => {

const Calls = ["SingleSync", "SingleAsync", "ManySync", "ManyAsync", "Polling"] as const;

const Options = ["write_checks", "typescript", "typescript_max_tuple_length", "typescript_enum", "manual_event_loop", "remote_scope", "remote_folder", "server_output", "client_output", "casing", "yield_type", "async_lib", "tooling", "tooling_output", "tooling_show_internal_data", "disable_fire_all", "types_output", "call_default"] as const;
const Options = ["write_checks", "typescript", "typescript_max_tuple_length", "typescript_enum", "manual_event_loop", "include_profile_labels", "remote_scope", "remote_folder", "server_output", "client_output", "casing", "yield_type", "async_lib", "tooling", "tooling_output", "tooling_show_internal_data", "disable_fire_all", "types_output", "call_default"] as const;

const TypeScriptEnum = ["StringLiteral", "ConstEnum", "StringConstEnum"].map((value) => `"${value}"`)
const Casing = ["PascalCase", "camelCase", "snake_case"].map((value) => `"${value}"`);
Expand Down Expand Up @@ -167,6 +167,7 @@ const beforeMount = (monaco: Monaco) => {

write_checks: Operators,
manual_event_loop: Operators,
include_profile_labels: Operators,

remote_scope: [],
remote_folder: [],
Expand Down
17 changes: 17 additions & 0 deletions docs/config/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ Note that Zap uses `RunService.Heartbeat` and a 61 hz rate by default.

<CodeBlock code="opt manual_event_loop = true" />

## `include_profile_labels`

When enabled, microprofiler lables are added to the generated file to diagnose performance issues.

### Default

`false`

### Options

- `true`
- `false`

### Example

<CodeBlock code="opt include_profile_labels = true" />

## `typescript_max_tuple_length`

The maximum non-nested length of tuples Zap can generate, with anything longer generating an array.
Expand Down
1 change: 1 addition & 0 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Config<'src> {

pub write_checks: bool,
pub manual_event_loop: bool,
pub include_profile_labels: bool,

pub remote_scope: &'src str,
pub remote_folder: &'src str,
Expand Down
87 changes: 87 additions & 0 deletions zap/src/output/luau/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ impl<'src> ClientOutput<'src> {
}

fn push_event_loop_body(&mut self) {
if self.config.include_profile_labels {
self.push_line("debug.profilebegin(\"Zap Send Events\")");
}

self.push_line("if outgoing_used ~= 0 then");
self.indent();
self.push_line("local buff = buffer.create(outgoing_used)");
Expand All @@ -220,6 +224,11 @@ impl<'src> ClientOutput<'src> {
self.push_line("table.clear(outgoing_inst)");
self.dedent();
self.push_line("end");

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

self.dedent();
}

Expand All @@ -241,6 +250,11 @@ impl<'src> ClientOutput<'src> {
fn push_reliable_header(&mut self) {
self.push_line("reliable.OnClientEvent:Connect(function(buff, inst)");
self.indent();

if self.config.include_profile_labels {
self.push_line("debug.profilebegin(\"Zap Reliable OnClientEvent\")");
}

self.push_line("incoming_buff = buff");
self.push_line("incoming_inst = inst");
self.push_line("incoming_read = 0");
Expand Down Expand Up @@ -380,6 +394,10 @@ impl<'src> ClientOutput<'src> {

self.indent();

if self.config.include_profile_labels {
self.push_line(&format!("debug.profilebegin(\"{} Deserialize\")", ev.name));
}

let values = self.get_values(ev.data.len());

self.push_line(&format!("local {values}"));
Expand All @@ -395,6 +413,10 @@ impl<'src> ClientOutput<'src> {
self.push_stmts(statements);
}

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

match ev.call {
EvCall::SingleSync | EvCall::SingleAsync => self.push_line(&format!("if reliable_events[{id}] then")),
EvCall::ManySync | EvCall::ManyAsync => self.push_line(&format!("if reliable_events[{id}][1] then")),
Expand Down Expand Up @@ -483,6 +505,10 @@ impl<'src> ClientOutput<'src> {

self.indent();

if self.config.include_profile_labels {
self.push_line(&format!("debug.profilebegin(\"{} Deserialize\")", fndecl.name));
}

self.push_line("local call_id = buffer.readu8(incoming_buff, read(1))");

let values = self.get_values(fndecl.rets.as_ref().map_or(0, |x| x.len()));
Expand All @@ -500,6 +526,10 @@ impl<'src> ClientOutput<'src> {
self.push_stmts(statements);
}

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

self.push_line(&format!("local thread = reliable_event_queue[{client_id}][call_id]"));
self.push_line(
"-- When using actors it's possible for multiple Zap clients to exist, but only one called the Zap remote function.",
Expand Down Expand Up @@ -547,6 +577,11 @@ impl<'src> ClientOutput<'src> {
self.push_line("end");
self.dedent();
self.push_line("end");

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

self.dedent();
self.push_line("end)");
}
Expand Down Expand Up @@ -588,6 +623,11 @@ impl<'src> ClientOutput<'src> {
id + 1
));
self.indent();

if self.config.include_profile_labels {
self.push_line(&format!("debug.profilebegin(\"Zap {} OnClientEvent\")", ev.name));
}

self.push_line("incoming_buff = buff");
self.push_line("incoming_inst = inst");
self.push_line("incoming_read = 0");
Expand Down Expand Up @@ -692,6 +732,10 @@ impl<'src> ClientOutput<'src> {
self.push_line("end");
}

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

self.dedent();
self.push_line("end)");
}
Expand Down Expand Up @@ -815,6 +859,10 @@ impl<'src> ClientOutput<'src> {
self.push(")\n");
self.indent();

if self.config.include_profile_labels {
self.push_line(&format!("debug.profilebegin(\"{} Fire\")", ev.name));
}

if let EvType::Unreliable(ordered) = ev.evty {
let id = ev.id;
if ordered {
Expand Down Expand Up @@ -854,6 +902,10 @@ impl<'src> ClientOutput<'src> {
self.push_line("load(saved)");
}

if self.config.include_profile_labels {
self.push_line("debug.profileend()");
}

self.dedent();
self.push_line("end,");
}
Expand Down Expand Up @@ -882,6 +934,13 @@ impl<'src> ClientOutput<'src> {
self.push(") -> ()): () -> ()\n");
self.indent();

if self.config.include_profile_labels {
self.push_line(&format!(
"callback = profiledCallback(\"{} Callback\", callback)",
ev.name
));
}

self.push_line(&format!("{}[{id}] = {callback}", events_table_name(ev)));

let event_queue_name = event_queue_table_name(ev);
Expand Down Expand Up @@ -950,6 +1009,13 @@ impl<'src> ClientOutput<'src> {
self.push(") -> ())\n");
self.indent();

if self.config.include_profile_labels {
self.push_line(&format!(
"callback = profiledCallback(\"{} Callback\", callback)",
ev.name
));
}

let events_table_name = events_table_name(ev);
let event_queue_name = event_queue_table_name(ev);

Expand Down Expand Up @@ -1203,6 +1269,10 @@ impl<'src> ClientOutput<'src> {
this.push("\n");
this.indent();

if self.config.include_profile_labels {
this.push_line(&format!("debug.profilebegin(\"{} Call\")", fndecl.name));
}

this.push_line("function_call_id += 1");

this.push_line("function_call_id %= 256");
Expand Down Expand Up @@ -1239,9 +1309,18 @@ impl<'src> ClientOutput<'src> {
this.push_line(&format!(
"reliable_event_queue[{client_id}][function_call_id] = coroutine.running()"
));

if self.config.include_profile_labels {
this.push_line("debug.profileend()");
}

this.push_line("return coroutine.yield()");
}
YieldType::Future => {
if self.config.include_profile_labels {
this.push_line("debug.profileend()");
}

this.push_line("return Future.new(function()");
this.indent();

Expand All @@ -1254,6 +1333,10 @@ impl<'src> ClientOutput<'src> {
this.push_line("end)");
}
YieldType::Promise => {
if self.config.include_profile_labels {
this.push_line("debug.profileend()");
}

this.push_line("return Promise.new(function(resolve)");
this.indent();

Expand Down Expand Up @@ -1367,6 +1450,10 @@ impl<'src> ClientOutput<'src> {

self.push_polling();

if self.config.include_profile_labels {
self.push_profiled_callback();
}

self.push_return();

self.buf
Expand Down
15 changes: 15 additions & 0 deletions zap/src/output/luau/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ pub trait Output<'src>: ConfigProvider<'src> {
));
self.push("\n");
}

fn push_profiled_callback(&mut self) {
self.push_line("local function profiledCallback(label, callback)");
self.indent();
self.push_line("return function(...)");
self.indent();
self.push_line("debug.profilebegin(label)");
self.push_line("callback(...)");
self.push_line("debug.profileend()");
self.dedent();
self.push_line("end");
self.dedent();
self.push_line("end");
self.push("\n");
}
}

fn events_table_name<'a>(evdecl: &EvDecl) -> &'a str {
Expand Down
Loading
Loading