Skip to content

Instantly share code, notes, and snippets.

@iliabylich
Created May 7, 2024 08:02
Show Gist options
  • Save iliabylich/6b65cc3e2002533200b1c356cb0ec332 to your computer and use it in GitHub Desktop.
Save iliabylich/6b65cc3e2002533200b1c356cb0ec332 to your computer and use it in GitHub Desktop.
prism bench
// usage:
// 1. build prism
// 2. clang bench.c -O3 -Lbuild -lprism -Iinclude -o bench
// 3. ./bench /path/to/dir/with/ruby/code
#include "include/prism.h"
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
void walkdir(const char *name, pm_string_list_t *files)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL)
{
char path[1024];
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
walkdir(path, files);
}
else if (strlen(path) > 3 && !strcmp(path + strlen(path) - 3, ".rb"))
{
pm_string_t file = {0};
pm_string_file_init(&file, path);
pm_string_list_append(files, &file);
}
}
closedir(dir);
}
void parse_one(const uint8_t *source, size_t length)
{
pm_parser_t parser;
pm_parser_init(&parser, source, length, NULL);
pm_node_t *root = pm_parse(&parser);
// pm_prettyprint(&buffer, &parser, root);
// pm_buffer_append_zeroes(&buffer, 1); // trailing 0, just in case
// printf("[PP] %s\n", pm_buffer_value(&buffer));
// pm_buffer_free(&buffer);
pm_node_destroy(&parser, root);
pm_parser_free(&parser);
}
int main(int argc, char **argv)
{
pm_string_list_t files = {0};
walkdir(argv[1], &files);
printf("Parsing %lu files\n", files.length);
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
for (size_t i = 0; i < files.length; i++)
{
pm_string_t file = files.strings[i];
const uint8_t *code = pm_string_source(&file);
size_t length = pm_string_length(&file);
parse_one(code, length);
}
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t diff = ((end.tv_sec * 1000000000) + end.tv_nsec) -
((start.tv_sec * 1000000000) + start.tv_nsec);
printf("Total time: %f\n", (double)diff / 1000000000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment