Skip to content

Commit

Permalink
Improve performance (toImmutable() method).
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Feb 22, 2020
1 parent d201f7f commit 93bde32
Show file tree
Hide file tree
Showing 34 changed files with 183 additions and 118 deletions.
3 changes: 3 additions & 0 deletions bench/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

vendor/
package-lock.php.yml
18 changes: 18 additions & 0 deletions bench/package.php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: bench
version: 1.0.0

plugins: [App]

deps:
jphp-core: '*'
jphp-zend-ext: '*'
jphp-json-ext: '*'

app:
encoding: UTF-8

sources:
- src

includes:
- 'bench.php'
1 change: 1 addition & 0 deletions bench/src/benchmarks/NewObjectBenchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function run()
{
for ($i = 0; $i < self::DEFAULT_ITERATIONS; $i++) {
$o1 = new stdClass();

$o2 = new NewObjectBenchmark_A();
$o3 = new NewObjectBenchmark_B();
$o4 = new NewObjectBenchmark_C();
Expand Down
4 changes: 2 additions & 2 deletions jphp-core/api-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## jphp-core
> version 1.1.1, created by JPPM.
> version 1.1.2, created by JPPM.
Compiler and Launcher for JPHP.

### Install
```
jppm add [email protected].1
jppm add [email protected].2
```

### API
Expand Down
4 changes: 2 additions & 2 deletions jphp-core/api-docs/README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## jphp-core
> версия 1.1.1, создано с помощью JPPM.
> версия 1.1.2, создано с помощью JPPM.
Compiler and Launcher for JPHP.

### Установка
```
jppm add [email protected].1
jppm add [email protected].2
```

### АПИ
Expand Down
4 changes: 2 additions & 2 deletions jphp-core/package.php.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

name: jphp-core
version: 1.1.1
version: 1.1.2
description: Compiler and Launcher for JPHP.

deps:
jphp-runtime: '~1.1.0'
jphp-runtime: '~1.1.3'


plugins: [Doc, Hub]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,9 @@ public void writeSysStaticCall(Class clazz, String method, Class returnClazz, Cl

public void writePopImmutable() {
if (!stackPeek().immutable) {
writeSysDynamicCall(Memory.class, "toImmutable", Memory.class);
writeSysStaticCall(Memory.class, "__static_fast_toImmutable", Memory.class, Memory.class);

//writeSysDynamicCall(Memory.class, "fast_toImmutable", Memory.class);
setStackPeekAsImmutable();
}
}
Expand Down
4 changes: 2 additions & 2 deletions jphp-runtime/api-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## jphp-runtime
> version 1.1.1, created by JPPM.
> version 1.1.3, created by JPPM.
Runtime for JPHP + Standard library.

### Install
```
jppm add [email protected].1
jppm add [email protected].3
```

### API
Expand Down
4 changes: 2 additions & 2 deletions jphp-runtime/api-docs/README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## jphp-runtime
> версия 1.1.1, создано с помощью JPPM.
> версия 1.1.3, создано с помощью JPPM.
Runtime for JPHP + Standard library.

### Установка
```
jppm add [email protected].1
jppm add [email protected].3
```

### АПИ
Expand Down
4 changes: 3 additions & 1 deletion jphp-runtime/package.php.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

name: jphp-runtime
version: 1.1.2
version: 1.1.3
description: Runtime for JPHP + Standard library.

plugins: [Doc, Hub]
Expand All @@ -14,6 +14,8 @@ config:
ignore: ['/package.hub.yml']

history:
1.1.2:
- Improve call stack performance.
1.1.1:
- Improve arrays performance.
1.1.0:
Expand Down
46 changes: 40 additions & 6 deletions jphp-runtime/src/php/runtime/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ public Memory toBinary() {

public Memory toArray() {
ArrayMemory result = new ArrayMemory();
result.add(toImmutable());
result.add(fast_toImmutable());
return result.toConstant();
}

public Memory toObject(Environment env) {
StdClass stdClass = new StdClass(env);
stdClass.getProperties().refOfIndex("scalar").assign(toImmutable());
stdClass.getProperties().refOfIndex("scalar").assign(fast_toImmutable());
return new ObjectMemory(stdClass);
}

Expand Down Expand Up @@ -354,9 +354,9 @@ public Memory pow(long value) {
public Memory pow(boolean value) {
Memory real = toNumeric();
if (real instanceof LongMemory) {
return value ? real.toImmutable() : Memory.CONST_INT_1;
return value ? real.fast_toImmutable() : Memory.CONST_INT_1;
}
return value ? real.toImmutable() : Memory.CONST_DOUBLE_1;
return value ? real.fast_toImmutable() : Memory.CONST_DOUBLE_1;
}
public Memory pow(String value) { return pow(StringMemory.toNumeric(value)); }

Expand Down Expand Up @@ -428,7 +428,7 @@ private static boolean _xor(boolean... args) {
abstract public boolean identical(Memory memory);
public boolean identical(long value) { return type == Type.INT && toLong() == value; }
public boolean identical(double value) { return type == Type.DOUBLE && DoubleMemory.almostEqual(toDouble(), value); }
public boolean identical(boolean value) { return type == Type.BOOL && value ? toImmutable() == TRUE : toImmutable() == FALSE; }
public boolean identical(boolean value) { return type == Type.BOOL && value ? fast_toImmutable() == TRUE : fast_toImmutable() == FALSE; }
public boolean identical(String value) { return type == Type.STRING && toString().equals(value); }

// NOT EQUAL
Expand Down Expand Up @@ -641,8 +641,42 @@ public Memory toImmutable(){
return this;
}

/**
* This method more faster than toImmutable because is not overrided and is final
*/
final public Memory fast_toImmutable() {
switch (type) {
case INT:
case BOOL:
case NULL:
case DOUBLE:
case STRING:
case OBJECT:
return this;
default:
return toImmutable();
}
}

/**
* For compiler.
*/
static public Memory __static_fast_toImmutable(Memory value) {
switch (value.type) {
case INT:
case BOOL:
case NULL:
case DOUBLE:
case STRING:
case OBJECT:
return value;
default:
return value.toImmutable();
}
}

public Memory toImmutable(Environment env, TraceInfo trace){
return toImmutable();
return fast_toImmutable();
}

@SuppressWarnings("unchecked")
Expand Down
18 changes: 16 additions & 2 deletions jphp-runtime/src/php/runtime/env/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,15 +767,15 @@ public ArrayMemory getConfigValues(String prefix, boolean includingGlobal) {
String key = entry.getKey();
if (prefix != null && !key.startsWith(prefix)) continue;

result.put(key, entry.getValue().toImmutable());
result.put(key, entry.getValue().fast_toImmutable());
}
}

for (Map.Entry<String, Memory> entry : configuration.entrySet()) {
String key = entry.getKey();

if (prefix != null && !key.startsWith(prefix)) continue;
result.put(key, entry.getValue().toImmutable());
result.put(key, entry.getValue().fast_toImmutable());
}
return result;
}
Expand Down Expand Up @@ -1578,6 +1578,20 @@ public Memory __throwException(InvocationTargetException e) {
return Memory.NULL;
}

public Memory __throwThrowable(Throwable throwable) {
if (throwable instanceof FinallyException) {
return Memory.NULL;
}

if (throwable instanceof JPHPException)
throw (RuntimeException) throwable;
else {
JavaReflection.exception(this, throwable);
}

return Memory.NULL;
}

public void __throwException(BaseBaseException e) {
__throwException(e, true);
}
Expand Down
8 changes: 4 additions & 4 deletions jphp-runtime/src/php/runtime/ext/core/MathFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ public static Memory max(Memory value, Memory... args){
if (max == null || one.greater(max))
max = one;
}
return max == null ? Memory.NULL : max.toImmutable();
return max == null ? Memory.NULL : max.fast_toImmutable();
} else {
Memory max = value;
if (args != null)
for(Memory one : args){
if (one.greater(max))
max = one;
}
return max.toImmutable();
return max.fast_toImmutable();
}
}

Expand All @@ -221,14 +221,14 @@ public static Memory min(Memory value, Memory... args){
if (min == null || one.smaller(min))
min = one;
}
return min == null ? Memory.NULL : min.toImmutable();
return min == null ? Memory.NULL : min.fast_toImmutable();
} else {
Memory min = value;
for(Memory one : args){
if (one.smaller(min))
min = one;
}
return min.toImmutable();
return min.fast_toImmutable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public static Memory toArray(Environment env, Memory... args) {
ArrayMemory r = new ArrayMemory();
while (iterator.next()) {
if (withKeys)
r.put(iterator.getMemoryKey(), iterator.getValue().toImmutable());
r.put(iterator.getMemoryKey(), iterator.getValue().fast_toImmutable());
else
r.add(iterator.getValue().toImmutable());
r.add(iterator.getValue().fast_toImmutable());
}

return r.toConstant();
Expand Down Expand Up @@ -206,7 +206,7 @@ public static Memory combine(Environment env, Memory... args) {

while (keyIterator.next()) {
if (valueIterator.next()) {
r.refOfIndex(keyIterator.getValue()).assign(valueIterator.getValue().toImmutable());
r.refOfIndex(keyIterator.getValue()).assign(valueIterator.getValue().fast_toImmutable());
} else {
return Memory.NULL;
}
Expand Down Expand Up @@ -276,9 +276,9 @@ public static Memory toList(Environment env, Memory... args) {
if (arg.isTraversable()) {
ForeachIterator iterator = arg.getNewIterator(env);
while (iterator.next())
r.add(iterator.getValue().toImmutable());
r.add(iterator.getValue().fast_toImmutable());
} else
r.add(arg.toImmutable());
r.add(arg.fast_toImmutable());
}
return r.toConstant();
}
Expand Down Expand Up @@ -306,7 +306,7 @@ protected static void flatten(Environment env, ForeachIterator iterator, Set<Int
Memory el = iterator.getValue();
ForeachIterator innerIterator = el.getNewIterator(env);
if (innerIterator == null || (level >= maxLevel && maxLevel > -1)) {
array.add(el.toImmutable());
array.add(el.fast_toImmutable());
} else {
if (used.add(el.getPointer())) {
flatten(env, innerIterator, used, array, level + 1, maxLevel);
Expand Down Expand Up @@ -390,7 +390,7 @@ public static Memory push(Environment env, Memory... args) throws Throwable {
Memory array = args[0];

for (int i = 1; i < args.length; i++) {
array.toValue(ArrayMemory.class).add(args[i].toImmutable());
array.toValue(ArrayMemory.class).add(args[i].fast_toImmutable());
}

return Memory.NULL;
Expand Down Expand Up @@ -431,7 +431,7 @@ public static Memory reverse(Environment env, Memory... args) {
ArrayMemory result = new ArrayMemory();

while (iterator.next()) {
result.unshift(iterator.getValue().toImmutable());
result.unshift(iterator.getValue().fast_toImmutable());
}

return result.toConstant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ synchronized public void __clone(Environment env, TraceInfo trace) throws Throwa
} else if (value.isObject()) {
this.value = value.clone(env, trace);
} else {
this.value = value.toImmutable();
this.value = value.fast_toImmutable();
}
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ public void __construct(ForeachIterator iterator) {
queue = new LinkedList<Memory>();

while (iterator.next()) {
queue.add(iterator.getValue().toImmutable());
queue.add(iterator.getValue().fast_toImmutable());
}
}

Expand Down Expand Up @@ -417,7 +417,7 @@ public void __construct(ForeachIterator iterator) {
stack = new Stack<Memory>();

while (iterator.next()) {
stack.push(iterator.getValue().toImmutable());
stack.push(iterator.getValue().fast_toImmutable());
}
}

Expand Down Expand Up @@ -557,7 +557,7 @@ public void __construct(ForeachIterator iterator) {
map = new LinkedHashMap<String, Memory>();

while (iterator.next()) {
map.put(iterator.getKey().toString(), iterator.getValue().toImmutable());
map.put(iterator.getKey().toString(), iterator.getValue().fast_toImmutable());
}
}

Expand Down
Loading

0 comments on commit 93bde32

Please sign in to comment.