Skip to content

Commit d7f25f5

Browse files
dcarneyCommit bot
authored andcommitted
use a hash table for the function cache as blink is leaking functiontemplates
BUG= Review URL: https://codereview.chromium.org/988283003 Cr-Commit-Position: refs/heads/master@{#27066}
1 parent d18bfa1 commit d7f25f5

5 files changed

Lines changed: 35 additions & 35 deletions

File tree

src/api-natives.cc

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,36 +207,33 @@ MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
207207
}
208208

209209

210-
void InstallInCache(Isolate* isolate, int serial_number,
211-
Handle<JSFunction> function) {
210+
void CacheFunction(Isolate* isolate, Handle<Smi> serial_number,
211+
Handle<JSFunction> function) {
212212
auto cache = isolate->function_cache();
213-
if (cache->length() <= serial_number) {
214-
int new_size;
215-
if (isolate->next_serial_number() < 50) {
216-
new_size = 100;
217-
} else {
218-
new_size = 3 * isolate->next_serial_number() / 2;
219-
}
220-
cache = FixedArray::CopySize(cache, new_size);
221-
isolate->native_context()->set_function_cache(*cache);
222-
}
223-
cache->set(serial_number, *function);
213+
auto new_cache = ObjectHashTable::Put(cache, serial_number, function);
214+
isolate->native_context()->set_function_cache(*new_cache);
215+
}
216+
217+
218+
void UncacheFunction(Isolate* isolate, Handle<Smi> serial_number) {
219+
auto cache = isolate->function_cache();
220+
bool was_present = false;
221+
auto new_cache = ObjectHashTable::Remove(cache, serial_number, &was_present);
222+
DCHECK(was_present);
223+
isolate->native_context()->set_function_cache(*new_cache);
224224
}
225225

226226

227227
MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
228228
Handle<FunctionTemplateInfo> data,
229229
Handle<Name> name) {
230-
int serial_number = Smi::cast(data->serial_number())->value();
230+
auto serial_number = handle(Smi::cast(data->serial_number()), isolate);
231231
// Probe cache.
232232
if (!data->do_not_cache()) {
233233
auto cache = isolate->function_cache();
234-
// Fast case: see if the function has already been instantiated
235-
if (serial_number < cache->length()) {
236-
Handle<Object> element = FixedArray::get(cache, serial_number);
237-
if (element->IsJSFunction()) {
238-
return Handle<JSFunction>::cast(element);
239-
}
234+
Object* element = cache->Lookup(serial_number);
235+
if (element->IsJSFunction()) {
236+
return handle(JSFunction::cast(element), isolate);
240237
}
241238
}
242239
// Enter a new scope. Recursion could otherwise create a lot of handles.
@@ -279,15 +276,14 @@ MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
279276
function->shared()->set_name(*name);
280277
}
281278
if (!data->do_not_cache()) {
282-
// Cache the function to limit recursion.
283-
InstallInCache(isolate, serial_number, function);
279+
// Cache the function.
280+
CacheFunction(isolate, serial_number, function);
284281
}
285282
auto result = ConfigureInstance(isolate, function, data);
286283
if (result.is_null()) {
287-
// uncache on error.
284+
// Uncache on error.
288285
if (!data->do_not_cache()) {
289-
auto cache = isolate->function_cache();
290-
cache->set(serial_number, isolate->heap()->undefined_value());
286+
UncacheFunction(isolate, serial_number);
291287
}
292288
return MaybeHandle<JSFunction>();
293289
}

src/api-natives.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace internal {
1212

1313
class ApiNatives {
1414
public:
15+
static const int kInitialFunctionCacheSize = 256;
16+
1517
MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction(
1618
Handle<FunctionTemplateInfo> data);
1719

src/bootstrapper.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,9 @@ bool Genesis::InstallNatives() {
21172117

21182118
InstallNativeFunctions();
21192119

2120-
native_context()->set_function_cache(heap()->empty_fixed_array());
2120+
auto function_cache =
2121+
ObjectHashTable::New(isolate(), ApiNatives::kInitialFunctionCacheSize);
2122+
native_context()->set_function_cache(*function_cache);
21212123

21222124
// Store the map for the string prototype after the natives has been compiled
21232125
// and the String function has been set up.

src/contexts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ enum BindingFlags {
140140
V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
141141
V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
142142
V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
143-
V(FUNCTION_CACHE_INDEX, FixedArray, function_cache) \
143+
V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache) \
144144
V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
145145
V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \
146146
V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \

test/cctest/test-api.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20038,15 +20038,15 @@ THREADED_TEST(FunctionNew) {
2003820038
env->Global()->Set(v8_str("func"), func);
2003920039
Local<Value> result = CompileRun("func();");
2004020040
CHECK(v8::Integer::New(isolate, 17)->Equals(result));
20041-
// Verify function not cached
20042-
int serial_number =
20043-
i::Smi::cast(v8::Utils::OpenHandle(*func)
20044-
->shared()->get_api_func_data()->serial_number())->value();
2004520041
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
20046-
i::Handle<i::FixedArray> cache(i_isolate->native_context()->function_cache());
20047-
if (serial_number < cache->length()) {
20048-
CHECK(cache->get(serial_number)->IsUndefined());
20049-
}
20042+
// Verify function not cached
20043+
auto serial_number = handle(i::Smi::cast(v8::Utils::OpenHandle(*func)
20044+
->shared()
20045+
->get_api_func_data()
20046+
->serial_number()),
20047+
i_isolate);
20048+
auto cache = i_isolate->function_cache();
20049+
CHECK(cache->Lookup(serial_number)->IsTheHole());
2005020050
// Verify that each Function::New creates a new function instance
2005120051
Local<Object> data2 = v8::Object::New(isolate);
2005220052
function_new_expected_env = data2;

0 commit comments

Comments
 (0)