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

Commit

Permalink
Add methods for the kind of op instead of using is expressions (#18)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fredemmott authored and facebook-github-bot committed May 1, 2019
1 parent 42372bf commit df993ab
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/CLIColoredUnifiedDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ final protected static function colorDeleteLineWithIntralineEdits(
): string {
$line = self::DELETE_COLOR.'-';
foreach ($ops as $op) {
if ($op is DiffKeepOp<_>) {
if ($op->isKeepOp()) {
$line .= $op->getContent();
continue;
}
if ($op is DiffDeleteOp<_>) {
if ($op->isDeleteOp()) {
$line .= self::INTRALINE_DELETE_COLOR.
$op->getContent().
self::RESET.
Expand All @@ -91,11 +91,11 @@ final protected static function colorInsertLineWithIntralineEdits(
): string {
$line = self::INSERT_COLOR.'+';
foreach ($ops as $op) {
if ($op is DiffKeepOp<_>) {
if ($op->isKeepOp()) {
$line .= $op->getContent();
continue;
}
if ($op is DiffInsertOp<_>) {
if ($op->isInsertOp()) {
$line .= self::INTRALINE_INSERT_COLOR.
$op->getContent().
self::RESET.
Expand Down
4 changes: 2 additions & 2 deletions src/ColoredUnifiedDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ final public static function create(
$words_next = vec(\preg_split('/([^a-zA-Z0-9_]+)/', $next, -1, \PREG_SPLIT_DELIM_CAPTURE));
$intraline = (new StringDiff($words_line, $words_next))->getDiff();
$out[] = $intraline
|> Vec\filter($$, $op ==> !$op is DiffInsertOp<_>)
|> Vec\filter($$, $op ==> !$op->isInsertOp())
|> static::colorDeleteLineWithIntralineEdits($$);
$out[] = $intraline
|> Vec\filter($$, $op ==> !$op is DiffDeleteOp<_>)
|> Vec\filter($$, $op ==> !$op->isDeleteOp())
|> static::colorInsertLineWithIntralineEdits($$);
continue;
}
Expand Down
5 changes: 5 additions & 0 deletions src/DiffDeleteOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function getContent(): TContent {
return $this->content;
}

<<__Override>>
public function isDeleteOp(): bool {
return true;
}

<<__Override>>
public function asDeleteOp(): DiffDeleteOp<TContent> {
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/DiffInsertOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function getContent(): TContent {
return $this->content;
}

<<__Override>>
public function isInsertOp(): bool {
return true;
}

<<__Override>>
public function asInsertOp(): DiffInsertOp<TContent> {
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/DiffKeepOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function getContent(): TContent {
return $this->content;
}

<<__Override>>
public function isKeepOp(): bool {
return true;
}

<<__Override>>
public function asKeepOp(): DiffKeepOp<TContent> {
return $this;
Expand Down
12 changes: 12 additions & 0 deletions src/DiffOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
abstract class DiffOp<TContent> {
abstract public function getContent(): TContent;

public function isDeleteOp(): bool {
return false;
}

public function isInsertOp(): bool {
return false;
}

public function isKeepOp(): bool {
return false;
}

public function asDeleteOp(): DiffDeleteOp<TContent> {
invariant_violation('not a deletion');
}
Expand Down
15 changes: 9 additions & 6 deletions src/StringDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public function getHunks(int $context): vec<vec<DiffOp<string>>> {
$remaining = $this->getDiff();
$last = C\lastx($remaining);
// diff -u ignores trailing newlines
if ($last is DiffKeepOp<_> && $last->getContent() === '') {
if ($last->isKeepOp() && $last->getContent() === '') {
$remaining = Vec\slice($remaining, 0, C\count($remaining) - 1);
}

while (!C\is_empty($remaining)) {
$not_keep = C\find_key($remaining, $row ==> !$row is DiffKeepOp<_>);
$not_keep = C\find_key($remaining, $row ==> !$row->isKeepOp());
if ($not_keep === null) {
break;
}
Expand All @@ -57,7 +57,7 @@ public function getHunks(int $context): vec<vec<DiffOp<string>>> {
$end = $count;
$run_start = null;
for ($i = $context; $i < $count; ++$i) {
if ($remaining[$i] is DiffKeepOp<_>) {
if ($remaining[$i]->isKeepOp()) {
$run_start ??= $i;
continue;
}
Expand Down Expand Up @@ -101,7 +101,8 @@ private function getUnifiedDiffHunk(
$lines = vec[];

foreach ($hunk as $op) {
if ($op is DiffKeepOp<_>) {
if ($op->isKeepOp()) {
$op = $op->asKeepOp();
$lines[] = ' '.$op->getContent();
$old_start ??= $op->getOldPos();
$new_start ??= $op->getNewPos();
Expand All @@ -110,15 +111,17 @@ private function getUnifiedDiffHunk(
continue;
}

if ($op is DiffDeleteOp<_>) {
if ($op->isDeleteOp()) {
$op = $op->asDeleteOp();
$lines[] = '-'.$op->getContent();
$old_start ??= $op->getOldPos();
$new_start ??= $op->getOldPos();
++$old_lines;
continue;
}

if ($op is DiffInsertOp<_>) {
if ($op->isInsertOp()) {
$op = $op->asInsertOp();
$lines[] = '+'.$op->getContent();
$old_start ??= $op->getNewPos();
$new_start ??= $op->getNewPos();
Expand Down
11 changes: 6 additions & 5 deletions src/cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,22 @@ function cluster<T>(
$cluster ==> {
$first = C\firstx($cluster);

if ($first is DiffDeleteOp<_>) {
if ($first->isDeleteOp()) {
return new DiffDeleteOp(
$first->getOldPos(),
$first->asDeleteOp()->getOldPos(),
Vec\map($cluster, $op ==> $op->asDeleteOp()->getContent()),
);
}

if ($first is DiffInsertOp<_>) {
if ($first->isInsertOp()) {
return new DiffInsertOp(
$first->getNewPos(),
$first->asInsertOp()->getNewPos(),
Vec\map($cluster, $op ==> $op->asInsertOp()->getContent()),
);
}

if ($first is DiffKeepOp<_>) {
if ($first->isKeepOp()) {
$first = $first->asKeepOp();
return new DiffKeepOp(
$first->getOldPos(),
$first->getNewPos(),
Expand Down

0 comments on commit df993ab

Please sign in to comment.