1+ # CombinationCaching.cmake
2+ # Smart type combination reuse and caching system
3+
4+ # Main function to handle combination reuse
5+ function (use_cached_type_combinations)
6+ # Check if combinations already exist in memory
7+ if (DEFINED COMBINATIONS_2 AND DEFINED COMBINATIONS_3)
8+ list (LENGTH COMBINATIONS_2 combo2_count)
9+ list (LENGTH COMBINATIONS_3 combo3_count)
10+ if (combo2_count GREATER 0 AND combo3_count GREATER 0)
11+ message (STATUS "♻️ Reusing existing combinations: 2-type=${combo2_count} , 3-type=${combo3_count} " )
12+ return ()
13+ endif ()
14+ endif ()
15+
16+ # Check for cached combinations file
17+ set (COMBO_CACHE_FILE "${CMAKE_BINARY_DIR} /type_combinations.cache" )
18+ if (EXISTS "${COMBO_CACHE_FILE} " )
19+ message (STATUS "📁 Loading combinations from cache file..." )
20+ include ("${COMBO_CACHE_FILE} " )
21+
22+ # Verify cache loaded successfully
23+ if (DEFINED COMBINATIONS_2 AND DEFINED COMBINATIONS_3)
24+ list (LENGTH COMBINATIONS_2 combo2_count)
25+ list (LENGTH COMBINATIONS_3 combo3_count)
26+ message (STATUS "✅ Loaded from cache: 2-type=${combo2_count} , 3-type=${combo3_count} " )
27+ return ()
28+ else ()
29+ message (STATUS "⚠️ Cache file corrupted, regenerating..." )
30+ endif ()
31+ endif ()
32+
33+ # Only generate if we absolutely have to
34+ message (STATUS "🔄 Generating type combinations (first time only)..." )
35+
36+ # Ensure type system is initialized
37+ if (NOT DEFINED SD_COMMON_TYPES_COUNT OR SD_COMMON_TYPES_COUNT EQUAL 0)
38+ validate_and_process_types_failfast()
39+ build_indexed_type_lists()
40+ endif ()
41+
42+ initialize_dynamic_combinations()
43+
44+ # Verify combinations were created
45+ if (DEFINED COMBINATIONS_2 AND DEFINED COMBINATIONS_3)
46+ list (LENGTH COMBINATIONS_2 combo2_count)
47+ list (LENGTH COMBINATIONS_3 combo3_count)
48+ message (STATUS "✅ Generated combinations: 2-type=${combo2_count} , 3-type=${combo3_count} " )
49+
50+ # Cache for future use
51+ save_combinations_to_cache("${COMBO_CACHE_FILE} " )
52+ else ()
53+ message (FATAL_ERROR "❌ Failed to generate type combinations!" )
54+ endif ()
55+ endfunction ()
56+
57+ # Save combinations to cache file
58+ function (save_combinations_to_cache cache_file)
59+ message (STATUS "💾 Saving combinations to cache..." )
60+
61+ set (cache_content "# Generated type combinations cache\n " )
62+ string (APPEND cache_content "# Generated on: ${CMAKE_CURRENT_TIMESTAMP} \n\n " )
63+
64+ # Save 2-type combinations
65+ string (APPEND cache_content "set(COMBINATIONS_2 \" " )
66+ foreach (combo ${COMBINATIONS_2} )
67+ string (APPEND cache_content "${combo} ;" )
68+ endforeach ()
69+ string (APPEND cache_content "\" CACHE INTERNAL \" 2-type combinations\" )\n\n " )
70+
71+ # Save 3-type combinations
72+ string (APPEND cache_content "set(COMBINATIONS_3 \" " )
73+ foreach (combo ${COMBINATIONS_3} )
74+ string (APPEND cache_content "${combo} ;" )
75+ endforeach ()
76+ string (APPEND cache_content "\" CACHE INTERNAL \" 3-type combinations\" )\n\n " )
77+
78+ # Save metadata
79+ string (APPEND cache_content "set(COMBINATIONS_CACHE_TIMESTAMP \" ${CMAKE_CURRENT_TIMESTAMP} \" CACHE INTERNAL \" Cache timestamp\" )\n " )
80+ string (APPEND cache_content "set(COMBINATIONS_CACHE_VALID TRUE CACHE INTERNAL \" Cache validity flag\" )\n " )
81+
82+ file (WRITE "${cache_file} " "${cache_content} " )
83+ message (STATUS "💾 Combinations cached to: ${cache_file} " )
84+ endfunction ()
85+
86+ # Generate reusable combination strings once
87+ function (generate_combination_strings_once)
88+ if (DEFINED GLOBAL_COMB2_STRING AND DEFINED GLOBAL_COMB3_STRING)
89+ message (STATUS "♻️ Reusing existing combination strings" )
90+ return ()
91+ endif ()
92+
93+ message (STATUS "🔧 Generating combination strings..." )
94+
95+ # Generate 2-type combination string
96+ set (COMB2_STRING "" )
97+ foreach (combination ${COMBINATIONS_2} )
98+ string (REPLACE "," ";" combo_parts "${combination} " )
99+ list (GET combo_parts 0 t1)
100+ list (GET combo_parts 1 t2)
101+ string (APPEND COMB2_STRING "INSTANTIATE_2(${t1} , ${t2} )" )
102+ endforeach ()
103+
104+ # Generate 3-type combination string
105+ set (COMB3_STRING "" )
106+ foreach (combination ${COMBINATIONS_3} )
107+ string (REPLACE "," ";" combo_parts "${combination} " )
108+ list (GET combo_parts 0 t1)
109+ list (GET combo_parts 1 t2)
110+ list (GET combo_parts 2 t3)
111+ string (APPEND COMB3_STRING "INSTANTIATE_3(${t1} , ${t2} , ${t3} ); " )
112+ endforeach ()
113+
114+ # Cache globally for reuse
115+ set (GLOBAL_COMB2_STRING "${COMB2_STRING} " CACHE INTERNAL "Global 2-type combination string" )
116+ set (GLOBAL_COMB3_STRING "${COMB3_STRING} " CACHE INTERNAL "Global 3-type combination string" )
117+
118+ list (LENGTH COMBINATIONS_2 combo2_count)
119+ list (LENGTH COMBINATIONS_3 combo3_count)
120+ message (STATUS "✅ Generated strings: 2-type=${combo2_count} instantiations, 3-type=${combo3_count} instantiations" )
121+ endfunction ()
122+
123+ # Clear cache if needed (for development)
124+ function (clear_combination_cache)
125+ set (COMBO_CACHE_FILE "${CMAKE_BINARY_DIR} /type_combinations.cache" )
126+ if (EXISTS "${COMBO_CACHE_FILE} " )
127+ file (REMOVE "${COMBO_CACHE_FILE} " )
128+ message (STATUS "🗑️ Cleared combination cache" )
129+ endif ()
130+
131+ # Clear memory cache
132+ unset (COMBINATIONS_2 CACHE )
133+ unset (COMBINATIONS_3 CACHE )
134+ unset (GLOBAL_COMB2_STRING CACHE )
135+ unset (GLOBAL_COMB3_STRING CACHE )
136+ unset (COMBINATIONS_CACHE_VALID CACHE )
137+ endfunction ()
138+
139+ # Validate cache integrity
140+ function (validate_combination_cache)
141+ if (NOT DEFINED COMBINATIONS_CACHE_VALID OR NOT COMBINATIONS_CACHE_VALID)
142+ message (STATUS "⚠️ Combination cache is invalid, will regenerate" )
143+ clear_combination_cache()
144+ return ()
145+ endif ()
146+
147+ # Check if combinations are reasonable
148+ if (DEFINED COMBINATIONS_2 AND DEFINED COMBINATIONS_3)
149+ list (LENGTH COMBINATIONS_2 combo2_count)
150+ list (LENGTH COMBINATIONS_3 combo3_count)
151+
152+ if (combo2_count LESS 10 OR combo3_count LESS 10)
153+ message (STATUS "⚠️ Combination counts seem too low, will regenerate" )
154+ clear_combination_cache()
155+ return ()
156+ endif ()
157+
158+ message (STATUS "✅ Cache validation passed" )
159+ else ()
160+ message (STATUS "⚠️ Combinations not found in cache, will regenerate" )
161+ clear_combination_cache()
162+ endif ()
163+ endfunction ()
164+
165+ # Main setup function
166+ function (setup_smart_combination_reuse)
167+ message (STATUS "=== SETTING UP SMART COMBINATION REUSE ===" )
168+
169+ # Validate existing cache
170+ validate_combination_cache()
171+
172+ # Use cached combinations without regeneration
173+ use_cached_type_combinations()
174+
175+ # Create combination strings once, reuse everywhere
176+ generate_combination_strings_once()
177+
178+ message (STATUS "✅ Smart combination reuse setup complete" )
179+ endfunction ()
0 commit comments