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

Code before:
    return makeObjectShared(createObject(OBJ_STRING, ptr));
}

/* These shared strings depend on the extended-redis-compatibility config and is
 * called when the config changes. When the config is phased out, these
 * initializations can be moved back inside createSharedObjects() below. */
void createSharedObjectsWithCompatNo(void) {
    const char *name = SERVER_TITLE;
    shared.loadingerr_valkey =
        createSharedStringByPtr(sdscatfmt(sdsempty(), "-LOADING %s is loading the dataset in memory\r\n", name));
    shared.slowevalerr_valkey = createSharedStringByPtr(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowscripterr_valkey = createSharedStringByPtr(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowmoduleerr_valkey =
        createSharedStringByPtr(sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name));
    shared.bgsaveerr_valkey =
        createSharedStringByPtr(sdscatfmt(sdsempty(),
                                          "-MISCONF %s is configured to save RDB snapshots, but it's currently"
                                          " unable to persist to disk. Commands that may modify the data set are"
                                          " disabled, because this instance is configured to report errors during"
                                          " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)."
                                          " Please check the %s logs for details about the RDB error.\r\n",
                                          name, name));
}

void createSharedObjectsWithCompatYes(void) {
    const char *name = "Redis";
    shared.loadingerr_redis =
        createSharedStringByPtr(sdscatfmt(sdsempty(), "-LOADING %s is loading the dataset in memory\r\n", name));
    shared.slowevalerr_redis = createSharedStringByPtr(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowscripterr_redis = createSharedStringByPtr(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowmoduleerr_redis =
        createSharedStringByPtr(sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name));
    shared.bgsaveerr_redis =
        createSharedStringByPtr(sdscatfmt(sdsempty(),
                                          "-MISCONF %s is configured to save RDB snapshots, but it's currently"
                                          " unable to persist to disk. Commands that may modify the data set are"
                                          " disabled, because this instance is configured to report errors during"
                                          " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)."
                                          " Please check the %s logs for details about the RDB error.\r\n",
                                          name, name));
}

void createSharedObjectsWithCompat(void) {

Review comment (bug):
Oh... I didn't realize we would duplicate large texts like this. It's not perfect....

:thinking: Is there any way to do this without duplicating a lot of code?

Maybe we can put these shared variants in arrays indexed by compat mode 0 or 1. Then we can have just one function to initialize these. We call it twice, with `compat=0 name=SERVER_NAME` and with `compat=1 name=Redis`.

```suggestion
    shared.slowmoduleerr_variants[is_compat] =
        createSharedStringByPtr(sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name));
    shared.bgsaveerr_variants[is_compat] =
        createSharedStringByPtr(sdscatfmt(sdsempty(),
                                          "-MISCONF %s is configured to save RDB snapshots, but it's currently"
                                          " unable to persist to disk. Commands that may modify the data set are"
                                          " disabled, because this instance is configured to report errors during"
                                          " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)."
                                          " Please check the %s logs for details about the RDB error.\r\n",
                                          name, name));
```

Code after:
    return makeObjectShared(createObject(OBJ_STRING, s));
}

/* These shared strings depend on the extended-redis-compatibility config and is
 * called at initialization. When the config is phased out, these
 * initializations can be moved back inside createSharedObjects() below. */
void createSharedObjectsForCompat(int compat) {
    const char *name = compat ? "Redis" : SERVER_TITLE;
    shared.loadingerr_variants[compat] =
        createSharedStringFromSds(sdscatfmt(sdsempty(), "-LOADING %s is loading the dataset in memory\r\n", name));
    shared.slowevalerr_variants[compat] = createSharedStringFromSds(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowscripterr_variants[compat] = createSharedStringFromSds(
        sdscatfmt(sdsempty(),
                  "-BUSY %s is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.\r\n", name));
    shared.slowmoduleerr_variants[compat] =
        createSharedStringFromSds(sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name));
    shared.bgsaveerr_variants[compat] =
        createSharedStringFromSds(sdscatfmt(sdsempty(),
                                            "-MISCONF %s is configured to save RDB snapshots, but it's currently"
                                            " unable to persist to disk. Commands that may modify the data set are"
                                            " disabled, because this instance is configured to report errors during"
                                            " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)."
                                            " Please check the %s logs for details about the RDB error.\r\n",
                                            name, name));
}

/* These shared strings depend on the extended-redis-compatibility config and are
 * called at initialization and when the config changes. */
void updateSharedObjectsWithCompat(void) {
    shared.loadingerr = shared.loadingerr_variants[server.extended_redis_compat];
    shared.slowevalerr = shared.slowevalerr_variants[server.extended_redis_compat];
    shared.slowscripterr = shared.slowscripterr_variants[server.extended_redis_compat];
    shared.slowmoduleerr = shared.slowmoduleerr_variants[server.extended_redis_compat];
    shared.bgsaveerr = shared.bgsaveerr_variants[server.extended_redis_compat];
}

void createSharedObjects(void) {
    int j;

    /* Shared command responses */
    shared.ok = createSharedString("+OK\r\n");
    shared.emptybulk = createSharedString("$0\r\n\r\n");
    shared.czero = createSharedString(":0\r\n");
    shared.cone = createSharedString(":1\r\n");
    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(" ");