summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbatleram <batleram@gmail.com>2026-10-24 17:59:43 -0400
committerbatleram <batleram@gmail.com>2026-10-24 17:59:43 -0400
commitf692f2b7c3a47f156aff5a457bbd9bddb0619249 (patch)
treeee62e47ee0db77291765ca2cc225dd6e0569d377
parentfc1252710f8064bb144677e17a598933b4c68d13 (diff)
cleanup lut ~800ms
-rw-r--r--Makefile3
-rw-r--r--calc.c20
-rw-r--r--itoa_gen.py17
3 files changed, 21 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 7d75e27..537381f 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,9 @@ build: calc.c lut
bench: build
./calc 10.0.0.0/8 2
+bigbench: build
+ ./calc 10.0.0.0/6 2
+
lut: itoa_gen.py
python3 ./itoa_gen.py
diff --git a/calc.c b/calc.c
index fee64a2..fb0a5a4 100644
--- a/calc.c
+++ b/calc.c
@@ -32,20 +32,20 @@ uint32_t subnetmask_for_netspace(unsigned int desired_netspace) {
void write_ip(char **chunk_ptr, uint32_t ip) {
// local ptr so we don't have to deref every time
char *ptr = *chunk_ptr;
- LutEntry e = {.bits = itoa_lut[(ip >> 24) & 0xff]};
- WRITE_STR(ptr, e.fields.chars, e.fields.len)
+ LutEntry e = itoa_lut[(ip >> 24) & 0xff];
+ WRITE_STR(ptr, e.chars, e.len)
ptr[0] = '.';
ptr += 1;
- e.bits = itoa_lut[(ip >> 16) & 0xff];
- WRITE_STR(ptr, e.fields.chars, e.fields.len)
+ e = itoa_lut[(ip >> 16) & 0xff];
+ WRITE_STR(ptr, e.chars, e.len)
ptr[0] = '.';
ptr += 1;
- e.bits = itoa_lut[(ip >> 8) & 0xff];
- WRITE_STR(ptr, e.fields.chars, e.fields.len)
+ e = itoa_lut[(ip >> 8) & 0xff];
+ WRITE_STR(ptr, e.chars, e.len)
ptr[0] = '.';
ptr += 1;
- e.bits = itoa_lut[ip & 0xff];
- WRITE_STR(ptr, e.fields.chars, e.fields.len)
+ e = itoa_lut[ip & 0xff];
+ WRITE_STR(ptr, e.chars, e.len)
*chunk_ptr = ptr;
}
@@ -106,8 +106,8 @@ int main(int argc, char** argv) {
write_ip(&chunk_ptr, broadcast);
WRITE_STR(chunk_ptr, "\",\n", 3)
WRITE_STR(chunk_ptr, "\t\t\"Nbr\": \"", 10)
- LutEntry e = {.bits = itoa_lut[ips_per_subnet - 2]};
- WRITE_STR(chunk_ptr, e.fields.chars, e.fields.len);
+ LutEntry e = itoa_lut[ips_per_subnet - 2];
+ WRITE_STR(chunk_ptr, e.chars, e.len);
if (i+1 == req.count) {
WRITE_STR(chunk_ptr, "\"\n\t}\n", 5)
}else {
diff --git a/itoa_gen.py b/itoa_gen.py
index 0437c62..2c62ed5 100644
--- a/itoa_gen.py
+++ b/itoa_gen.py
@@ -1,15 +1,14 @@
with open("itoa_lut.h", "w") as f:
f.write("#include <stdint.h>\n")
- f.write("typedef union { struct { char chars[3]; uint8_t len; } fields; uint32_t bits; } LutEntry;\n")
- f.write("const uint32_t itoa_lut[256] = {")
+ f.write("typedef struct { char chars[3]; uint8_t len; } LutEntry;\n")
+ f.write("const LutEntry itoa_lut[256] = {\n")
out = []
for i in range(0, 256):
running=len(str(i))
- for j in reversed(range(3)):
- running = running << 8
- if j < len(str(i)):
- running += ord(str(i)[j])
- out.append(hex(running))
- f.write(", ".join(out))
- f.write("};")
+ s = str(i)
+ while len(s) < 3:
+ s=s+chr(69)
+ out.append("{{"+",".join(map(lambda x: f"'{x}'", s))+"}, "+str(len(str(i)))+"}")
+ f.write(",\n".join(out))
+ f.write("\n};")