Skip to content

Commit

Permalink
Merge pull request #1684 from uqbar-project/fix-#1370-performance-int…
Browse files Browse the repository at this point in the history
…erpreter

Fix #1370 performance interpreter
  • Loading branch information
fdodino committed Jul 21, 2019
2 parents 472ace2 + 29e5313 commit 0096aec
Show file tree
Hide file tree
Showing 38 changed files with 460 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class WollokLauncherInterpreterEvaluator extends WollokInterpreterEvaluator {
if (main !== null)
main.eval
else {
val time = System.currentTimeMillis
val _isASuite = isASuite
var testsToRun = tests
var String suiteName = null
Expand All @@ -54,7 +55,7 @@ class WollokLauncherInterpreterEvaluator extends WollokInterpreterEvaluator {
}
]
} finally {
wollokTestsReporter.finished
wollokTestsReporter.finished(System.currentTimeMillis - time)
}
}
}
Expand Down Expand Up @@ -87,17 +88,15 @@ class WollokLauncherInterpreterEvaluator extends WollokInterpreterEvaluator {
override dispatch evaluate(WTest test) {
try {
test.elements.forEach [ expr |
interpreter.performOnStack(expr, currentContext) [ |
expr.eval
]
interpreter.performOnStack(expr, currentContext) [ | expr.eval ]
]
wollokTestsReporter.reportTestOk(test)
null
} catch (Exception e) {
handleExceptionInTest(e, test)
}
}

protected def WollokObject handleExceptionInTest(Exception e, WTest test) {
if (e.isAssertionException) {
wollokTestsReporter.reportTestAssertError(test, e.generateAssertionError, e.lineNumber, e.URI)
Expand Down Expand Up @@ -139,7 +138,7 @@ class SuiteBuilder {
if (test !== null) {
// Now, declaring test local variables as suite wko instance variables
test.variableDeclarations.forEach[ variable |
suiteObject.addMember(variable)
suiteObject.addMember(variable, false)
]
}
suiteObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DefaultWollokTestsReporter implements WollokTestsReporter {
throw exception
}

override finished() {
override finished(long millisecondsElapsed) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import wollok.lib.AssertionException
import static org.fusesource.jansi.Ansi.*
import static org.fusesource.jansi.Ansi.Color.*

import static extension org.uqbar.project.wollok.utils.StringUtils.*
import static extension org.uqbar.project.wollok.errorHandling.WollokExceptionExtensions.*

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ class WollokConsoleTestsReporter implements WollokTestsReporter {
)
}

override finished() {
override finished(long millisecondsElapsed) {
val STATUS = if (processWasOK) GREEN else RED
println(ansi
.fg(STATUS)
Expand All @@ -75,6 +76,8 @@ class WollokConsoleTestsReporter implements WollokTestsReporter {
.a(testsFailed).a(if (testsFailed == 1) " failure and " else " failures and ")
.a(testsErrored).a(if (testsErrored == 1) " error" else " errors")
.a("\n")
.a("Total time: ").a(millisecondsElapsed.asSeconds).a(" seconds")
.a("\n")
.reset
)
AnsiConsole.systemUninstall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class WollokRemoteTestReporter implements WollokTestsReporter {
@Inject
var WollokLauncherParameters parameters

var Client client
Client client
var callHandler = new CallHandler
var WollokRemoteUITestNotifier remoteTestNotifier
WollokRemoteUITestNotifier remoteTestNotifier
val testsResult = new LinkedList<WollokResultTestDTO>
var boolean processingManyFiles
var String folder
boolean processingManyFiles
String folder
long initialTime

String suiteName
List<WollokTestInfo> testFiles
Expand Down Expand Up @@ -80,21 +81,22 @@ class WollokRemoteTestReporter implements WollokTestsReporter {
resource?.toString))
}

override finished() {
override finished(long timeElapsedInMilliseconds) {
if (!processingManyFiles) {
remoteTestNotifier.testsResult(testsResult)
remoteTestNotifier.testsResult(testsResult, timeElapsedInMilliseconds)
}
}

override initProcessManyFiles(String folder) {
this.processingManyFiles = true
this.folder = folder
this.initialTime = System.currentTimeMillis
}

override endProcessManyFiles() {
remoteTestNotifier => [
testsToRun(suiteName, "", this.testFiles, true)
testsResult(testsResult)
testsResult(testsResult, (System.currentTimeMillis - this.initialTime))
]
processingManyFiles = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface WollokRemoteUITestNotifier {

def void error(String testName, String exceptionAsString, StackTraceElementDTO[] stackTrace, int lineNumber, String resource)

def void testsResult(List<WollokResultTestDTO> resultTests)
def void testsResult(List<WollokResultTestDTO> resultTests, long millisecondsElapsed)

def void showFailuresAndErrorsOnly(boolean showFailuresAndErrors)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface WollokTestsReporter {
/**
* Tells this reporter that the execution has finished
*/
def void finished()
def void finished(long millisecondsElapsed)

/** Just for processing many files */
def void initProcessManyFiles(String folder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WollokJSONTestsReporter implements WollokTestsReporter {
]
}

override finished() {
override finished(long millisecondsElapsed) {
writer => [
endArray
writeSummary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WollokServerTestsReporter extends WollokJSONTestsReporter {
writer.name("tests").beginArray
}

override finished() {
override finished(long millisecondsElapsed) {
writer => [
endArray
writeSummary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.uqbar.project.wollok.lib
import org.uqbar.project.wollok.interpreter.core.WollokObject
import wollok.lang.Closure
import wollok.lang.WList
import wollok.lang.WSet

import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*
import static org.uqbar.project.wollok.sdk.WollokSDK.*
Expand All @@ -17,11 +18,12 @@ import static org.uqbar.project.wollok.sdk.WollokSDK.*
class WollokSDKExtensions {

// conversion to natives shortcuts

def static asString(WollokObject it) { wollokToJava(String) as String }
def static asClosure(WollokObject it) { getNativeObject(CLOSURE) as Closure }
def static asList(WollokObject it) { getNativeObject(LIST) as WList }
def static asSet(WollokObject it) { getNativeObject(SET) as WSet }

def static asVisual(WollokObject it) { new WVisual(it) }
def static asVisualIn(WollokObject it, WollokObject position) { new WVisual(it, position) }

}
45 changes: 43 additions & 2 deletions org.uqbar.project.wollok.lib/src/wollok/lang.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,16 @@ class Collection {
method flatten() = self.flatMap { e => e }

/** @private */
override method internalToSmartString(alreadyShown) =
self.toStringPrefix() + self.map{ e => e.toSmartString(alreadyShown) }.join(', ') + self.toStringSuffix()
/*
* Optimized version for long collections
*
* @see Object#toString()
*/
override method internalToSmartString(alreadyShown) {
const size = self.size()
const internalCollection = if (size > 50) "..." + size + " elements" else self.map{ e => e.toSmartString(alreadyShown) }.join(", ")
return self.toStringPrefix() + internalCollection + self.toStringSuffix()
}

/** @private */
method toStringPrefix()
Expand Down Expand Up @@ -888,6 +896,17 @@ class Set inherits Collection {
*/
override method fold(initialValue, closure) native

/**
* @see Collection#filter(closure)
*/
override method filter(closure) native


/**
* @see Collection#max()
*/
override method max() native

/**
* Tries to find an element in a collection (based on a closure) or
* applies a continuation closure.
Expand Down Expand Up @@ -956,6 +975,12 @@ class Set inherits Collection {
*/
override method join() native

/**
*
* @see List#contains(other)
*/
override method contains(other) native

/**
* Two sets are equals if they have the same elements
*
Expand All @@ -976,6 +1001,7 @@ class Set inherits Collection {
* @see Object#==
*/
override method ==(other) native

}

/**
Expand Down Expand Up @@ -1158,6 +1184,21 @@ class List inherits Collection {
*/
method reverse() = self.subList(self.size() - 1, 0)

/**
* @see Collection#filter(closure)
*/
override method filter(closure) native

/**
* @see Collection#contains(obj)
*/
override method contains(obj) native

/**
* @see Collection#max()
*/
override method max() native

// REFACTORME: DUP METHODS
/**
* Reduce a collection to a certain value, beginning with a seed or initial value
Expand Down
2 changes: 1 addition & 1 deletion org.uqbar.project.wollok.lib/src/wollok/lang/Closure.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Closure implements NodeAware<WClosure>, Function1<WollokObject, Object> {
this.container = interpreter.currentContext
}

def closure() {
def closure() {
EObject
}

Expand Down
47 changes: 42 additions & 5 deletions org.uqbar.project.wollok.lib/src/wollok/lang/WCollection.xtend
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package wollok.lang

import java.util.ArrayList
import java.util.Collection
import org.eclipse.xtend.lib.annotations.Accessors
import org.uqbar.project.wollok.interpreter.api.WollokInterpreterAccess
import org.uqbar.project.wollok.interpreter.core.WCallable
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.nativeobj.JavaWrapper
import org.uqbar.project.wollok.interpreter.nativeobj.NativeMessage

import static org.uqbar.project.wollok.sdk.WollokSDK.*

import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*
import static extension org.uqbar.project.wollok.lib.WollokSDKExtensions.*
import java.util.ArrayList

/**
* @author jfernandes
Expand All @@ -26,6 +29,38 @@ class WCollection<T extends Collection<WollokObject>> {
]
}

/**
* @author: dodain
*
* For performance reasons I had to use C-ish syntax, which resulted
* in a much better performance ratio.
*/
def filter(WollokObject objClosure) {
val closure = objClosure.asClosure
val wrappedAsList = wrapped.toArray
val Collection<WollokObject> result = newArrayList
for (var i = 0; i < wrapped.size; i++) {
val element = wrappedAsList.get(i) as WollokObject
if ((closure.doApply(element).getNativeObject(BOOLEAN) as JavaWrapper<Boolean>).wrapped) {
result.add(element)
}
}
result
}

/**
* @author dodain
*
* Optimized implementation
*/
def contains(WollokObject obj) {
for (var i = 0; i < wrapped.size; i++) {
val element = wrapped.get(i)
if (element.wollokEquals(obj)) return true
}
return false
}

def Object findOrElse(WollokObject _predicate, WollokObject _continuation) {
val predicate = _predicate.asClosure
val continuation = _continuation.asClosure
Expand All @@ -38,7 +73,9 @@ class WCollection<T extends Collection<WollokObject>> {
continuation.doApply()
}

def void add(WollokObject e) { wrapped.add(e) }
def void add(WollokObject e) {
wrapped.add(e)
}

def void remove(WollokObject e) {
// This is necessary because native #contains does not take into account Wollok object equality
Expand Down Expand Up @@ -71,13 +108,13 @@ class WCollection<T extends Collection<WollokObject>> {
hasNativeType(this.class.name)
}

protected def Collection<?> getNativeCollection(WollokObject it) {
protected def Collection<WollokObject> getNativeCollection(WollokObject it) {
getNativeObject(this.class).getWrapped()
}

protected def verifySizes(Collection<?> col, Collection<?> col2) {
protected def verifySizes(Collection<WollokObject> col, Collection<WollokObject> col2) {
col.size.equals(col2.size)
}

protected def verifyWollokElementsContained(Collection<?> set, Collection<?> set2) { false } // Abstract method
protected def verifyWollokElementsContained(Collection<WollokObject> set, Collection<WollokObject> set2) { false } // Abstract method
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import org.uqbar.project.wollok.interpreter.nativeobj.JavaWrapper
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*
import static extension org.uqbar.project.wollok.lib.WollokSDKExtensions.*

class WDictionary implements JavaWrapper<Map> {
class WDictionary implements JavaWrapper<Map<WollokObject, WollokObject>> {

val WollokObject wollokInstance
@Accessors var Map wrapped
@Accessors var Map<WollokObject, WollokObject> wrapped
protected extension WollokInterpreterAccess = new WollokInterpreterAccess

new(WollokObject o) {
Expand Down Expand Up @@ -50,12 +50,12 @@ class WDictionary implements JavaWrapper<Map> {
convertToList(wrapped.values)
}

def convertToList(Collection _value) {
def convertToList(Collection<WollokObject> _value) {
_value.map [ it.javaToWollok ].toList
}

def void forEach(WollokObject proc) {
wrapped.entrySet.sortBy [ toString ].forEach [ Entry entry |
wrapped.entrySet.sortBy [ toString ].forEach [ Entry<WollokObject, WollokObject> entry |
val c = proc.asClosure
c.doApply(entry.key.javaToWollok, entry.value.javaToWollok)
]
Expand Down
Loading

0 comments on commit 0096aec

Please sign in to comment.