Skip to content

Commit

Permalink
perf: optimize TicketSet#getTargetStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Nov 11, 2024
1 parent 1bd210e commit e3c80d4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/ishland/flowsched/scheduler/TicketSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class TicketSet<K, V, Ctx> {

private final ItemStatus<K, V, Ctx> initialStatus;
private final Set<ItemTicket<K, V, Ctx>>[] status2Tickets;
// private volatile int targetStatus = 0;
private volatile int targetStatus = 0;

public TicketSet(ItemStatus<K, V, Ctx> initialStatus, ObjectFactory objectFactory) {
this.initialStatus = initialStatus;
// this.targetStatus = initialStatus.ordinal();
this.targetStatus = initialStatus.ordinal();
ItemStatus<K, V, Ctx>[] allStatuses = initialStatus.getAllStatuses();
this.status2Tickets = new Set[allStatuses.length];
for (int i = 0; i < allStatuses.length; i++) {
Expand All @@ -29,6 +29,8 @@ public boolean add(ItemTicket<K, V, Ctx> ticket) {
final boolean added = this.status2Tickets[targetStatus.ordinal()].add(ticket);
if (!added) return false;

this.targetStatus = this.computeTargetStatusSlow();

// if (this.targetStatus < targetStatus.ordinal()) {
// this.targetStatus = targetStatus.ordinal();
// }
Expand All @@ -41,6 +43,8 @@ public boolean remove(ItemTicket<K, V, Ctx> ticket) {
final boolean removed = this.status2Tickets[targetStatus.ordinal()].remove(ticket);
if (!removed) return false;

this.targetStatus = this.computeTargetStatusSlow();

// decreaseStatusAtomically();

return true;
Expand All @@ -65,7 +69,7 @@ public boolean remove(ItemTicket<K, V, Ctx> ticket) {
// }

public ItemStatus<K, V, Ctx> getTargetStatus() {
return this.initialStatus.getAllStatuses()[this.computeTargetStatusSlow()];
return this.initialStatus.getAllStatuses()[this.targetStatus];
}

public Set<ItemTicket<K, V, Ctx>> getTicketsForStatus(ItemStatus<K, V, Ctx> status) {
Expand Down

0 comments on commit e3c80d4

Please sign in to comment.