Skip to content

Commit

Permalink
feat(objectionary#889): speed up DirectivesSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 29, 2024
1 parent 20ebb08 commit 08c1050
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,16 @@ private DirectivesSeq(final String name, final Iterable<Directive>... elements)

@Override
public Iterator<Directive> iterator() {
final List<Directives> all = this.stream()
.map(Directives::new)
.collect(Collectors.toList());
return new DirectivesJeoObject(
String.format("seq.of%d", this.size()),
String.format("seq.of%d", all.size()),
this.name,
this.stream().map(Directives::new).collect(Collectors.toList())
all
).iterator();
}

/**
* Size of the sequence.
* @return Size.
*/
private long size() {
return this.stream().count();
}

/**
* Stream of directives.
* @return Stream of directives.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.jeo.representation.xmir;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ void convertsToXmirAndBack() {
);
}
final long end = System.currentTimeMillis();
final long l = end - start;
Logger.info(
this,
"We made %d attempts to convert bytecode to xmir and back in %[ms]s",
attempts,
l
end - start
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
package org.eolang.jeo.representation.directives;

import com.jcabi.matchers.XhtmlMatchers;
import java.util.stream.Stream;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.xembly.Directives;
import org.xembly.ImpossibleModificationException;
import org.xembly.Xembler;

Expand All @@ -52,4 +57,48 @@ void convertsToNumberedSeq() throws ImpossibleModificationException {
)
);
}

@ParameterizedTest
@MethodSource("sequences")
void correctlyComputesSize(
final DirectivesSeq actual, final int expected
) throws ImpossibleModificationException {
MatcherAssert.assertThat(
"The size of the sequence is not as expected",
new Xembler(actual).xml(),
XhtmlMatchers.hasXPath(
String.format(
"/o[contains(@base,'seq.of%d') and @name='@']",
expected
)
)
);
}

/**
* Sequences to test.
* @return Stream of arguments.
*/
private static Stream<Arguments> sequences() {
return Stream.of(
Arguments.of(
new DirectivesSeq(
new DirectivesValue("1"), new DirectivesValue("2")
),
2
),
Arguments.of(new DirectivesSeq(), 0),
Arguments.of(new DirectivesSeq(new Directives(), new Directives()), 0),
Arguments.of(
new DirectivesSeq(
new DirectivesValue("1"), new Directives(),
new Directives(), new DirectivesValue("4")
),
2
),
Arguments.of(
new DirectivesSeq(), 0
)
);
}
}

0 comments on commit 08c1050

Please sign in to comment.