This is an automated email from the ASF dual-hosted git repository.

Cole-Greer pushed a commit to branch docs-3.7
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit d9ba2a866ed6a68207b579a34d62e5bac40b7799
Author: Cole Greer <[email protected]>
AuthorDate: Mon May 25 10:35:59 2026 -0700

    Fix CodeRay performance: use callMethod instead of evalScriptlet
    
    evalScriptlet with embedded source code forced JRuby to parse a new
    Ruby script for every highlight call (~970 calls), taking 25-30s each.
    Now uses callMethod to invoke the cached CodeRay Duo object directly,
    passing source as a RubyString argument. Dry-run drops from hours to
    16 seconds.
---
 .../gremlin/docs/GremlinTreeprocessor.java          | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git 
a/tools/tinkerpop-docs/src/main/java/org/apache/tinkerpop/gremlin/docs/GremlinTreeprocessor.java
 
b/tools/tinkerpop-docs/src/main/java/org/apache/tinkerpop/gremlin/docs/GremlinTreeprocessor.java
index 0b524b0df3..e3564eaf92 100644
--- 
a/tools/tinkerpop-docs/src/main/java/org/apache/tinkerpop/gremlin/docs/GremlinTreeprocessor.java
+++ 
b/tools/tinkerpop-docs/src/main/java/org/apache/tinkerpop/gremlin/docs/GremlinTreeprocessor.java
@@ -316,17 +316,20 @@ public class GremlinTreeprocessor extends Treeprocessor {
         return result.toString();
     }
 
+    private org.jruby.runtime.builtin.IRubyObject coderayEncoder;
+    private org.jruby.Ruby rubyRuntime;
+
     private String doHighlight(final StructuralNode parent, final String lang, 
final String source) {
         try {
-            final org.jruby.Ruby ruby = 
org.asciidoctor.jruby.internal.JRubyRuntimeContext.get(parent);
-            if (ruby == null) return escapeHtml(source);
-            // Initialize CodeRay encoder once
-            ruby.evalScriptlet("require 'coderay' unless defined?(CodeRay); " +
-                    "$tp_coderay_groovy ||= CodeRay::Duo[:groovy, :html, :css 
=> :class]");
-            // Use heredoc to avoid escaping issues
-            final String marker = "TPDOC" + System.identityHashCode(source);
-            final String script = "$tp_coderay_groovy.highlight(<<'" + marker 
+ "'.chomp\n" + source + "\n" + marker + "\n)";
-            final org.jruby.runtime.builtin.IRubyObject result = 
ruby.evalScriptlet(script);
+            if (rubyRuntime == null) {
+                rubyRuntime = 
org.asciidoctor.jruby.internal.JRubyRuntimeContext.get(parent);
+                if (rubyRuntime == null) return escapeHtml(source);
+                coderayEncoder = rubyRuntime.evalScriptlet(
+                        "require 'coderay'; CodeRay::Duo[:groovy, :html, :css 
=> :class]");
+            }
+            final org.jruby.RubyString rubySource = 
org.jruby.RubyString.newString(rubyRuntime, source);
+            final org.jruby.runtime.builtin.IRubyObject result = 
coderayEncoder.callMethod(
+                    rubyRuntime.getCurrentContext(), "highlight", rubySource);
             return result != null ? result.asJavaString() : escapeHtml(source);
         } catch (final Exception e) {
             LOG.warning("CodeRay highlighting failed, falling back to plain: " 
+ e.getMessage());

Reply via email to