Skip to content

Commit 1278f53

Browse files
authored
Merge pull request erlang#6177 from bjorng/bjorn/erts/optimize-comparisons
Optimize comparison of equal terms
2 parents 5bc3e96 + ba77daa commit 1278f53

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

erts/emulator/beam/beam_common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ erts_gc_update_map_assoc(Process* p, Eterm* reg, Uint live,
21742174

21752175
ASSERT(kp < (Eterm *)mp);
21762176
key = *old_keys;
2177-
if ((c = CMP_TERM(key, new_key)) < 0) {
2177+
if ((c = (key == new_key) ? 0 : CMP_TERM(key, new_key)) < 0) {
21782178
/* Copy old key and value */
21792179
*kp++ = key;
21802180
*hp++ = *old_vals;

erts/emulator/beam/erl_map.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ static Eterm flatmap_merge(Process *p, Eterm nodeA, Eterm nodeB) {
13781378
vs2 = flatmap_get_values(mp2);
13791379

13801380
while(i1 < n1 && i2 < n2) {
1381-
c = CMP_TERM(ks1[i1],ks2[i2]);
1381+
c = (ks1[i1] == ks2[i2]) ? 0 : CMP_TERM(ks1[i1],ks2[i2]);
13821382
if (c == 0) {
13831383
/* use righthand side arguments map value,
13841384
* but advance both maps */

erts/emulator/beam/jit/arm/instr_common.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,10 @@ void BeamModuleAssembler::emit_is_lt(const ArgLabel &Fail,
15831583
} else if (LHS.isSmall()) {
15841584
a.and_(TMP1, rhs.reg, imm(_TAG_IMMED1_MASK));
15851585
} else {
1586+
/* Avoid the expensive generic comparison for equal terms. */
1587+
a.cmp(lhs.reg, rhs.reg);
1588+
a.b_eq(next);
1589+
15861590
ERTS_CT_ASSERT(_TAG_IMMED1_SMALL == _TAG_IMMED1_MASK);
15871591
a.and_(TMP1, lhs.reg, rhs.reg);
15881592
a.and_(TMP1, TMP1, imm(_TAG_IMMED1_MASK));
@@ -1687,6 +1691,10 @@ void BeamModuleAssembler::emit_is_ge(const ArgLabel &Fail,
16871691
} else if (LHS.isSmall()) {
16881692
a.and_(TMP1, rhs.reg, imm(_TAG_IMMED1_MASK));
16891693
} else {
1694+
/* Avoid the expensive generic comparison for equal terms. */
1695+
a.cmp(lhs.reg, rhs.reg);
1696+
a.b_eq(next);
1697+
16901698
ERTS_CT_ASSERT(_TAG_IMMED1_SMALL == _TAG_IMMED1_MASK);
16911699
a.and_(TMP1, lhs.reg, rhs.reg);
16921700
a.and_(TMP1, TMP1, imm(_TAG_IMMED1_MASK));

erts/emulator/beam/jit/x86/instr_common.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,10 @@ void BeamModuleAssembler::emit_is_lt(const ArgLabel &Fail,
17001700
} else if (LHS.isSmall()) {
17011701
a.mov(RETd, ARG2d);
17021702
} else {
1703+
/* Avoid the expensive generic comparison for equal terms. */
1704+
a.cmp(ARG1, ARG2);
1705+
a.short_().je(do_jge);
1706+
17031707
a.mov(RETd, ARG1d);
17041708
a.and_(RETd, ARG2d);
17051709
}
@@ -1795,6 +1799,10 @@ void BeamModuleAssembler::emit_is_ge(const ArgLabel &Fail,
17951799
} else if (LHS.isSmall()) {
17961800
a.mov(RETd, ARG2d);
17971801
} else {
1802+
/* Avoid the expensive generic comparison for equal terms. */
1803+
a.cmp(ARG1, ARG2);
1804+
a.short_().je(do_jl);
1805+
17981806
a.mov(RETd, ARG1d);
17991807
a.and_(RETd, ARG2d);
18001808
}

0 commit comments

Comments
 (0)