Hacker News new | ask | show | jobs
by pdkl95 3784 days ago
> But is that system easy to understand?

Yes, if you documented it properly.

> Is the code easy

I haven't used go, but it's easy in C. Setting up macros for all the bit manipulation is a very common technique. Usage would be trivial - just call a couple accessor macros. It's certainly easier than walking the parse tree of a JSON record. Using a simple bitmap would also skip the initial parsing step.

It's easier to use the bitmap. The "example usage" section below is very simple. Setting up ElasticSearch or fiddling with a JSON parser is more work (and a lot harder on CPU/RAM).

    /* the "2nd array" */
    uint64 *port_states;
    #define IP_PORT_STATES(ipaddr) (port_states[ip])

    #define PORT_22_INDEX 0
    #define PORT_80_INDEX 1
    // ...etc...

    /* some of the accessor macros */
    #define PORT_STATE_MASK (0x00000003)
    #define PORT_STATE(ipaddr, port_index) \
        (PORT_STATE_MASK & (IP_PORT_STATES(ipaddr) >> (3 * (port_index)))

    #define PORT_CLOSED 0
    #define PORT_OPEN   1
    // ...etc...

    /* example usage  */
    uint32 open_count = 0;
    uint32 ip = 1;
    do {
        if (PORT_STATE(ip, PORT_22_INDEX) == PORT_OPEN) {
            open_count++;
        }
    } while (ip < 0xffffffff)
> future hires

If they can't handle calling a couple macros over a uint64 array, I wouldn't recommend hiring them.

> too large to fit in memory

It's not necessarily in memory - the "old-timer" is using a memory mapped file.

edit: bugfix

1 comments

This is 206, why are you using #defines instead of "const int"s.
Because the last time I wrote a bitmap like that was about a decade ago using a proprietary compiler for a Z80 clone that implemented a "C89-like"[1] language? Call it a bad habit from too many years working with embedded micros.

If I was using a modern(-ish) C, an enum might be more appropriate.

[1] When you only have 16k of RAM and a 256 byte stack, having "int x;" default to "static" storage instead of the stack is a feature.

Assuming C code (not C++), then const int is not the same as a #define. An enum is only good if the values fit within the range of a native int.
static const's may not be optimized to literals. constexpr is not fully supported.