Skip to content

Commit 5d56885

Browse files
committed
Improve array access performance.
1 parent df0997b commit 5d56885

File tree

12 files changed

+102
-65
lines changed

12 files changed

+102
-65
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
innoSetupDir=C:\\Program Files (x86)\\Inno Setup 5
2+
innoSetupDir=C:\\Program Files (x86)\\Inno Setup 6

jphp-core/api-docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

55
## jphp-core
6-
> version 1.1.0, created by JPPM.
6+
> version 1.1.1, created by JPPM.
77
88
Compiler and Launcher for JPHP.
99

1010
### Install
1111
```
12-
12+
1313
```
1414

1515
### API

jphp-core/api-docs/README.ru.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

55
## jphp-core
6-
> версия 1.1.0, создано с помощью JPPM.
6+
> версия 1.1.1, создано с помощью JPPM.
77
88
Compiler and Launcher for JPHP.
99

1010
### Установка
1111
```
12-
12+
1313
```
1414

1515
### АПИ

jphp-core/package.php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
name: jphp-core
3-
version: 1.1.0
3+
version: 1.1.1
44
description: Compiler and Launcher for JPHP.
55

66
deps:
7-
jphp-runtime: '~1.0.3'
7+
jphp-runtime: '~1.1.0'
88

99

1010
plugins: [Doc, Hub]

jphp-core/src/org/develnext/jphp/core/compiler/jvm/statement/ExpressionStmtCompiler.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ public void writePushMemory(Memory memory) {
416416
if (arrayKey instanceof String) {
417417
writePushConstString((String) arrayKey);
418418
writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class, String.class);
419-
} else if (arrayKey instanceof LongMemory) {
420-
writePushConstantMemory((Memory) arrayKey);
421-
writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class, Memory.class);
419+
} else if (arrayKey instanceof Long) {
420+
writePushConstLong((long) arrayKey);
421+
writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class, Long.TYPE);
422422
} else {
423423
writeSysStaticCall(KeyValueMemory.class, "valueOf", Memory.class, Memory.class, Memory.class);
424424
}
@@ -449,16 +449,24 @@ public void writePushMemory(Memory memory) {
449449
writeSysDynamicCall(ArrayMemory.class, "add", ReferenceMemory.class, Memory.class);
450450
} else {
451451
Memory key = foreachIterator.getMemoryKey();
452-
writePushMemory(key);
453-
454-
if (!key.isString()) {
452+
if (key instanceof LongMemory) {
453+
writePushConstLong(key.toLong());
454+
writePushMemory(foreachIterator.getValue());
455455
writePopBoxing();
456-
}
457456

458-
writePushMemory(foreachIterator.getValue());
459-
writePopBoxing();
457+
writeSysDynamicCall(ArrayMemory.class, "put", ReferenceMemory.class, Long.TYPE, Memory.class);
458+
} else {
459+
writePushMemory(key);
460460

461-
writeSysDynamicCall(ArrayMemory.class, "put", ReferenceMemory.class, Object.class, Memory.class);
461+
if (!key.isString()) {
462+
writePopBoxing();
463+
}
464+
465+
writePushMemory(foreachIterator.getValue());
466+
writePopBoxing();
467+
468+
writeSysDynamicCall(ArrayMemory.class, "put", ReferenceMemory.class, Object.class, Memory.class);
469+
}
462470
}
463471

464472
writePopAll(1);

jphp-core/tests/php/runtime/memory/ArrayMemoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ public void testKeySet() {
203203
set = arr.keySet();
204204
setArr = set.toArray();
205205
assertEquals(3, setArr.length);
206-
assertEquals(0, ((Memory)setArr[0]).toInteger());
207-
assertEquals(1, ((Memory)setArr[1]).toInteger());
206+
assertEquals(0L, setArr[0]);
207+
assertEquals(1L, setArr[1]);
208208
assertEquals("3", setArr[2].toString());
209209
}
210210
}

jphp-runtime/package.php.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ config:
1414
ignore: ['/package.hub.yml']
1515

1616
history:
17+
1.1.1:
18+
- Improve arrays performance.
1719
1.1.0:
1820
- Add PHP 7.2 features (ref in list, is_countable, count() warnings).
1921
1.0.10:

jphp-runtime/src/php/runtime/memory/ArrayMemory.java

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -235,26 +235,26 @@ public ArrayMemory checkCopied() {
235235
public static Object toKey(Memory key) {
236236
switch (key.type) {
237237
case STRING: {
238-
String key1 = key.toString();
239-
Memory number = StringMemory.toLong(key1);
240-
if (number == null)
241-
return key1;
242-
else
243-
return number;
238+
if (((StringMemory)key).canBeConvertedToLong()) {
239+
return StringMemory.toLong(key.toString()).toLong();
240+
} else {
241+
return key.toString();
242+
}
244243
}
245244
case INT:
246-
return key;
245+
case DOUBLE:
246+
return key.toLong();
247247
case NULL:
248248
return Memory.CONST_EMPTY_STRING;
249249
case REFERENCE:
250250
return toKey(key.toValue());
251251
default:
252-
return LongMemory.valueOf(key.toLong());
252+
return toKey(StringMemory.valueOf(key.toString()));
253253
}
254254
}
255255

256256
public boolean containsLongKey(long key) {
257-
return containsKey(LongMemory.valueOf(key));
257+
return containsKey(key);
258258
}
259259

260260
public boolean containsKey(Object key) {
@@ -273,10 +273,10 @@ public boolean containsKey(Object key) {
273273
private void convertToMap() {
274274
map = new ArrayMemoryMap();
275275
if (_list != null && !_list.isEmpty()) {
276-
int i = 0;
276+
long i = 0;
277277
for (ReferenceMemory memory : _list) {
278278
if (memory != null) {
279-
map.put(LongMemory.valueOf(i), memory.getValue());
279+
map.put(i, memory.getValue());
280280
}
281281
i++;
282282
}
@@ -343,8 +343,8 @@ public ReferenceMemory getByScalarOrCreate(Object sKey) {
343343

344344
public ReferenceMemory getByScalar(Object key) {
345345
if (_list != null) {
346-
if (key instanceof Memory) {
347-
int index = (int) ((Memory) key).toLong();
346+
if (key instanceof Long) {
347+
int index = ((Long)key).intValue();
348348
if (index >= 0 && index < _list.size()) {
349349
return _list.get(index);
350350
} else
@@ -396,7 +396,7 @@ public ReferenceMemory add(Memory value) {
396396
getList().add(ref);
397397
size++;
398398
} else {
399-
ref = put(LongMemory.valueOf(++lastLongIndex), value);
399+
ref = put(++lastLongIndex, value);
400400
}
401401

402402
return ref;
@@ -431,7 +431,7 @@ public void merge(ArrayMemory array, boolean recursive, Set<Integer> done) {
431431
} else {
432432
for (Map.Entry<Object, Memory> entry : array.map.entrySet()) {
433433
Object key = entry.getKey();
434-
if (key instanceof LongMemory) {
434+
if (key instanceof Long) {
435435
add(entry.getValue().toImmutable());
436436
} else {
437437
Memory value = entry.getValue();
@@ -466,10 +466,10 @@ public void merge(ArrayMemory array, boolean recursive, Set<Integer> done) {
466466

467467
public void putAll(ArrayMemory array) {
468468
if (array.map == null) {
469-
int i = 0;
469+
long i = 0;
470470
for (ReferenceMemory memory : array.getList()) {
471471
if (memory != null)
472-
put(LongMemory.valueOf(i), memory.toImmutable());
472+
put(i, memory.toImmutable());
473473
i++;
474474
}
475475
} else {
@@ -488,10 +488,10 @@ public void putAll(ArrayMemory array) {
488488

489489
public void putAllRef(ArrayMemory array) {
490490
if (array.map == null) {
491-
int i = 0;
491+
long i = 0;
492492
for (ReferenceMemory memory : array.getList()) {
493493
if (memory != null)
494-
put(LongMemory.valueOf(i), memory);
494+
put(i, memory);
495495
i++;
496496
}
497497
} else {
@@ -541,16 +541,21 @@ public ReferenceMemory put(Object key, IObject value) {
541541
return put(key, ObjectMemory.valueOf(value));
542542
}
543543

544+
public ReferenceMemory put(long key, Memory value) {
545+
return put((Long) key, value);
546+
}
547+
544548
public ReferenceMemory put(Object key, Memory value) {
545-
if (key instanceof LongMemory) {
549+
if (key instanceof Long) {
546550
ReferenceMemory mem = new ReferenceMemory(value);
547551

548-
int index = (int) ((LongMemory) key).value;
549-
550-
if (index > lastLongIndex)
551-
lastLongIndex = index;
552+
if ((Long) key > lastLongIndex) {
553+
lastLongIndex = (Long) key;
554+
}
552555

553556
if (map == null) {
557+
int index = ((Number) key).intValue();
558+
554559
int size = getList().size();
555560
if (index >= 0) {
556561
if (index < size) {
@@ -618,14 +623,13 @@ else if (key instanceof String) {
618623
}
619624

620625
{
621-
if (key instanceof Long)
622-
key = LongMemory.valueOf((Long) key);
623-
else if (key instanceof Integer)
624-
key = LongMemory.valueOf((Integer) key);
625-
else if (key instanceof String) {
626+
if (key instanceof String) {
626627
Memory tmp = StringMemory.toLong((String) key);
627-
if (tmp != null)
628-
key = tmp;
628+
if (tmp != null) {
629+
key = tmp.toLong();
630+
}
631+
} else if (key instanceof Integer) {
632+
key = ((Integer) key).longValue();
629633
}
630634

631635
Memory memory = map.remove(key);
@@ -639,7 +643,7 @@ else if (key instanceof String) {
639643
public Memory remove(Memory key) {
640644
Object _key = toKey(key);
641645
if (map == null) {
642-
int index = _key instanceof LongMemory ? (int) key.toLong() : -1;
646+
int index = _key instanceof Long ? ((Long) _key).intValue() : -1;
643647
if (index < 0 || _list == null || index >= getList().size())
644648
return null;
645649

@@ -813,8 +817,8 @@ public Memory peekKey() {
813817
return LongMemory.valueOf(size - 1);
814818
else {
815819
Object key = map.lastKey();
816-
if (key instanceof Memory) {
817-
return (Memory) key;
820+
if (key instanceof Long) {
821+
return LongMemory.valueOf((Long) key);
818822
} else if (key instanceof String) {
819823
return StringMemory.valueOf(key.toString());
820824
}
@@ -834,8 +838,8 @@ public Memory getRandomElementKey(Random rnd) {
834838
}
835839

836840
Object key = keys.next();
837-
if (key instanceof LongMemory)
838-
return (LongMemory) key;
841+
if (key instanceof Long)
842+
return LongMemory.valueOf((long) key);
839843
else
840844
return new StringMemory((String) key);
841845
}
@@ -1168,13 +1172,13 @@ public Memory valueOfIndex(TraceInfo trace, Memory index) {
11681172

11691173
@Override
11701174
public Memory valueOfIndex(TraceInfo trace, long index) {
1171-
Memory e = getByScalar(LongMemory.valueOf(index));
1175+
Memory e = getByScalar(index);
11721176
return e == null ? UNDEFINED : e;
11731177
}
11741178

11751179
@Override
11761180
public Memory valueOfIndex(TraceInfo trace, double index) {
1177-
Memory e = getByScalar(LongMemory.valueOf((long) index));
1181+
Memory e = getByScalar((long) index);
11781182
return e == null ? UNDEFINED : e;
11791183
}
11801184

@@ -1187,7 +1191,7 @@ public Memory valueOfIndex(TraceInfo trace, boolean index) {
11871191
@Override
11881192
public Memory valueOfIndex(TraceInfo trace, String index) {
11891193
Memory number = StringMemory.toLong(index);
1190-
Memory e = number == null ? getByScalar(index) : getByScalar(number);
1194+
Memory e = number == null ? getByScalar(index) : getByScalar(number.toLong());
11911195
return e == null ? UNDEFINED : e;
11921196
}
11931197

@@ -1234,12 +1238,12 @@ public Memory refOfIndex(TraceInfo trace, Memory index) {
12341238
@Override
12351239
public Memory refOfIndex(TraceInfo trace, long index) {
12361240
checkCopied();
1237-
return getOrCreate(LongMemory.valueOf(index));
1241+
return getByScalarOrCreate(index);
12381242
}
12391243

12401244
@Override
12411245
public Memory refOfIndex(TraceInfo trace, double index) {
1242-
return refOfIndex(null, LongMemory.valueOf((long) index));
1246+
return refOfIndex(trace, (long) index);
12431247
}
12441248

12451249
@Override
@@ -1252,7 +1256,7 @@ public Memory refOfIndex(TraceInfo trace, boolean index) {
12521256
public Memory refOfIndex(TraceInfo trace, String index) {
12531257
checkCopied();
12541258
Memory number = StringMemory.toLong(index);
1255-
return number == null ? getByScalarOrCreate(index) : getByScalarOrCreate(number);
1259+
return number == null ? getByScalarOrCreate(index) : getByScalarOrCreate(number.toLong());
12561260
}
12571261

12581262
@Override
@@ -1402,7 +1406,7 @@ protected boolean prevValue() {
14021406
return false;
14031407
} else {
14041408
cursor--;
1405-
currentKey = LongMemory.valueOf((long) cursor);
1409+
currentKey = (long) cursor;
14061410
setCurrentValue(getList().get(cursor));
14071411
return true;
14081412
}
@@ -1434,7 +1438,7 @@ protected boolean nextValue() {
14341438
return false;
14351439
}
14361440

1437-
currentKey = LongMemory.valueOf((long) cursor);
1441+
currentKey = (long) cursor;
14381442
setCurrentValue(getList().get(cursor));
14391443
cursor++;
14401444
return true;

jphp-runtime/src/php/runtime/memory/KeyValueMemory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public static Memory valueOf(Memory key, Memory value, String arrayKey) {
2222
}
2323

2424
public static Memory valueOf(Memory key, Memory value, Memory arrayKey) {
25+
KeyValueMemory memory = new KeyValueMemory(key, value);
26+
memory.arrayKey = ArrayMemory.toKey(arrayKey);
27+
return memory;
28+
}
29+
30+
public static Memory valueOf(Memory key, Memory value, long arrayKey) {
2531
KeyValueMemory memory = new KeyValueMemory(key, value);
2632
memory.arrayKey = arrayKey;
2733
return memory;

0 commit comments

Comments
 (0)