-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move compile_cache to jsg/compile_cache (#2990)
- Loading branch information
Showing
4 changed files
with
92 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#include "compile-cache.h" | ||
|
||
namespace workerd::jsg { | ||
|
||
// CompileCache::Data | ||
|
||
std::unique_ptr<v8::ScriptCompiler::CachedData> CompileCache::Data::AsCachedData() { | ||
return std::make_unique<v8::ScriptCompiler::CachedData>( | ||
data, length, v8::ScriptCompiler::CachedData::BufferNotOwned); | ||
} | ||
|
||
// CompileCache | ||
|
||
void CompileCache::add( | ||
kj::StringPtr key, std::shared_ptr<v8::ScriptCompiler::CachedData> cached) const { | ||
cache.lockExclusive()->upsert(kj::str(key), Data(kj::mv(cached)), [](auto&, auto&&) {}); | ||
} | ||
|
||
kj::Maybe<CompileCache::Data&> CompileCache::find(kj::StringPtr key) const { | ||
KJ_IF_SOME(value, cache.lockExclusive()->find(key)) { | ||
if (value.data != nullptr) { | ||
return value; | ||
} | ||
} | ||
return kj::none; | ||
} | ||
|
||
} // namespace workerd::jsg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
#pragma once | ||
|
||
#include "jsg.h" | ||
#include "setup.h" | ||
|
||
#include <v8.h> | ||
|
||
#include <kj/string.h> | ||
|
||
namespace workerd::jsg { | ||
|
||
// The CompileCache is used to hold cached compilation data for built-in JavaScript modules. | ||
// | ||
// Importantly, this is a process-lifetime in-memory cache that is only appropriate for | ||
// built-in modules. | ||
// | ||
// The memory-safety of this cache depends on the assumption that entries are never removed | ||
// or replaced. If things are ever changed such that entries are removed/replaced, then | ||
// we'd likely need to have find return an atomic refcount or something similar. | ||
class CompileCache { | ||
public: | ||
class Data { | ||
public: | ||
Data(): data(nullptr), length(0), owningPtr(nullptr) {}; | ||
explicit Data(std::shared_ptr<v8::ScriptCompiler::CachedData> cached_data) | ||
: data(cached_data->data), | ||
length(cached_data->length), | ||
owningPtr(cached_data) {}; | ||
|
||
// Returns a v8::ScriptCompiler::CachedData corresponding to this | ||
// CompileCache::Data. The lifetime of the returned | ||
// v8::ScriptCompiler::CachedData must not outlive that of the data. | ||
std::unique_ptr<v8::ScriptCompiler::CachedData> AsCachedData(); | ||
|
||
const uint8_t* data; | ||
size_t length; | ||
|
||
private: | ||
std::shared_ptr<void> owningPtr; | ||
}; | ||
|
||
void add(kj::StringPtr key, std::shared_ptr<v8::ScriptCompiler::CachedData> cached) const; | ||
kj::Maybe<Data&> find(kj::StringPtr key) const; | ||
|
||
static const CompileCache& get() { | ||
static const CompileCache instance; | ||
return instance; | ||
} | ||
|
||
private: | ||
// The key is the address of the static global that was compiled to produce the CachedData. | ||
kj::MutexGuarded<kj::HashMap<kj::String, Data>> cache; | ||
}; | ||
|
||
} // namespace workerd::jsg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters