Project

General

Profile

Actions

Feature #22134

open

Faster rb_scan_args() for keyword args (optimization)

Feature #22134: Faster rb_scan_args() for keyword args (optimization)

Added by luke-gru (Luke Gruber) 6 days ago. Updated 6 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:125877]

Description

Motivation

When using the rb_scan_args() API, often we want to find a value for a given keyword argument. In order to do this, we call rb_scan_args() like so:

VALUE str;
VALUE kwargs;
VALUE example;
rb_scan_args(argc, argv, "1:", &str, &kwargs); // duplicates the kwargs hash in argv
if (!NIL_P(kwargs)) rb_get_kwargs(kwargs, &id_example, 0, 1, &example); // mutates the duplicated kwargs hash to retrieve `example:`

This duplicates the keyword args hash given in argv. It would be nice to be able to grab a direct reference to the keyword hash
and to have a variant of rb_get_kwargs() that didn't mutate the passed in hash.

Proposal

Add a new valid format character for rb_scan_args():

VALUE str;
VALUE kwargs;
VALUE example;
rb_scan_args(argc, argv, "1:^", &str, &kwargs); // access kwargs directly from argv
if (!NIL_P(kwargs)) rb_get_kwargs_const(kwargs, &id_example, 0, 1, &example); // don't mutate the passed kwargs hash

This '^' character would only be valid after a ":'.

I have a pull request available for anyone that is interested.

Thank you!

Actions

Also available in: PDF Atom