Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 5341a5d

Browse files
committed
Add StartupData::warmUpSnapshotDataBlob(), closes #59
1 parent 9303a47 commit 5341a5d

4 files changed

+96
-4
lines changed

src/php_v8_startup_data.cc

+37-4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ static PHP_METHOD(StartupData, createFromSource) {
140140
php_v8_startup_data_create(return_value, startup_blob);
141141
}
142142

143+
static PHP_METHOD(StartupData, warmUpSnapshotDataBlob) {
144+
zval *php_cold_data_zv;
145+
zend_string *source = NULL;
146+
147+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "oS", &php_cold_data_zv, &source) == FAILURE) {
148+
return;
149+
}
150+
151+
PHP_V8_STARTUP_DATA_FETCH_INTO(php_cold_data_zv, php_v8_cold_data);
152+
153+
/* we can't try-catch here while we have no isolate yet */
154+
155+
const char *warmup_source = ZSTR_VAL(source);
156+
php_v8_init();
157+
158+
v8::StartupData *warm_data = new v8::StartupData();
159+
160+
*warm_data = v8::V8::WarmUpSnapshotDataBlob(*php_v8_cold_data->blob->data(), warmup_source);
161+
162+
if (warm_data->data == NULL) {
163+
PHP_V8_THROW_EXCEPTION("Failed to warm up snapshot");
164+
return;
165+
}
166+
167+
php_v8_startup_data_create(return_value, warm_data);
168+
}
169+
143170

144171
PHP_V8_ZEND_BEGIN_ARG_WITH_CONSTRUCTOR_INFO_EX(arginfo___construct, 1)
145172
ZEND_ARG_TYPE_INFO(0, blob, IS_STRING, 0)
@@ -155,12 +182,18 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_createFromSource, ZEND_RET
155182
ZEND_ARG_TYPE_INFO(0, source, IS_STRING, 0)
156183
ZEND_END_ARG_INFO()
157184

185+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_warmUpSnapshotDataBlob, ZEND_RETURN_VALUE, 2, V8\\StartupData, 0)
186+
ZEND_ARG_OBJ_INFO(0, cold_startup_data, V8\\StartupData, 0)
187+
ZEND_ARG_TYPE_INFO(0, warmup_source, IS_STRING, 0)
188+
ZEND_END_ARG_INFO()
189+
158190

159191
static const zend_function_entry php_v8_startup_data_methods[] = {
160-
PHP_V8_ME(StartupData, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
161-
PHP_V8_ME(StartupData, getData, ZEND_ACC_PUBLIC)
162-
PHP_V8_ME(StartupData, getRawSize, ZEND_ACC_PUBLIC)
163-
PHP_V8_ME(StartupData, createFromSource, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
192+
PHP_V8_ME(StartupData, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
193+
PHP_V8_ME(StartupData, getData, ZEND_ACC_PUBLIC)
194+
PHP_V8_ME(StartupData, getRawSize, ZEND_ACC_PUBLIC)
195+
PHP_V8_ME(StartupData, createFromSource, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
196+
PHP_V8_ME(StartupData, warmUpSnapshotDataBlob, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
164197

165198
PHP_FE_END
166199
};

stubs/src/StartupData.php

+16
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@ public static function createFromSource(string $source): StartupData
4646

4747
return new self($blob);
4848
}
49+
50+
/**
51+
* Bootstrap an isolate and a context from the cold startup blob, run the
52+
* warm-up script to trigger code compilation. The side effects are then
53+
* discarded. The resulting startup snapshot will include compiled code.
54+
*
55+
* The argument startup blob is untouched.
56+
*
57+
* @param StartupData $cold_startup_data
58+
* @param string $warmup_source
59+
*
60+
* @return StartupData
61+
*/
62+
public static function warmUpSnapshotDataBlob(StartupData $cold_startup_data, string $warmup_source): StartupData
63+
{
64+
}
4965
}

tests/001-verify_extension_entities.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class V8\StartupData
325325
public function getData(): string
326326
public function getRawSize(): int
327327
public static function createFromSource(string $source): V8\StartupData
328+
public static function warmUpSnapshotDataBlob(V8\StartupData $cold_startup_data, string $warmup_source): V8\StartupData
328329

329330
class V8\Isolate
330331
const MEMORY_PRESSURE_LEVEL_NONE = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
V8\StartupData::warmUpSnapshotDataBlob
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
5+
--ENV--
6+
HOME=/tmp/we-need-home-env-var-set-to-load-valgrindrc
7+
--FILE--
8+
<?php
9+
require '.tracking_dtors.php';
10+
11+
/** @var \Phpv8Testsuite $helper */
12+
$helper = require '.testsuite.php';
13+
14+
require '.v8-helpers.php';
15+
$v8_helper = new PhpV8Helpers($helper);
16+
17+
18+
19+
$source = 'function test_snapshot() { return "hello, world";}';
20+
$data = V8\StartupData::createFromSource($source);
21+
22+
$isolate = new V8\Isolate($data);
23+
$context = new \V8\Context($isolate);
24+
25+
$helper->assert('Context should have test function', $v8_helper->CompileTryRun($context, 'test_snapshot()')->value(), 'hello, world');
26+
27+
$wam_source = 'test_snapshot = function () { return "hello, warm world";}';
28+
29+
$warm_data = V8\StartupData::warmUpSnapshotDataBlob($data, $wam_source);
30+
31+
32+
$isolate = new V8\Isolate($warm_data);
33+
$context = new \V8\Context($isolate);
34+
35+
36+
$helper->assert('Warm data has no side effects', $res = $v8_helper->CompileTryRun($context, 'test_snapshot()')->value(), 'hello, world');
37+
38+
39+
?>
40+
--EXPECT--
41+
Context should have test function: ok
42+
Warm data has no side effects: ok

0 commit comments

Comments
 (0)