@@ -168,14 +168,14 @@ public static function lookupLength(mixed $base, bool $strict = false): mixed
168168 public static function createContext (mixed $ context , array $ options , array $ compiledPartials ): RuntimeContext
169169 {
170170 $ parentCx = $ options ['_cx ' ] ?? null ;
171+ $ data = $ options ['data ' ] ?? [];
171172
172173 if ($ parentCx !== null ) {
173174 // Partial context: reuse the parent's already-merged helpers and partials directly.
174175 // PHP copy-on-write ensures inlinePartials is only copied if the partial registers a new {{#* inline}} partial.
175176 // invokePartial() always passes the caller's current data frame via options, so partials inherit @index, @key, etc.
176177 // Unset 'root' first to break the reference established by `$in = &$cx->data['root']` in the
177178 // calling template; a direct assignment would write through it and corrupt the caller's $in.
178- $ data = $ options ['data ' ] ?? [];
179179 unset($ data ['root ' ]);
180180 $ data ['root ' ] = $ context ;
181181 return new RuntimeContext (
@@ -187,11 +187,9 @@ public static function createContext(mixed $context, array $options, array $comp
187187 );
188188 }
189189
190- $ data = $ options ['data ' ] ?? [];
191190 $ data ['root ' ] ??= $ context ;
192- $ extraHelpers = $ options ['helpers ' ] ?? [];
193191 return new RuntimeContext (
194- helpers: $ extraHelpers ? array_replace (Runtime::defaultHelpers (), $ extraHelpers ) : Runtime:: defaultHelpers ( ),
192+ helpers: array_replace (Runtime::defaultHelpers (), $ options [ ' helpers ' ] ?? [] ),
195193 partials: array_replace ($ compiledPartials , $ options ['partials ' ] ?? []),
196194 data: $ data ,
197195 );
@@ -316,9 +314,15 @@ public static function escapeExpression(mixed $var): string
316314
317315 /**
318316 * Get string representation for output
317+ *
318+ * This function has been optimized for JIT via a microbenchmark with common inputs.
319319 */
320320 public static function raw (mixed $ value ): string
321321 {
322+ if (is_string ($ value )) {
323+ return $ value ;
324+ }
325+
322326 if ($ value === true ) {
323327 return 'true ' ;
324328 }
@@ -437,16 +441,11 @@ public static function invokePartial(RuntimeContext $cx, ?string $name, mixed $c
437441 }
438442
439443 if ($ indent !== '' ) {
440- $ lines = explode ("\n" , $ result );
441- $ lastIdx = count ($ lines ) - 1 ;
442- foreach ($ lines as $ i => &$ line ) {
443- if ($ line === '' && $ i === $ lastIdx ) {
444- break ;
445- }
446- $ line = $ indent . $ line ;
444+ if (str_ends_with ($ result , "\n" )) {
445+ $ result = $ indent . str_replace ("\n" , "\n" . $ indent , substr ($ result , 0 , -1 )) . "\n" ;
446+ } elseif ($ result !== '' ) {
447+ $ result = $ indent . str_replace ("\n" , "\n" . $ indent , $ result );
447448 }
448- unset($ line );
449- $ result = implode ("\n" , $ lines );
450449 }
451450
452451 return $ result ;
0 commit comments