Repository: hyprwm/Hyprland
File: src/config/ConfigManager.hpp (C++)

Code before:

    struct SVectorData {
        Vector2D vec, min, max;
    };

    std::string       value; // e.g. general:gaps_in
    std::string       description;
    std::string       specialCategory; // if value is special (e.g. device:abc) value will be abc and special device
    bool              specialKey = false;
    eConfigOptionType type       = CONFIG_OPTION_BOOL;
    uint32_t          flags      = 0; // eConfigOptionFlags

    std::string       jsonify() const;

    //
    std::variant<SBoolData, SRangeData, SFloatData, SStringData, SColorData, SChoiceData, SGradientData, SVectorData> data;
};

struct SFirstExecRequest {
    std::string exec      = "";
    bool        withRules = false;
};

struct FloatCache {
    std::string class_name;
    std::string title;

    FloatCache(PHLWINDOW window) : class_name(window->m_szClass), title(window->m_szTitle) {}
    FloatCache(const std::string& class_name_, const std::string& title_) : class_name(class_name_), title(title_) {}

    bool operator==(const FloatCache& other) const {
        return class_name == other.class_name && title == other.title;
    }
};

namespace std {
    template <>
    struct hash<FloatCache> {
        size_t operator()(const FloatCache& id) const {
            return std::hash<std::string>{}(id.class_name) ^ (std::hash<std::string>{}(id.title) << 1);
        }
    };
}

class CConfigManager {
  public:
    CConfigManager();

    void                                                            init();
    void                                                            reload();
    std::string                                                     verify();

Review comment (performance):
don't store these I'd say. Calculate the hash and store the hash instead. Then in std::hash impl just ret cache.hash

a bonus is that let's say the user has opened 1000 unique pairs of title + class in his session. Far-fetched, but I'd say the upper bounds of reasonable.

Instead of that being 1000 * let's say 200 bytes for class + title on avg = 200kB of RAM, we just store 1000 * 8 bytes = 8kB

Code after:

    struct SVectorData {
        Vector2D vec, min, max;
    };

    std::string       value; // e.g. general:gaps_in
    std::string       description;
    std::string       specialCategory; // if value is special (e.g. device:abc) value will be abc and special device
    bool              specialKey = false;
    eConfigOptionType type       = CONFIG_OPTION_BOOL;
    uint32_t          flags      = 0; // eConfigOptionFlags

    std::string       jsonify() const;

    //
    std::variant<SBoolData, SRangeData, SFloatData, SStringData, SColorData, SChoiceData, SGradientData, SVectorData> data;
};

struct SFirstExecRequest {
    std::string exec      = "";
    bool        withRules = false;
};

struct SFloatCache {
    size_t hash;

    SFloatCache(PHLWINDOW window) {
        hash = std::hash<std::string>{}(window->m_szClass) ^ (std::hash<std::string>{}(window->m_szTitle) << 1);
    }

    bool operator==(const SFloatCache& other) const {
        return hash == other.hash;
    }
};

namespace std {
    template <>
    struct hash<SFloatCache> {
        size_t operator()(const SFloatCache& id) const {
            return id.hash;
        }
    };
}

class CConfigManager {
  public:
    CConfigManager();

    void                                                            init();
    void                                                            reload();
    std::string                                                     verify();