Skip to content

Commit

Permalink
Remove getWaitHandle from Hack array helpers
Browse files Browse the repository at this point in the history
Summary: - To be able to kill Userland Awaitable we first need to stop supporting them

Reviewed By: jano

Differential Revision: D6878069

fbshipit-source-id: ad72bb576ae9af8ebac9c4af85440ae7955e27dc
  • Loading branch information
WizKid authored and facebook-github-bot committed Feb 5, 2018
1 parent ef61c01 commit 556bb0a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 72 deletions.
53 changes: 23 additions & 30 deletions src/dict/async.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
async function from_async<Tk as arraykey, Tv>(
KeyedTraversable<Tk, Awaitable<Tv>> $awaitables,
): Awaitable<dict<Tk, Tv>> {
$wait_handles = dict[];
$awaitables = dict($awaitables);

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
await AwaitAllWaitHandle::fromDict($awaitables);
foreach ($awaitables as $key => $value) {
$wait_handles[$key] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
}
/* HH_IGNORE_ERROR[4135] Unset local variable to reduce peak memory. */
unset($awaitables);
await AwaitAllWaitHandle::fromDict($wait_handles);
foreach ($wait_handles as $key => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
$wait_handles[$key] = \HH\Asio\result($value);
$awaitables[$key] = \HH\Asio\result($value);
}
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
return $wait_handles;
return $awaitables;
}

/**
Expand All @@ -43,24 +38,23 @@
Traversable<Tk> $keys,
(function(Tk): Awaitable<Tv>) $async_func,
): Awaitable<dict<Tk, Tv>> {
$wait_handles = dict[];
$awaitables = dict[];
foreach ($keys as $key) {
if (!C\contains_key($wait_handles, $key)) {
$value = $async_func($key);
$wait_handles[$key] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
if (!C\contains_key($awaitables, $key)) {
$awaitables[$key] = $async_func($key);
}
}
/* HH_IGNORE_ERROR[4135] Unset local variable to reduce peak memory. */
unset($keys);
await AwaitAllWaitHandle::fromDict($wait_handles);
foreach ($wait_handles as $key => $value) {

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
await AwaitAllWaitHandle::fromDict($awaitables);
foreach ($awaitables as $key => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
$wait_handles[$key] = \HH\Asio\result($value);
$awaitables[$key] = \HH\Asio\result($value);
}
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
return $wait_handles;
return $awaitables;
}

/**
Expand Down Expand Up @@ -114,20 +108,19 @@
KeyedTraversable<Tk, Tv1> $traversable,
(function(Tv1): Awaitable<Tv2>) $value_func,
): Awaitable<dict<Tk, Tv2>> {
$wait_handles = dict[];
$awaitables = dict[];
foreach ($traversable as $key => $value) {
$value = $value_func($value);
$wait_handles[$key] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
$awaitables[$key] = $value_func($value);
}
/* HH_IGNORE_ERROR[4135] Unset local variable to reduce peak memory. */
unset($traversable);
await AwaitAllWaitHandle::fromDict($wait_handles);
foreach ($wait_handles as $key => $value) {

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
await AwaitAllWaitHandle::fromDict($awaitables);
foreach ($awaitables as $key => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
$wait_handles[$key] = \HH\Asio\result($value);
$awaitables[$key] = \HH\Asio\result($value);
}
/* HH_IGNORE_ERROR[4110] Reuse the existing dict to reduce peak memory. */
return $wait_handles;
return $awaitables;
}
34 changes: 13 additions & 21 deletions src/tuple/async.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,27 @@
* The function signature here is inaccurate as it can not be correctly
* expressed in Hack; this function is special-cased in the typechecker.
*/
async function from_async(?Awaitable<mixed> ...$args): Awaitable<mixed> {
async function from_async(?Awaitable<mixed> ...$awaitables): Awaitable<mixed> {
/* The oss-enable/disable + vec/varray dance is because varray is banned
* externally, and HH_IGNORE_ERROR/HH_FIXME/UNSAFE_EXPR can't be used to
* bypass the ban. */

// @oss-disable: $wait_handles = varray[];
$wait_handles = vec[]; // @oss-enable
// @oss-disable: $awaitables = varray($awaitables);
$awaitables = vec($awaitables); // @oss-enable

foreach ($args as $value) {
foreach ($awaitables as $index => $value) {
if ($value === null) {
// Calling ->getWaitHandle so that Hack knows this is WaitHandle (it's
// safe because HHVM will optimize it away).
$async_null = async { return null; };
$wait_handles[] = $async_null->getWaitHandle();
} else {
$wait_handles[] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
$awaitables[$index] = async { return null; };
}
}
/* HH_IGNORE_ERROR[4135] unset() is not supposed to be used here, but we want
* the memory back */
unset($args);
// @oss-disable: await AwaitAllWaitHandle::fromArray($wait_handles);
await AwaitAllWaitHandle::fromVec($wait_handles); // @oss-enable
foreach ($wait_handles as $index => $value) {

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
// @oss-disable: await AwaitAllWaitHandle::fromArray($awaitables);
await AwaitAllWaitHandle::fromVec($awaitables); // @oss-enable
foreach ($awaitables as $index => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing array to reduce peak memory. */
$wait_handles[$index] = \HH\Asio\result($value);
$awaitables[$index] = \HH\Asio\result($value);
}
return \HH\Lib\_Private\tuple_from_vec($wait_handles); // @oss-enable
// @oss-disable: return $wait_handles;
return \HH\Lib\_Private\tuple_from_vec($awaitables); // @oss-enable
// @oss-disable: return $awaitables;
}
36 changes: 15 additions & 21 deletions src/vec/async.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
async function from_async<Tv>(
Traversable<Awaitable<Tv>> $awaitables,
): Awaitable<vec<Tv>> {
$wait_handles = vec[];
foreach ($awaitables as $value) {
$wait_handles[] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
}
/* HH_IGNORE_ERROR[4135] Unset local variable to reduce peak memory. */
unset($awaitables);
await AwaitAllWaitHandle::fromVec($wait_handles);
foreach ($wait_handles as $index => $value) {
$awaitables = vec($awaitables);

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
await AwaitAllWaitHandle::fromVec($awaitables);
foreach ($awaitables as $index => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing vec to reduce peak memory. */
$wait_handles[$index] = \HH\Asio\result($value);
$awaitables[$index] = \HH\Asio\result($value);
}
/* HH_IGNORE_ERROR[4110] Reuse the existing vec to reduce peak memory. */
return $wait_handles;
return $awaitables;
}

/**
Expand Down Expand Up @@ -63,20 +58,19 @@
Traversable<Tv1> $traversable,
(function(Tv1): Awaitable<Tv2>) $async_func,
): Awaitable<vec<Tv2>> {
$wait_handles = vec[];
$awaitables = vec[];
foreach ($traversable as $value) {
$value = $async_func($value);
$wait_handles[] = $value instanceof WaitHandle
? $value
: $value->getWaitHandle();
$awaitables[] = $async_func($value);
}
/* HH_IGNORE_ERROR[4135] Unset local variable to reduce peak memory. */
unset($traversable);
await AwaitAllWaitHandle::fromVec($wait_handles);
foreach ($wait_handles as $index => $value) {

/* HH_IGNORE_ERROR[4110] Okay to pass in Awaitable */
await AwaitAllWaitHandle::fromVec($awaitables);
foreach ($awaitables as $index => $value) {
/* HH_IGNORE_ERROR[4110] Reuse the existing vec to reduce peak memory. */
$wait_handles[$index] = \HH\Asio\result($value);
$awaitables[$index] = \HH\Asio\result($value);
}
/* HH_IGNORE_ERROR[4110] Reuse the existing vec to reduce peak memory. */
return $wait_handles;
return $awaitables;
}

0 comments on commit 556bb0a

Please sign in to comment.