Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class RxJavaStrategy extends AbstractStrategy<Strategy.ProviderStrategy> implements Strategy.ProviderStrategy {
- @Override
- public <C> void apply(final Bytecode<C> bytecode) {
- if (bytecode.getParent().isEmpty()) { // root bytecode
- final String id = UUID.randomUUID().toString();
- bytecode.addSourceInstruction(RxJavaProcessor.RX_BYCODE_ID, id);
- } else if (!BytecodeUtil.hasSourceInstruction(bytecode, CommonCompiler.Symbols.WITH_PROCESSOR)) {
- if (RxJavaStrategy.isSimple(bytecode)) {
- bytecode.addSourceInstruction(CommonCompiler.Symbols.WITH_PROCESSOR, RxJavaProcessor.class, Map.of(RxJavaProcessor.RXJAVA_THREADS, 0)); // guaranteed serial execution
- } else {
- final Bytecode<C> root = BytecodeUtil.getRootBytecode(bytecode);
- final List<SourceInstruction> processors = BytecodeUtil.getSourceInstructions(root, CommonCompiler.Symbols.WITH_PROCESSOR); // potential parallel execution
- for (final SourceInstruction sourceInstruction : processors) {
- bytecode.addSourceInstruction(sourceInstruction.op(), sourceInstruction.args());
- }
- }
- }
- }
- private static boolean isSimple(final Bytecode<?> bytecode) {
- final CompositeCompiler compiler = BytecodeUtil.getCompilers(bytecode);
- return bytecode.getInstructions().size() < 4 && bytecode.getInstructions().stream().noneMatch(i -> {
- final FunctionType functionType = compiler.getFunctionType(i.op());
- return FunctionType.FLATMAP == functionType || FunctionType.BRANCH == functionType;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement