Skip to content

Commit ac6ae9e

Browse files
committed
Move call sites to a data structure
* Reduced size of parser * Reduced init cost of parser * Slightly more indirection
1 parent 8caff50 commit ac6ae9e

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

ext/java/org/jruby/ext/psych/PsychParser.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ public class PsychParser extends RubyObject {
103103
public static void initPsychParser(Ruby runtime, RubyModule psych) {
104104
RubyClass psychParser = runtime.defineClassUnder("Parser", runtime.getObject(), PsychParser::new, psych);
105105

106-
CachingCallSite[] sites =
107-
Arrays.stream(Call.values())
108-
.map((call) -> new FunctionalCachingCallSite(call.name()))
109-
.toArray(CachingCallSite[]::new);
110-
psychParser.setInternalVariable(JRUBY_CALL_SITES, sites);
106+
psychParser.setInternalVariable(JRUBY_CALL_SITES, new CallSites());
111107

112108
runtime.getLoadService().require("psych/syntax_error");
113109
psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
@@ -128,19 +124,7 @@ public static void initPsychParser(Ruby runtime, RubyModule psych) {
128124
public PsychParser(Ruby runtime, RubyClass klass) {
129125
super(runtime, klass);
130126

131-
CachingCallSite[] sites = (CachingCallSite[]) klass.getInternalVariable(JRUBY_CALL_SITES);
132-
this.path = sites[Call.path.ordinal()];
133-
this.event_location = sites[Call.event_location.ordinal()];
134-
this.start_stream = sites[Call.start_stream.ordinal()];
135-
this.start_document = sites[Call.start_document.ordinal()];
136-
this.end_document = sites[Call.end_document.ordinal()];
137-
this.alias = sites[Call.alias.ordinal()];
138-
this.scalar = sites[Call.scalar.ordinal()];
139-
this.start_sequence = sites[Call.start_sequence.ordinal()];
140-
this.end_sequence = sites[Call.end_sequence.ordinal()];
141-
this.start_mapping = sites[Call.start_mapping.ordinal()];
142-
this.end_mapping = sites[Call.end_mapping.ordinal()];
143-
this.end_stream = sites[Call.end_stream.ordinal()];
127+
this.sites = (CallSites) klass.getInternalVariable(JRUBY_CALL_SITES);
144128

145129
// prepare settings builder and apply global defaults
146130
LoadSettingsBuilder lsb = LoadSettings.builder();
@@ -257,7 +241,7 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
257241
parser = new ParserImpl(loadSettings, new ScannerImpl(loadSettings, readerFor(context, yaml, loadSettings)));
258242

259243
if (path.isNil() && yaml.respondsTo("path")) {
260-
path = this.path.call(context, this, yaml);
244+
path = sites.path.call(context, this, yaml);
261245
}
262246

263247
while (parser.hasNext()) {
@@ -271,24 +255,24 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
271255
IRubyObject end_line = runtime.newFixnum(end.getLine());
272256
IRubyObject end_column = runtime.newFixnum(end.getColumn());
273257

274-
event_location.call(context, this, handler, start_line, start_column, end_line, end_column);
258+
sites.event_location.call(context, this, handler, start_line, start_column, end_line, end_column);
275259

276260
switch (event.getEventId()) {
277261
case StreamStart:
278-
start_stream.call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
262+
sites.start_stream.call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
279263
break;
280264
case DocumentStart:
281265
handleDocumentStart(context, (DocumentStartEvent) event, handler);
282266
break;
283267
case DocumentEnd:
284268
IRubyObject notExplicit = runtime.newBoolean(!((DocumentEndEvent) event).isExplicit());
285269

286-
end_document.call(context, this, handler, notExplicit);
270+
sites.end_document.call(context, this, handler, notExplicit);
287271
break;
288272
case Alias:
289273
IRubyObject alias = stringOrNilForAnchor(context, ((AliasEvent) event).getAnchor());
290274

291-
this.alias.call(context, this, handler, alias);
275+
sites.alias.call(context, this, handler, alias);
292276
break;
293277
case Scalar:
294278
handleScalar(context, (ScalarEvent) event, handler);
@@ -297,16 +281,16 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
297281
handleSequenceStart(context, (SequenceStartEvent) event, handler);
298282
break;
299283
case SequenceEnd:
300-
end_sequence.call(context, this, handler);
284+
sites.end_sequence.call(context, this, handler);
301285
break;
302286
case MappingStart:
303287
handleMappingStart(context, (MappingStartEvent) event, handler);
304288
break;
305289
case MappingEnd:
306-
end_mapping.call(context, this, handler);
290+
sites.end_mapping.call(context, this, handler);
307291
break;
308292
case StreamEnd:
309-
end_stream.call(context, this, handler);
293+
sites.end_stream.call(context, this, handler);
310294
break;
311295
}
312296
}
@@ -369,7 +353,7 @@ private void handleDocumentStart(ThreadContext context, DocumentStartEvent dse,
369353

370354
IRubyObject notExplicit = runtime.newBoolean(!dse.isExplicit());
371355

372-
start_document.call(context, this, handler, version, tags, notExplicit);
356+
sites.start_document.call(context, this, handler, version, tags, notExplicit);
373357
}
374358

375359
private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IRubyObject handler) {
@@ -379,7 +363,7 @@ private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IR
379363
IRubyObject implicit = runtime.newBoolean(mse.isImplicit());
380364
IRubyObject style = runtime.newFixnum(translateFlowStyle(mse.getFlowStyle()));
381365

382-
start_mapping.call(context, this, handler, anchor, tag, implicit, style);
366+
sites.start_mapping.call(context, this, handler, anchor, tag, implicit, style);
383367
}
384368

385369
private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject handler) {
@@ -393,7 +377,7 @@ private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject han
393377
IRubyObject style = runtime.newFixnum(translateStyle(se.getScalarStyle()));
394378
IRubyObject val = stringFor(context, se.getValue());
395379

396-
scalar.call(context, this, handler, val, anchor, tag, plain_implicit,
380+
sites.scalar.call(context, this, handler, val, anchor, tag, plain_implicit,
397381
quoted_implicit, style);
398382
}
399383

@@ -404,7 +388,7 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
404388
IRubyObject implicit = runtime.newBoolean(sse.isImplicit());
405389
IRubyObject style = runtime.newFixnum(translateFlowStyle(sse.getFlowStyle()));
406390

407-
start_sequence.call(context, this, handler, anchor, tag, implicit, style);
391+
sites.start_sequence.call(context, this, handler, anchor, tag, implicit, style);
408392
}
409393

410394
private static void raiseParserException(ThreadContext context, ReaderException re, IRubyObject rbPath) {
@@ -656,10 +640,21 @@ private LoadSettings buildSettings() {
656640
private Parser parser;
657641
private Event event;
658642
private final LoadSettingsBuilder loadSettingsBuilder;
659-
660-
private enum Call {
661-
path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream
643+
private final CallSites sites;
644+
645+
private static class CallSites {
646+
private final CachingCallSite path = new FunctionalCachingCallSite("path");
647+
private final CachingCallSite event_location = new FunctionalCachingCallSite("event_location");
648+
private final CachingCallSite start_stream = new FunctionalCachingCallSite("start_stream");
649+
private final CachingCallSite start_document = new FunctionalCachingCallSite("start_document");
650+
private final CachingCallSite end_document = new FunctionalCachingCallSite("end_document");
651+
private final CachingCallSite alias = new FunctionalCachingCallSite("alias");
652+
private final CachingCallSite scalar = new FunctionalCachingCallSite("scalar");
653+
private final CachingCallSite start_sequence = new FunctionalCachingCallSite("start_sequence");
654+
private final CachingCallSite end_sequence = new FunctionalCachingCallSite("end_sequence");
655+
private final CachingCallSite start_mapping = new FunctionalCachingCallSite("start_mapping");
656+
private final CachingCallSite end_mapping = new FunctionalCachingCallSite("end_mapping");
657+
private final CachingCallSite end_stream = new FunctionalCachingCallSite("end_stream");
662658
}
663659

664-
private final CachingCallSite path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream;
665660
}

0 commit comments

Comments
 (0)