//temp = 0 inside if block
int dec(int a, int condition) {
int temp;
if (condition) {
temp = 0; //<-- memory write depends on condition
//compute something here in a loop
while (temp < a) {
a -= (temp++);
}
}
return a;
}
//temp = 0 at declaration
int dec2(int a, int condition) {
int temp = 0; // <-- memory write always executed
if (condition) {
//compute something here in a loop
while (temp < a) {
a -= (temp++);
}
}
return a;
}
We can disassemble the output to verify that dec() will only write to memory if the condition was satisfied (see 400458), while dec2() will always write into temp (see 400482): # <dec>
400448: push %rbp
400449: mov %rsp,%rbp
40044c: mov %edi,0xffec(%rbp)
40044f: mov %esi,0xffe8(%rbp)
400452: cmpl $0x0,0xffe8(%rbp) # if (condition)
400456: je 400473 <dec+0x2b>
400458: movl $0x0,0xfffc(%rbp) # temp = 0 <-- depends on condition
40045f: jmp 40046b <dec+0x23> # while
400461: mov 0xfffc(%rbp),%eax
400464: sub %eax,0xffec(%rbp)
400467: addl $0x1,0xfffc(%rbp)
40046b: mov 0xfffc(%rbp),%eax
40046e: cmp 0xffec(%rbp),%eax
400471: jl 400461 <dec+0x19> # loop
400473: mov 0xffec(%rbp),%eax # return a
400476: leaveq
400477: retq
# <dec2>
400478: push %rbp
400479: mov %rsp,%rbp
40047c: mov %edi,0xffec(%rbp)
40047f: mov %esi,0xffe8(%rbp)
400482: movl $0x0,0xfffc(%rbp) # temp = 0 <-- always executed
400489: cmpl $0x0,0xffe8(%rbp) # if (condition)
40048d: je 4004a3 <dec2+0x2b>
40048f: jmp 40049b <dec2+0x23> # while
400491: mov 0xfffc(%rbp),%eax
400494: sub %eax,0xffec(%rbp)
400497: addl $0x1,0xfffc(%rbp)
40049b: mov 0xfffc(%rbp),%eax
40049e: cmp 0xffec(%rbp),%eax
4004a1: jl 400491 <dec2+0x19> # loop
4004a3: mov 0xffec(%rbp),%eax # return a
4004a6: leaveq
4004a7: retq
The above code was compiled with gcc 4.1.2 on amd64/linux. gcc -O2 and gcc -O3 completely do away with the 'temp' variable and generate fewer instructions.
This makes it clear that you're inheriting everything from sleepy.Resource, so you can provide reasonable implementations for all methods of resources, not just HTTP methods handlers, e.g. getBodyAsJSON(), redirect(resource), etc.
Also, depending on how your mux works, you could store app-level data in sleepy.Resource, to be accessed by all resources:
and other things I can't think of right now.