GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(derive): fill the partial through &mut instead of returning it (… · jdx/usage@3399281

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • derive/src
    • codegen.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎derive/src/codegen.rs‎

Copy file name to clipboardExpand all lines: derive/src/codegen.rs
+38-6Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ pub fn emit(cli: &Cli) -> TokenStream {
207207
// would sit in the layer that outranks every other, named after a flag nobody
208208
// typed — and counted twice by a `union` setting whose CLI also has an
209209
// `EnvLayer`. The environment has a layer of its own to arrive in.
210-
let mut partial = read_argv(Self::command(), argv)?;
210+
#defaults
211+
read_argv_into(Self::command(), argv, &mut partial)?;
211212
let __usage_settings = settings_layer(&partial);
212213
check(&mut partial)?;
213214
let __usage_built = Self {
@@ -336,7 +337,24 @@ pub fn emit(cli: &Cli) -> TokenStream {
336337
argv: &'v [&'v ::std::ffi::OsStr],
337338
) -> ::std::result::Result<Partial, usage_argv::Error<'static, 'v>> {
338339
#defaults
340+
read_argv_into(command, argv, &mut partial)?;
341+
::std::result::Result::Ok(partial)
342+
}
339343

344+
/// `read_argv`, filling a partial the caller already owns.
345+
///
346+
/// The entry points call this rather than `read_argv`, because a `Partial` is
347+
/// the whole CLI's worth of accumulator — every command's fields, inlined —
348+
/// and returning one by value is a memcpy of all of it. At mise's scale that is
349+
/// 11KB a move, and returning it up the chain from here to `parse_from` made
350+
/// four: 87% of a parse was spent copying a struct of which all but one
351+
/// command's worth goes untouched. Threading `&mut` puts the partial in the
352+
/// frame that will consume it, leaving only the one copy that builds it.
353+
pub fn read_argv_into<'v>(
354+
command: &'static usage_argv::Command<'static>,
355+
argv: &'v [&'v ::std::ffi::OsStr],
356+
partial: &mut Partial,
357+
) -> ::std::result::Result<(), usage_argv::Error<'static, 'v>> {
340358
let mut __usage_parser = usage_argv::Parser::new(command, argv);
341359
while let ::std::option::Option::Some(__usage_event) =
342360
__usage_parser.next_event()
@@ -365,10 +383,10 @@ pub fn emit(cli: &Cli) -> TokenStream {
365383
// `apply` handles this command's own fields and routes anything
366384
// else into its subcommands, which is why a nested command needs
367385
// nothing extra here.
368-
apply(&mut partial, &__usage_event);
386+
apply(partial, &__usage_event);
369387
}
370388

371-
::std::result::Result::Ok(partial)
389+
::std::result::Result::Ok(())
372390
}
373391

374392
/// Every token read, and everything decided after the last one.
@@ -381,11 +399,22 @@ pub fn emit(cli: &Cli) -> TokenStream {
381399
command: &'static usage_argv::Command<'static>,
382400
argv: &'v [&'v ::std::ffi::OsStr],
383401
) -> ::std::result::Result<Partial, usage_argv::Error<'static, 'v>> {
384-
let mut partial = read_argv(command, argv)?;
385-
check(&mut partial)?;
402+
#defaults
403+
read_into(command, argv, &mut partial)?;
386404
::std::result::Result::Ok(partial)
387405
}
388406

407+
/// `read`, filling a partial the caller already owns. See `read_argv_into`.
408+
pub fn read_into<'v>(
409+
command: &'static usage_argv::Command<'static>,
410+
argv: &'v [&'v ::std::ffi::OsStr],
411+
partial: &mut Partial,
412+
) -> ::std::result::Result<(), usage_argv::Error<'static, 'v>> {
413+
read_argv_into(command, argv, partial)?;
414+
check(partial)?;
415+
::std::result::Result::Ok(())
416+
}
417+
389418
#settings_given
390419
#settings_bindings
391420
#settings_layer
@@ -430,7 +459,10 @@ pub fn emit(cli: &Cli) -> TokenStream {
430459
pub fn parse_from<'v>(
431460
argv: &'v [&'v ::std::ffi::OsStr],
432461
) -> ::std::result::Result<Self, usage_argv::Error<'static, 'v>> {
433-
let partial = read(Self::command(), argv)?;
462+
// The partial is built here and filled through `&mut`, rather than
463+
// returned up the chain: see `read_argv_into`.
464+
#defaults
465+
read_into(Self::command(), argv, &mut partial)?;
434466
::std::result::Result::Ok(Self {
435467
#sub_build
436468
#(#field_finals),*

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.