惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

风雪之隅

深入理解PHP7内核之OBJECT - 风雪之隅 PHP 8新特性之Attributes(注解) - 风雪之隅 博客迁移到腾讯云 - 风雪之隅 在Qcon 2015 北京上的演讲PPT - PHP7 Yar-2.1 新功能介绍 - 风雪之隅 Yaf and Phalcon, which is faster? HTTPOXY漏洞说明 - 风雪之隅 一个关于Zend O+的小分享 - 风雪之隅 在PHP中使用协程实现多任务调度 - 风雪之隅 Curl的毫秒超时的一个"Bug" - 风雪之隅 关于PHP,关于Realsee - 风雪之隅 PHP8新特性之match表达式 - 风雪之隅 PHP 8新特性之JIT简介 - 风雪之隅 使用SSE2指令高效实现strtolower - 风雪之隅 使用OSC52实现iTerm2远程pbcopy - 风雪之隅 Yaf 3.2 发布 - 风雪之隅 使用PHP Socket开发Yar TCP服务 - 风雪之隅 Yac 2.1 升级说明 - 风雪之隅 Yaf-3.1 10%性能提升版 - 风雪之隅 Yaconf-1.1 40%速度提升版 - 风雪之隅 PHP FFI详解 - 一种全新的PHP扩展方式 - 风雪之隅 使用SSE2指令集加速字符替换 - 风雪之隅 Yaf_Loader重构测试 - 风雪之隅 PHP_INT_MIN 和 -9223372036854775808 - 风雪之隅 深入理解PHP7内核之FAST_ZPP - 风雪之隅 深入理解PHP7内核之HashTable - 风雪之隅 var_dump(1...9)输出什么? - 风雪之隅 使用内存硬盘(tmpfs)来加速你的网站 - 风雪之隅 print不是函数 - 风雪之隅 令人困惑的strtotime - 风雪之隅 深入理解PHP7内核之Reference - 风雪之隅 深入理解PHP7内核之zval - 风雪之隅 PHP的性能演进(从PHP5.0到PHP7.1的性能全评测) - 风雪之隅 让PHP7达到最高性能的几个Tips - 风雪之隅 写在PHP7发布之际的一些话 - 风雪之隅 让你的PHP7更快之Hugepage - 风雪之隅 让你的PHP7更快(GCC PGO) - 风雪之隅 Yaconf - 一个高性能的配置管理扩展 - 风雪之隅 记录一场没有胜利的局部战斗 - 风雪之隅 PHP7 VS HHVM (Wordpress) - 风雪之隅 GCC优化引起的一个"问题" - 风雪之隅 Weibo LAMP演变 - 6月在上海分享的PPT - 风雪之隅 一个小玩意PHP-Valgrind的介绍 - 风雪之隅 PHP浮点数的一个常见问题的解答 - 风雪之隅 Yac (Yet Another Cache) - 无锁共享内存Cache PDOStatement::bindParam的一个陷阱 - 风雪之隅 Mcrypt响应慢的一个原因 - 风雪之隅 一个程序员眼中的价值 - 风雪之隅 一个关于if else容易迷惑的问题 - 风雪之隅
PHP8.0的Named Parameter - 风雪之隅
laruence · 2022-05-10 · via 风雪之隅

年前花了点时间,对Yar的性能做了一些做了一些提升,但是也遇到一个让我有点不舒服的当初没有良好设计遗留的问题,就是在并行调用RPC的时候,现在的方法原型是:

public static Yar_Concurrent_Client::call(string $uri, string $method, ?array $arguments = NULL, ?callable $callback = NULL, ?callable $error_callback = NULL, ?array $options = NULL):null|int|bool {}

是不是一看就很头大?

因为在实际的使用过程中,很有可能回调函数和错误回调函数是空的,因为可以真正发起调用的时候,也就是在loop重全局指定:

Yar_Concurrent_Client::loop(?callable $callback = NULL, ?callable $error_callback = NULL, ?array $options = NULL):?bool {}

而很多时候$options是有用的,根据调用相关,所以就导致,实际的使用的时候,大量的并行调用的代码会在参数中写很多的NULL, 类似:

Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val1"));

Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val2"));

Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val2"));

Yar_Concurrent_Clinet::call("https://xxx.com/api", "method", array("arguments"), NULL, NULL, array(YAR_OPT_HEADER=>array("header:val4"));

于是我一直想如何能让这样的调用更优雅一些,曾经一度我想使用多态,或者新增一个API,类似:

public static Yar_Concurrent_Client::callArray(array $arguments):null|int|bool {}

但强迫症让我觉得这样做,遗祸无穷, 今天早上我突然想起以前曾经看到过的一个RFC,于是找了半天,发现早在PHP5.6的时候,就commit了, 但反正我比较老派,新特性研究的少,也是没怎么用过,就不知道大家是否会用过了。 🙂

就是今天要介绍的第一个特性:Argument unpacking。

Argument unpacking

我们知道PHP支持可变参数,也就是variadic function. 比如对于如下的函数定义:

function variadic(...$arguments) {
    var_dump($arguments);
}

注意参数的定义,使用了三个点…(ellipsis符号), 意思就是无论你在调用这个函数的时候传递了多少个参数,这些参数都会被打包成一个名字为$arguments的数组:

variadic();
//output: array(0) { }
variadic(NULL);
//output: array(1) { [0]=> NULL }
variadic("foo", "bar");
//output: array(2) { [0]=> string(3) "foo"  [1]=> string(3) "bar" }
variadic(NULL, array(), "dummy");
//output: array(3) { [0]=> NULL [1]=>[] [2]=> string(5) "dummy" }

当然,这个不是我们今天要用到的,这个特性还有一个对应的在调用时刻使用的兄弟形式,叫做argument unpacking:

比如,类似上面我的那个问题,我们定义了一个函数

function dummy($a, $b = NULL, $c = NULL, $d = NULL, $e = NULL) {
    var_dump($a, $b, $c, $d, $e);
}

如果大部分情况下我们的参数b, c, d都是NULL, 但是e可能需要传递,那我们就可以使用argument unpacking来避免代码中大量的NULL参数,类似:

$arguments = array(
    "First argument",
    NULL,  NULL,  NULL,
    "Fifth argument",
);

dummy(...$arguments);

//output:
// string(14) "First argument"
// NULL
// NULL
// NULL
// string(14) "Fifth argument"

注意在调用的时候,我也使用了…,这里的意思就是,把…后面的数组解开,按照顺序分别依次传递给被调用的函数,第一个元素对应第一个参数, 第二个对应第二个。

但是注意,这里的位置是跟填充位置相关的,跟索引无关,也就是说:

$arguments = array(
    4=> "First argument",
    0=> "Fifth argument"
),

这样的形式, 索引4依然是被认为是第一个参数。

想到这个以后,我就突然发现,我不需要给Yar引入新东西了,最开的例子就可以变成:

$arguments = array(
    "https://xxx.com/api",
    "method",
    array("arguments"),
    NULL, NULL, 
    "options" => array(YAR_OPT_HEADER => array("header:val1")
)
Yar_Concurrent_Clinet::call(...$arguments);

$arguments["options"][YAR_OPT_HADER] = ["header:val2"];
Yar_Concurrent_Clinet::call(...$arguments);

$arguments["options"][YAR_OPT_HADER] = ["header:val3"];
Yar_Concurrent_Clinet::call(...$arguments);

$arguments["options"][YAR_OPT_HADER] = ["header:val4"];
Yar_Concurrent_Clinet::call(...$arguments);

你以为这就完了么?

考虑到如上的代码,还是有一个问题,就是需要构造一个中间数组,对于强迫症的我们来说,总还是觉得会有点,那啥…

但其实我们还可以利用PHP8.0中引入的另外一个RFC, Named parameter:

Named Parameter

在PHP8.0以后,容许用户在传递参数的时候,指定参数名字, 比如还是对于上面的例子函数:

function dummy($a, $b = NULL, $c = NULL, $d = NULL, $e = NULL) {
    var_dump($a, $b, $c, $d, $e);
}

现在我们可以在调用的时候,指定要传递的参数名字,比如:

dummy(a:"dummy", e:"foo");
//output:
// string(5) "dummy"
// NULL
// NULL
// NULL
// string(3) "foo"

也就是说,我指定了传递给a和e参数,没有指定的就是默认缺省值,你甚至可以不按声明顺序来,比如:

dummy(e:"foo", a:"dummy");

输出结果也是一样的。

这样以来,开头的代码就可以变成:

Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val1")));
Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val2")));
Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val3")));
Yar_Concurrent_Client::call("https://xxx.com/api", "method", arguments:array("arguments"), options:array(YAR_OPT_HEADER=>array("header:val4")));

你就可以在调用的时候,想传那个传那个,想怎么传就怎么传

虽然代码相比argument unpacking还是有点长,但是既解决了不用写那么多NULL,又不会引入新的中间变量。

于是,问题完美解决,我也不用引入新的API了 :)