Skip to content

Commit 7ab602f

Browse files
committed
Merge pull request #211 from stesie/setaverageobjectsize
add V8Js::setAverageObjectSize method
2 parents 770c9b2 + 12903ca commit 7ab602f

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ class V8Js
142142
public function setMemoryLimit($limit)
143143
{}
144144

145+
/**
146+
* Set the average object size (in bytes) for this V8Js object.
147+
* V8's "amount of external memory" is adjusted by this value for every exported object. V8 triggers a garbage collection once this totals to 192 MB.
148+
* @param int $average_object_size
149+
*/
150+
public function setAverageObjectSize($average_object_size)
151+
{}
152+
145153
/**
146154
* Returns uncaught pending exception or null if there is no pending exception.
147155
* @return V8JsScriptException|null
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test V8::setAverageObjectSize() : Average object size can be set on V8Js object
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
$v8 = new V8Js();
8+
$v8->setAverageObjectSize(32768);
9+
10+
// there's no API to query the currently announced external memory allocation,
11+
// hence not much we can do here...
12+
13+
?>
14+
===EOF===
15+
--EXPECT--
16+
===EOF===

v8js_class.cc

+24-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
158158
it != c->weak_objects.end(); ++it) {
159159
zval *value = it->first;
160160
zval_ptr_dtor(&value);
161-
c->isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
161+
c->isolate->AdjustAmountOfExternalAllocatedMemory(-c->average_object_size);
162162
it->second.Reset();
163163
}
164164
c->weak_objects.~map();
@@ -254,6 +254,8 @@ static zend_object_value v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
254254
new(&c->v8js_v8objects) std::list<v8js_v8object *>();
255255
new(&c->script_objects) std::vector<v8js_script *>();
256256

257+
c->average_object_size = 1024;
258+
257259
retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) v8js_free_storage, NULL TSRMLS_CC);
258260
retval.handlers = &v8js_object_handlers;
259261

@@ -899,6 +901,22 @@ static PHP_METHOD(V8Js, setMemoryLimit)
899901
}
900902
/* }}} */
901903

904+
/* {{{ proto void V8Js::setAverageObjectSize(average_object_size)
905+
*/
906+
static PHP_METHOD(V8Js, setAverageObjectSize)
907+
{
908+
v8js_ctx *c;
909+
long average_object_size = 0;
910+
911+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &average_object_size) == FAILURE) {
912+
return;
913+
}
914+
915+
c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
916+
c->average_object_size = average_object_size;
917+
}
918+
/* }}} */
919+
902920
static void v8js_persistent_zval_ctor(zval **p) /* {{{ */
903921
{
904922
zval *orig_ptr = *p;
@@ -1178,6 +1196,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
11781196
ZEND_ARG_INFO(0, callable)
11791197
ZEND_END_ARG_INFO()
11801198

1199+
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setaverageobjectsize, 0, 0, 1)
1200+
ZEND_ARG_INFO(0, average_object_size)
1201+
ZEND_END_ARG_INFO()
1202+
11811203
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
11821204
ZEND_ARG_INFO(0, extension_name)
11831205
ZEND_ARG_INFO(0, script)
@@ -1217,6 +1239,7 @@ const zend_function_entry v8js_methods[] = { /* {{{ */
12171239
PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
12181240
PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
12191241
PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
1242+
PHP_ME(V8Js, setAverageObjectSize, arginfo_v8js_setaverageobjectsize, ZEND_ACC_PUBLIC)
12201243
PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
12211244
PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
12221245

v8js_class.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct v8js_ctx {
4747
bool time_limit_hit;
4848
long memory_limit;
4949
bool memory_limit_hit;
50+
long average_object_size;
5051

5152
v8js_tmpl_t global_template;
5253
v8js_tmpl_t array_tmpl;

v8js_object_export.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& i
259259

260260
// Just tell v8 that we're allocating some external memory
261261
// (for the moment we just always tell 1k instead of trying to find out actual values)
262-
isolate->AdjustAmountOfExternalAllocatedMemory(1024);
262+
isolate->AdjustAmountOfExternalAllocatedMemory(ctx->average_object_size);
263263
}
264264
/* }}} */
265265

@@ -275,7 +275,7 @@ static void v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zva
275275
ctx->weak_objects.at(value).Reset();
276276
ctx->weak_objects.erase(value);
277277

278-
isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
278+
isolate->AdjustAmountOfExternalAllocatedMemory(-ctx->average_object_size);
279279
}
280280

281281
static void v8js_weak_closure_callback(const v8::WeakCallbackData<v8::Object, v8js_tmpl_t> &data) {

0 commit comments

Comments
 (0)