Skip to content

Commit

Permalink
Merge pull request #57 from kares/less-garbage
Browse files Browse the repository at this point in the history
[refactor] to generate less garbage (from JRuby ext)
  • Loading branch information
bklang committed Sep 19, 2019
2 parents 7c28b03 + fdc455b commit b14a166
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions ext/ruby_speech/RubySpeechGRXMLMatcher.java
Expand Up @@ -6,6 +6,7 @@
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
Expand Down Expand Up @@ -43,22 +44,26 @@ public IRubyObject find_match(ThreadContext context, IRubyObject buffer)
}
return callMethod(context, "match_for_buffer", buffer);
} else if (m.hitEnd()) {
RubyModule potential_match = runtime.getClassFromPath("RubySpeech::GRXML::PotentialMatch");
return potential_match.callMethod(context, "new");
return getGRXMLType(runtime, "PotentialMatch").newInstance(context, Block.NULL_BLOCK);
}
RubyModule nomatch = runtime.getClassFromPath("RubySpeech::GRXML::NoMatch");
return nomatch.callMethod(context, "new");
return getGRXMLType(runtime, "NoMatch").newInstance(context, Block.NULL_BLOCK);
}

public Boolean is_max_match(String buffer)
{
String search_set = "0123456789#*ABCD";
for (int i = 0; i < 16; i++) {
String new_buffer = buffer + search_set.charAt(i);
Matcher m = p.matcher(new_buffer);
if (m.matches()) return false;
private boolean is_max_match(String buffer) {
final int len = buffer.length();
StringBuilder new_buffer = new StringBuilder(len + 1);
new_buffer.append(buffer).append('\0');
final String search_set = "0123456789#*ABCD";
for (int i = 0; i < search_set.length(); i++) {
new_buffer.setCharAt(len, search_set.charAt(i));
if (p.matcher(new_buffer).matches()) return false;
}
return true;
}

private static RubyClass getGRXMLType(final Ruby runtime, final String name) {
RubyModule grxml = (RubyModule) runtime.getModule("RubySpeech").getConstantAt("GRXML");
return (RubyClass) grxml.getConstantAt(name);
}

}
2 changes: 1 addition & 1 deletion lib/ruby_speech/grxml/matcher.rb
Expand Up @@ -90,7 +90,7 @@ def initialize(grammar)
# ```
#
def match(buffer)
find_match buffer.dup
find_match buffer
end

private
Expand Down

0 comments on commit b14a166

Please sign in to comment.