Repository: valkey-io/valkey
File: src/server.c (C)

Code before:
        shared.loadingerr = shared.loadingerr_valkey;
        shared.slowevalerr = shared.slowevalerr_valkey;
        shared.slowscripterr = shared.slowscripterr_valkey;
        shared.slowmoduleerr = shared.slowmoduleerr_valkey;
        shared.bgsaveerr = shared.bgsaveerr_valkey;
    }
}

void createSharedObjects(void) {
    int j;

    /* Shared command responses */
    shared.ok = makeObjectShared(createObject(OBJ_STRING, sdsnew("+OK\r\n")));
    shared.emptybulk = makeObjectShared(createObject(OBJ_STRING, sdsnew("$0\r\n\r\n")));
    shared.czero = makeObjectShared(createObject(OBJ_STRING, sdsnew(":0\r\n")));
    shared.cone = makeObjectShared(createObject(OBJ_STRING, sdsnew(":1\r\n")));
    shared.emptyarray = makeObjectShared(createObject(OBJ_STRING, sdsnew("*0\r\n")));
    shared.pong = makeObjectShared(createObject(OBJ_STRING, sdsnew("+PONG\r\n")));
    shared.queued = makeObjectShared(createObject(OBJ_STRING, sdsnew("+QUEUED\r\n")));
    shared.emptyscan = makeObjectShared(createObject(OBJ_STRING, sdsnew("*2\r\n$1\r\n0\r\n*0\r\n")));
    shared.space = makeObjectShared(createObject(OBJ_STRING, sdsnew(" ")));
    shared.plus = makeObjectShared(createObject(OBJ_STRING, sdsnew("+")));

    /* Shared command error responses */
    shared.wrongtypeerr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n")));
    shared.err = makeObjectShared(createObject(OBJ_STRING, sdsnew("-ERR\r\n")));
    shared.nokeyerr = makeObjectShared(createObject(OBJ_STRING, sdsnew("-ERR no such key\r\n")));
    shared.syntaxerr = makeObjectShared(createObject(OBJ_STRING, sdsnew("-ERR syntax error\r\n")));
    shared.sameobjecterr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-ERR source and destination objects are the same\r\n")));
    shared.outofrangeerr = makeObjectShared(createObject(OBJ_STRING, sdsnew("-ERR index out of range\r\n")));
    shared.noscripterr = makeObjectShared(createObject(OBJ_STRING, sdsnew("-NOSCRIPT No matching script.\r\n")));
    createSharedObjectsWithCompat();
    updateSharedObjectsWithCompat();
    shared.primarydownerr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'.\r\n")));
    shared.roreplicaerr = makeObjectShared((createObject(
            OBJ_STRING, sdsnew("-READONLY You can't write against a read only replica.\r\n"))));
    shared.noautherr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-NOAUTH Authentication required.\r\n")));
    shared.oomerr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-OOM command not allowed when used memory > 'maxmemory'.\r\n")));
    shared.execaborterr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-EXECABORT Transaction discarded because of previous errors.\r\n")));
    shared.noreplicaserr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-NOREPLICAS Not enough good replicas to write.\r\n")));
    shared.busykeyerr = makeObjectShared(createObject(
            OBJ_STRING, sdsnew("-BUSYKEY Target key name already exists.\r\n")));

    /* The shared NULL depends on the protocol version. */

Review comment (bug):
If we anyway touch these lines, maybe we can add a small function `createSharedString` to do this. It makes the code smaller:

```suggestion
    shared.wrongtypeerr = createSharedString("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n");
```

The implementation can be static inline, very small:

```c
static inline robj *createSharedString(const char *string) {
    return makeObjectShared(createObject(OBJ_STRING, sdsnew(string));
}

Code after:
    shared.emptyarray = createSharedString("*0\r\n");
    shared.pong = createSharedString("+PONG\r\n");
    shared.queued = createSharedString("+QUEUED\r\n");
    shared.emptyscan = createSharedString("*2\r\n$1\r\n0\r\n*0\r\n");
    shared.space = createSharedString(" ");
    shared.plus = createSharedString("+");

    /* Shared command error responses */
    shared.wrongtypeerr = createSharedString("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n");
    shared.err = createSharedString("-ERR\r\n");
    shared.nokeyerr = createSharedString("-ERR no such key\r\n");
    shared.syntaxerr = createSharedString("-ERR syntax error\r\n");
    shared.sameobjecterr = createSharedString("-ERR source and destination objects are the same\r\n");
    shared.outofrangeerr = createSharedString("-ERR index out of range\r\n");
    shared.noscripterr = createSharedString("-NOSCRIPT No matching script.\r\n");
    createSharedObjectsForCompat(0);
    createSharedObjectsForCompat(1);
    updateSharedObjectsWithCompat();
    shared.primarydownerr = createSharedString("-MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'.\r\n");
    shared.roreplicaerr = createSharedString("-READONLY You can't write against a read only replica.\r\n");
    shared.noautherr = createSharedString("-NOAUTH Authentication required.\r\n");
    shared.oomerr = createSharedString("-OOM command not allowed when used memory > 'maxmemory'.\r\n");
    shared.execaborterr = createSharedString("-EXECABORT Transaction discarded because of previous errors.\r\n");
    shared.noreplicaserr = createSharedString("-NOREPLICAS Not enough good replicas to write.\r\n");
    shared.busykeyerr = createSharedString("-BUSYKEY Target key name already exists.\r\n");

    /* The shared NULL depends on the protocol version. */
    shared.null[0] = NULL;
    shared.null[1] = NULL;
    shared.null[2] = createSharedString("$-1\r\n");
    shared.null[3] = createSharedString("_\r\n");

    shared.nullarray[0] = NULL;
    shared.nullarray[1] = NULL;
    shared.nullarray[2] = createSharedString("*-1\r\n");
    shared.nullarray[3] = createSharedString("_\r\n");

    shared.emptymap[0] = NULL;
    shared.emptymap[1] = NULL;
    shared.emptymap[2] = createSharedString("*0\r\n");
    shared.emptymap[3] = createSharedString("%0\r\n");

    shared.emptyset[0] = NULL;
    shared.emptyset[1] = NULL;
    shared.emptyset[2] = createSharedString("*0\r\n");
    shared.emptyset[3] = createSharedString("~0\r\n");

    for (j = 0; j < PROTO_SHARED_SELECT_CMDS; j++) {
        char dictid_str[64];
        int dictid_len;