Skip to content

Instantly share code, notes, and snippets.

View bdw's full-sized avatar
💭
Hacking

Bart Wiegmans bdw

💭
Hacking
View GitHub Profile
@bdw
bdw / reciprocal.c
Last active August 19, 2018 08:12
minor benchmarks
#include <stdio.h>
/* this takes, excluding compilation, 0.23s on my machine.
* MoarVM is within a factor of three of that */
int main(int argc, char **argv) {
double x = 0;
int i;
for (i = 1; i < 50000000; i++)
x += 1.0/i;
printf("%f", x);
=================================================================
==31674==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 416 byte(s) in 2 object(s) allocated from:
#0 0x7fd9b5224e50 in calloc (/lib64/libasan.so.5+0xeee50)
#1 0x7fd9b40924c5 in MVM_calloc src/core/alloc.h:11
#2 0x7fd9b409de50 in MVM_spesh_graph_create src/spesh/graph.c:1214
#3 0x7fd9b40df1f9 in MVM_spesh_inline_try_get_graph_from_unspecialized src/spesh/inline.c:236
#4 0x7fd9b40cd1f3 in optimize_call src/spesh/optimize.c:1831
@bdw
bdw / calculate.nqp
Created June 10, 2018 16:14
A hopelessly optimal case for the JIT compiler
#!/usr/bin/env nqp
# From https://www.johnhawthorn.com/2018/02/playing-with-ruby-jit-mjit/
sub calculate(int $a, int $b, int $n) {
my int $i := 0;
my int $c := 0;
while $i < $n {
$a := $a * 16807 % 2147483647;
$b := $b * 48271 % 2147483647;
@bdw
bdw / mislabel.sam
Created May 20, 2018 09:36
broken-label.asm
;; we jump inbetween this and the next, and so corrupt our stack and our frame
505a: 4c 8b 5b 58 mov r11,QWORD PTR [rbx+0x58]
505e: 48 8b 53 60 mov rdx,QWORD PTR [rbx+0x60]
5062: 48 89 93 e8 02 00 00 mov QWORD PTR [rbx+0x2e8],rdx
5069: 48 c7 c2 04 00 00 00 mov rdx,0x4
5070: 48 8b 8b d8 02 00 00 mov rcx,QWORD PTR [rbx+0x2d8]
5077: 4c 89 5c 24 20 mov QWORD PTR [rsp+0x20],r11
507c: 48 89 54 24 30 mov QWORD PTR [rsp+0x30],rdx
5081: 4c 89 c0 mov rax,r8
5084: 4c 89 ca mov rdx,r9
@bdw
bdw / search_frame_handlers_lex.s
Created May 9, 2018 06:25
broken assembly on windows
0x0000000069bc7443 <+0>: push rbp
0x0000000069bc7444 <+1>: push rbx
0x0000000069bc7445 <+2>: sub rsp,0x98
0x0000000069bc744c <+9>: lea rbp,[rsp+0x80]
0x0000000069bc7454 <+17>: mov QWORD PTR [rbp+0x30],rcx
0x0000000069bc7458 <+21>: mov QWORD PTR [rbp+0x38],rdx
0x0000000069bc745c <+25>: mov DWORD PTR [rbp+0x40],r8d
0x0000000069bc7460 <+29>: mov QWORD PTR [rbp+0x48],r9
0x0000000069bc7464 <+33>: mov rax,QWORD PTR [rbp+0x58]
0x0000000069bc7468 <+37>: mov eax,DWORD PTR [rax]
#!/usr/bin/env perl6
multi sub euclid ($a is copy, $b is copy) {
while ($b != 0) {
($a, $b) = ($b, $a % $b);
}
return $a;
}
multi sub euclid ($a, $b where $a < $b) {
Starting dump of JIT expression tree
====================================
digraph {
n_0001 [label="LOCAL"];
n_0002 [label="ADDR(0xd8)"];
n_0002 -> n_0001;
n_0008 [label="TC"];
n_0009 [label="ADDR(0x1b0)"];
n_0009 -> n_0008;
n_0012 [label="LOAD(0x8)"];
#!/usr/bin/env perl
use strict;
use warnings;
sub foo {
my ($x) = @_;
return 0 if ($x == 0);
return foo $x - 1;
}
foo 3;
@bdw
bdw / channel-breakage.pl6
Created November 16, 2017 16:44
breaking-expr-in-inline-lastpayload
#!/usr/bin/env perl6
use v6;
for (0..1000) {
my $channel = Channel.new;
$channel.close;
my $result = (try $channel.receive) // buf8;
say "Result is undefined" unless $result.defined;
}
diff --git a/src/profiler/heapsnapshot.c b/src/profiler/heapsnapshot.c
index 071954f..a4a6d9c 100644
--- a/src/profiler/heapsnapshot.c
+++ b/src/profiler/heapsnapshot.c
@@ -820,7 +820,7 @@ void references_to_filehandle(MVMThreadContext *tc, MVMHeapSnapshotCollection *c
MVMuint64 maxval = MAX(kind, cindex);
- if (maxval + 1 >= 1l << 32) {
+ if (maxval + 1 >= (UINT64_C(1) << 32)) {