Skip to content

Commit

Permalink
Merge pull request #35 from ytake/feature/added-gmp
Browse files Browse the repository at this point in the history
added gmp
  • Loading branch information
ytake authored Jan 1, 2025
2 parents 97bb399 + b138b66 commit 515288a
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
phps: [
{version: '8.3', swoole: 'swoole-5.1.6'},
#{version: '8.3', swoole: 'swoole-5.1.6'},
{version: '8.4', swoole: 'swoole-6.0.0'}
]
name: PHP ${{ matrix.phps.version }} Test
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"ramsey/uuid": "^4.7.5",
"symfony/cache": "^7.0",
"symfony/uid": "^7.0",
"brick/math": "^0.12"
"brick/math": "^0.12",
"ext-gmp": "*"
},
"require-dev": {
"phpunit/phpunit": "^10.5.11",
Expand Down
2 changes: 1 addition & 1 deletion src/Phluxor/ActorSystem/Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function push(ReceiveFunction $receive): void
$this->behaviors[] = $receive;
}

private function pop(): ReceiveFunction
private function pop(): ?ReceiveFunction
{
$behavior = null;
$length = $this->len();
Expand Down
20 changes: 7 additions & 13 deletions src/Phluxor/ActorSystem/ConcurrentMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Phluxor\ActorSystem;

use function array_key_exists;

class ConcurrentMap
{
/** @var ConcurrentMapShared[] */
Expand All @@ -16,7 +14,7 @@ class ConcurrentMap
public function __construct()
{
for ($i = 0; $i < self::SHARD_COUNT; $i++) {
$this->map[] = new ConcurrentMapShared();
$this->map[$i] = new ConcurrentMapShared();
}
}

Expand All @@ -27,9 +25,6 @@ public function __construct()
public function getShard(string $key): ConcurrentMapShared
{
$shardIndex = $this->fnv32($key) % self::SHARD_COUNT;
if (!array_key_exists($shardIndex, $this->map)) {
return new ConcurrentMapShared();
}
return $this->map[$shardIndex];
}

Expand Down Expand Up @@ -92,14 +87,13 @@ public function get(string $key): ConcurrentMapResult
*/
public function fnv32(string $key): int
{
$hash = 2166136261;
$prime32 = 16777619;
$keyLength = strlen($key);
for ($i = 0; $i < $keyLength; $i++) {
$hash *= $prime32;
$hash = (int) $hash;
$hash = 0x811c9dc5; // FNV offset basis (32bit)
$prime32 = 0x01000193; // FNV prime (32bit)
$len = strlen($key);
for ($i = 0; $i < $len; $i++) {
$hash ^= ord($key[$i]);
$hash = ($hash * $prime32) & 0xffffffff;
}
return (int) $hash & 0xffffffff;
return $hash;
}
}
4 changes: 2 additions & 2 deletions src/Phluxor/ActorSystem/ConcurrentMapResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
readonly class ConcurrentMapResult
{
public function __construct(
private mixed $value,
private bool $exists
public mixed $value,
public bool $exists
) {
}

Expand Down
28 changes: 9 additions & 19 deletions src/Phluxor/ActorSystem/DeadLetterEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,25 @@
*/
readonly class DeadLetterEvent
{
/**
* @param Ref|null $ref
* @param mixed $message
* @param Ref|null $sender
*/
public function __construct(
private Ref|null $pid,
private mixed $message,
private Ref|null $sender,
public Ref|null $ref,
public mixed $message,
public Ref|null $sender,
) {
}

public function getMessage(): mixed
{
return $this->message;
}

public function getSender(): Ref|null
{
return $this->sender;
}

public function isNoSender(): bool
{
return $this->sender === null;
}

public function getRef(): Ref|null
{
return $this->pid;
}

public function isNoPid(): bool
{
return $this->pid === null;
return $this->ref === null;
}
}
12 changes: 6 additions & 6 deletions src/Phluxor/ActorSystem/DeadLetterProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function (int $i) {
if ($message instanceof DeadLetterEvent) {
if (!$message->isNoSender()) {
$this->actorSystem->root()->send(
$message->getSender(),
$message->sender,
new ActorSystem\ProtoBuf\DeadLetterResponse()
);
}
Expand All @@ -88,9 +88,9 @@ function (int $i) {
$this->actorSystem->getLogger()->info(
"deadletter",
[
"message" => $message->getMessage(),
"sender" => (string) $message->getSender(),
"pid" => (string) $message->getRef()
"message" => $message->message,
"sender" => (string) $message->sender,
"pid" => (string) $message->ref
]
);
}
Expand All @@ -99,14 +99,14 @@ function (int $i) {
});
$this->actorSystem->getEventStream()?->subscribe(function (mixed $message) {
if ($message instanceof DeadLetterEvent) {
$m = $message->getMessage();
$m = $message->message;
if ($m instanceof ActorSystem\ProtoBuf\Watch) {
if ($m->getWatcher() != null) {
$pid = new Ref($m->getWatcher());
$pid->sendSystemMessage(
$this->actorSystem,
new ActorSystem\ProtoBuf\Terminated([
"who" => $message->getRef(),
"who" => $message->ref,
'why' => ActorSystem\ProtoBuf\TerminatedReason::NotFound
])
);
Expand Down
10 changes: 5 additions & 5 deletions src/Phluxor/ActorSystem/ProcessRegistryValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function remove(Ref $pid): void
{
$bucket = $this->localPIDs->getBucket($pid->protobufPid()->getId());
$ref = $bucket->pop($pid->protobufPid()->getId());
$value = $ref->getValue();
$value = $ref->value;
if ($value instanceof ActorProcess) {
$value->dead()->set(1);
}
Expand All @@ -116,10 +116,10 @@ public function get(?Ref $pid): ProcessRegistryResult
}
$bucket = $this->localPIDs->getBucket($pid->protobufPid()->getId());
$value = $bucket->get($pid->protobufPid()->getId());
if (!$value->exists()) {
if (!$value->exists) {
return new ProcessRegistryResult($this->actorSystem->getDeadLetter(), false);
}
return new ProcessRegistryResult($value->getValue(), true);
return new ProcessRegistryResult($value->value, true);
}

/**
Expand All @@ -130,10 +130,10 @@ public function getLocal(string $id): ProcessRegistryResult
{
$bucket = $this->localPIDs->getBucket($id);
$value = $bucket->get($id);
if (!$value->exists()) {
if (!$value->exists) {
return new ProcessRegistryResult($this->actorSystem->getDeadLetter(), false);
}
return new ProcessRegistryResult($value->getValue(), true);
return new ProcessRegistryResult($value->value, true);
}

public function getAddress(): string
Expand Down
Loading

0 comments on commit 515288a

Please sign in to comment.