Skip to content

Commit

Permalink
a terrible hack to make hrtime faster
Browse files Browse the repository at this point in the history
  • Loading branch information
billywhizz committed Aug 12, 2022
1 parent c598dc9 commit efbf3d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
17 changes: 16 additions & 1 deletion just.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
std::map<std::string, just::builtin*> just::builtins;
std::map<std::string, just::register_plugin> just::modules;
uint32_t scriptId = 1;
just::handle* handles[1024];

ssize_t just::process_memory_usage() {
char buf[1024];
Expand Down Expand Up @@ -558,8 +559,22 @@ void just::Modules(const FunctionCallbackInfo<Value> &args) {
}

void just::HRTime(const FunctionCallbackInfo<Value> &args) {
uint64_t* ptr = reinterpret_cast<uint64_t*>(Local<BigInt>::Cast(args[0])->Uint64Value());
int index = Local<Integer>::Cast(args[0])->Value();
// todo: check out of bounds etc.
just::handle* h = handles[index];
if (h) {
uint64_t* ptr = (uint64_t*)h->ptr;
*ptr = just::hrtime();
return;
}
h = (just::handle*)calloc(1, sizeof(just::handle));
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
std::shared_ptr<BackingStore> backing = ab->GetBackingStore();
h->ptr = static_cast<char *>(backing->Data());
uint64_t* ptr = (uint64_t*)h->ptr;
*ptr = just::hrtime();
h->ptr = ptr;
handles[index] = h;
}

void just::Init(Isolate* isolate, Local<ObjectTemplate> target) {
Expand Down
3 changes: 3 additions & 0 deletions just.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ struct builtin {
unsigned int size;
const char* source;
};
struct handle {
void* ptr;
};
extern std::map<std::string, builtin*> builtins;
extern std::map<std::string, register_plugin> modules;
void builtins_add (const char* name, const char* source,
Expand Down
11 changes: 7 additions & 4 deletions just.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
(function () {
let handles = 0

function wrapHRTime (hrtime) {
const buf = new ArrayBuffer(8)
const u64 = new BigUint64Array(buf)
const address = buf.getAddress()
const handle = handles++
hrtime(handle, buf)
return () => {
hrtime(address)
return u64[0]
hrtime(handle)
return buf
}
}

Expand Down Expand Up @@ -299,6 +301,7 @@
just.net.setNonBlocking = setNonBlocking
just.require = global.require = require
just.require.cache = cache
just._hrtime = just.hrtime
just.hrtime = wrapHRTime(just.hrtime)
just.memoryUsage = wrapMemoryUsage(just.memoryUsage)
just.cpuUsage = wrapCpuUsage(just.sys.cpuUsage)
Expand Down

0 comments on commit efbf3d4

Please sign in to comment.