Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Change: Count subtypes for pipeline threads #1681

Merged
merged 8 commits into from
Jan 8, 2020

Conversation

DyrellC
Copy link
Contributor

@DyrellC DyrellC commented Dec 6, 2019

Description

Use reflections package to count the number of implementations of the Stage class to determine the number of threads that are needed for the TransactionProcessingPipeline. It excludes the HashingStage class as it is only used manually.

Fixes #1680

Type of change

  • Enhancement (a non-breaking change which adds functionality)

How Has This Been Tested?

Ensured count (excluding HashingStage) was equal to the number of threads that was previously manually entered.

Checklist:

  • My code follows the style guidelines for this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes

@DyrellC DyrellC requested a review from GalRogozinski December 6, 2019 04:25
/**
* List of stages that will be ignored when determining thread count
*/
private static List<Stage> IGNORED_STAGES = new LinkedList<Stage>(){{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to
private static final List IGNORED_STAGES = IotaUtils.createImmutableList ...

You can optionally refactor createImmutableList to ImmutableList in a separate commit (only if you feel the name is too long, else you can leave it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 82 to 91
private int getNumberOfThreads() {
int threads = 0;
for(Stage stage: Stage.values()) {
if(!IGNORED_STAGES.contains(stage)){
threads += 1;
}
}
return threads;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have done it in a one liner:
return Stages.values().length - IGNORED_STAGES.size()

The above should also run faster (not that it matters much here)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, the reason I feel this is important is because any extra line of code is one more line for the maintainer to read

Since code is written once but read many times, these little changes throughout the codebase may save lots of time later on.

@@ -69,6 +76,10 @@
private BlockingQueue<ProcessingContext> broadcastStageQueue = new ArrayBlockingQueue<>(100);
private BlockingQueue<ProcessingContext> replyStageQueue = new ArrayBlockingQueue<>(100);

private int getNumberOfThreads() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing, usually when a method doesn't use the fields of the class I like to attach static to it
I used to think that the JIT optimizes it better but I don't think that's really true

However, I see advantages to it because:

  1. it can be used by public static methods
  2. when you review/maintain code you know immediately that this method can be reused/moved w/o fear
  3. when you change code you know immediately that the output of the method won't be affected due to a change to the state of the class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to a static final int now.

@@ -265,7 +265,6 @@
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't understand why this changed, but I'll ignore it this time
just be careful next time

On bigger PRs all those little changes are annoying
It also messes up hisotry

Copy link
Contributor

@GalRogozinski GalRogozinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

* List of stages that will be ignored when determining thread count
*/
private static final List IGNORED_STAGES = IotaUtils.createImmutableList(Stage.MULTIPLE, Stage.ABORT, Stage.FINISH);
private static final int NUMBER_OF_THREADS = Stage.values().length - IGNORED_STAGES.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@GalRogozinski GalRogozinski changed the title Feature: Count subtypes for pipeline threads Change: Count subtypes for pipeline threads Jan 8, 2020
@GalRogozinski GalRogozinski merged commit 7a29733 into iotaledger:dev Jan 8, 2020
@GalRogozinski GalRogozinski mentioned this pull request May 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add reflective subclass identifier in TransactionProcessingPipeline
2 participants