Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit df993ab

Browse files
fredemmottfacebook-github-bot
authored andcommitted
Add methods for the kind of op instead of using is expressions (#18)
Summary: This is a > 75% speedup for `getUnifiedDiff(string $a, string $b)` on large files when not running in repo-auth mode. Pull Request resolved: #18 Reviewed By: pittsw Differential Revision: D15170438 Pulled By: fredemmott fbshipit-source-id: ec5cd8e2d59951b2e5fe7aa637c29f740d0630d8
1 parent 42372bf commit df993ab

8 files changed

+48
-17
lines changed

src/CLIColoredUnifiedDiff.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ final protected static function colorDeleteLineWithIntralineEdits(
7070
): string {
7171
$line = self::DELETE_COLOR.'-';
7272
foreach ($ops as $op) {
73-
if ($op is DiffKeepOp<_>) {
73+
if ($op->isKeepOp()) {
7474
$line .= $op->getContent();
7575
continue;
7676
}
77-
if ($op is DiffDeleteOp<_>) {
77+
if ($op->isDeleteOp()) {
7878
$line .= self::INTRALINE_DELETE_COLOR.
7979
$op->getContent().
8080
self::RESET.
@@ -91,11 +91,11 @@ final protected static function colorInsertLineWithIntralineEdits(
9191
): string {
9292
$line = self::INSERT_COLOR.'+';
9393
foreach ($ops as $op) {
94-
if ($op is DiffKeepOp<_>) {
94+
if ($op->isKeepOp()) {
9595
$line .= $op->getContent();
9696
continue;
9797
}
98-
if ($op is DiffInsertOp<_>) {
98+
if ($op->isInsertOp()) {
9999
$line .= self::INTRALINE_INSERT_COLOR.
100100
$op->getContent().
101101
self::RESET.

src/ColoredUnifiedDiff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ final public static function create(
116116
$words_next = vec(\preg_split('/([^a-zA-Z0-9_]+)/', $next, -1, \PREG_SPLIT_DELIM_CAPTURE));
117117
$intraline = (new StringDiff($words_line, $words_next))->getDiff();
118118
$out[] = $intraline
119-
|> Vec\filter($$, $op ==> !$op is DiffInsertOp<_>)
119+
|> Vec\filter($$, $op ==> !$op->isInsertOp())
120120
|> static::colorDeleteLineWithIntralineEdits($$);
121121
$out[] = $intraline
122-
|> Vec\filter($$, $op ==> !$op is DiffDeleteOp<_>)
122+
|> Vec\filter($$, $op ==> !$op->isDeleteOp())
123123
|> static::colorInsertLineWithIntralineEdits($$);
124124
continue;
125125
}

src/DiffDeleteOp.php

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function getContent(): TContent {
2727
return $this->content;
2828
}
2929

30+
<<__Override>>
31+
public function isDeleteOp(): bool {
32+
return true;
33+
}
34+
3035
<<__Override>>
3136
public function asDeleteOp(): DiffDeleteOp<TContent> {
3237
return $this;

src/DiffInsertOp.php

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function getContent(): TContent {
2727
return $this->content;
2828
}
2929

30+
<<__Override>>
31+
public function isInsertOp(): bool {
32+
return true;
33+
}
34+
3035
<<__Override>>
3136
public function asInsertOp(): DiffInsertOp<TContent> {
3237
return $this;

src/DiffKeepOp.php

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function getContent(): TContent {
3232
return $this->content;
3333
}
3434

35+
<<__Override>>
36+
public function isKeepOp(): bool {
37+
return true;
38+
}
39+
3540
<<__Override>>
3641
public function asKeepOp(): DiffKeepOp<TContent> {
3742
return $this;

src/DiffOp.php

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
abstract class DiffOp<TContent> {
1919
abstract public function getContent(): TContent;
2020

21+
public function isDeleteOp(): bool {
22+
return false;
23+
}
24+
25+
public function isInsertOp(): bool {
26+
return false;
27+
}
28+
29+
public function isKeepOp(): bool {
30+
return false;
31+
}
32+
2133
public function asDeleteOp(): DiffDeleteOp<TContent> {
2234
invariant_violation('not a deletion');
2335
}

src/StringDiff.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function getHunks(int $context): vec<vec<DiffOp<string>>> {
4040
$remaining = $this->getDiff();
4141
$last = C\lastx($remaining);
4242
// diff -u ignores trailing newlines
43-
if ($last is DiffKeepOp<_> && $last->getContent() === '') {
43+
if ($last->isKeepOp() && $last->getContent() === '') {
4444
$remaining = Vec\slice($remaining, 0, C\count($remaining) - 1);
4545
}
4646

4747
while (!C\is_empty($remaining)) {
48-
$not_keep = C\find_key($remaining, $row ==> !$row is DiffKeepOp<_>);
48+
$not_keep = C\find_key($remaining, $row ==> !$row->isKeepOp());
4949
if ($not_keep === null) {
5050
break;
5151
}
@@ -57,7 +57,7 @@ public function getHunks(int $context): vec<vec<DiffOp<string>>> {
5757
$end = $count;
5858
$run_start = null;
5959
for ($i = $context; $i < $count; ++$i) {
60-
if ($remaining[$i] is DiffKeepOp<_>) {
60+
if ($remaining[$i]->isKeepOp()) {
6161
$run_start ??= $i;
6262
continue;
6363
}
@@ -101,7 +101,8 @@ private function getUnifiedDiffHunk(
101101
$lines = vec[];
102102

103103
foreach ($hunk as $op) {
104-
if ($op is DiffKeepOp<_>) {
104+
if ($op->isKeepOp()) {
105+
$op = $op->asKeepOp();
105106
$lines[] = ' '.$op->getContent();
106107
$old_start ??= $op->getOldPos();
107108
$new_start ??= $op->getNewPos();
@@ -110,15 +111,17 @@ private function getUnifiedDiffHunk(
110111
continue;
111112
}
112113

113-
if ($op is DiffDeleteOp<_>) {
114+
if ($op->isDeleteOp()) {
115+
$op = $op->asDeleteOp();
114116
$lines[] = '-'.$op->getContent();
115117
$old_start ??= $op->getOldPos();
116118
$new_start ??= $op->getOldPos();
117119
++$old_lines;
118120
continue;
119121
}
120122

121-
if ($op is DiffInsertOp<_>) {
123+
if ($op->isInsertOp()) {
124+
$op = $op->asInsertOp();
122125
$lines[] = '+'.$op->getContent();
123126
$old_start ??= $op->getNewPos();
124127
$new_start ??= $op->getNewPos();

src/cluster.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ function cluster<T>(
4040
$cluster ==> {
4141
$first = C\firstx($cluster);
4242

43-
if ($first is DiffDeleteOp<_>) {
43+
if ($first->isDeleteOp()) {
4444
return new DiffDeleteOp(
45-
$first->getOldPos(),
45+
$first->asDeleteOp()->getOldPos(),
4646
Vec\map($cluster, $op ==> $op->asDeleteOp()->getContent()),
4747
);
4848
}
4949

50-
if ($first is DiffInsertOp<_>) {
50+
if ($first->isInsertOp()) {
5151
return new DiffInsertOp(
52-
$first->getNewPos(),
52+
$first->asInsertOp()->getNewPos(),
5353
Vec\map($cluster, $op ==> $op->asInsertOp()->getContent()),
5454
);
5555
}
5656

57-
if ($first is DiffKeepOp<_>) {
57+
if ($first->isKeepOp()) {
58+
$first = $first->asKeepOp();
5859
return new DiffKeepOp(
5960
$first->getOldPos(),
6061
$first->getNewPos(),

0 commit comments

Comments
 (0)