Skip to content

Commit

Permalink
pj_open_file(): cache result in PJ_CONTEXT structure
Browse files Browse the repository at this point in the history
Helps marginally for MapServer/MapServer#7189
  • Loading branch information
rouault committed Nov 28, 2024
1 parent c4b29f6 commit 9ab0f64
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ projCppContext *pj_ctx::get_cpp_context() {
/************************************************************************/

void pj_ctx::set_search_paths(const std::vector<std::string> &search_paths_in) {
lookupedFiles.clear();
search_paths = search_paths_in;
delete[] c_compat_paths;
c_compat_paths = nullptr;
Expand Down
15 changes: 15 additions & 0 deletions src/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,16 @@ NS_PROJ::FileManager::open_resource_file(PJ_CONTEXT *ctx, const char *name,
*/
int pj_find_file(PJ_CONTEXT *ctx, const char *short_filename,
char *out_full_filename, size_t out_full_filename_size) {
const auto iter = ctx->lookupedFiles.find(short_filename);
if (iter != ctx->lookupedFiles.end()) {
if (iter->second.empty()) {
out_full_filename[0] = 0;
return 0;
}
snprintf(out_full_filename, out_full_filename_size, "%s",
iter->second.c_str());
return 1;
}
const bool old_network_enabled =
proj_context_is_network_enabled(ctx) != FALSE;
if (old_network_enabled)
Expand All @@ -1787,6 +1797,11 @@ int pj_find_file(PJ_CONTEXT *ctx, const char *short_filename,
ctx, short_filename, out_full_filename, out_full_filename_size);
if (old_network_enabled)
proj_context_set_enable_network(ctx, true);
if (file) {
ctx->lookupedFiles[short_filename] = out_full_filename;
} else {
ctx->lookupedFiles[short_filename] = std::string();
}
return file != nullptr;
}

Expand Down
3 changes: 3 additions & 0 deletions src/proj_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@ struct pj_ctx {
void *user_data) = nullptr;
void *file_finder_user_data = nullptr;

// Cache result of pj_find_file()
std::map<std::string, std::string> lookupedFiles{};

bool defer_grid_opening = false; // set transiently by pj_obj_create()

projFileApiCallbackAndData fileApi{};
Expand Down

0 comments on commit 9ab0f64

Please sign in to comment.