From 3b24739071bf67036c07c71efe022618be16f850 Mon Sep 17 00:00:00 2001 From: ochafik Date: Fri, 28 Jun 2024 21:21:47 +0100 Subject: [PATCH] json: cache externally fetched refs (forever for now) --- common/json-schema-to-grammar.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index d149bf908..03460aa3d 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -1057,7 +1057,14 @@ std::string json_schema_to_grammar(const json & schema) { std::function fetch_json = [](const std::string &) { return json::object(); }; #if defined(LLAMA_USE_CURL) - fetch_json = [](const std::string & url) { + // TODO: implement HTTP caching semantics. + std::unordered_map cache; + + fetch_json = [&](const std::string & url) { + auto it = cache.find(url); + if (it != cache.end()) { + return it->second; + } std::unique_ptr curl(curl_easy_init(), &curl_easy_cleanup); if (!curl) { fprintf(stderr, "%s: error initializing libcurl\n", __func__); @@ -1082,7 +1089,7 @@ std::string json_schema_to_grammar(const json & schema) { throw std::runtime_error("Failed to fetch " + url + ": " + curl_easy_strerror(res)); } response << '\0'; - return json::parse(response.str()); + return cache[url] = json::parse(response.str()); }; #endif