Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CopyOnWriteArrayList in PrettyTime. #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Isira-Seneviratne
Copy link
Contributor

No description provided.

@Isira-Seneviratne Isira-Seneviratne deleted the ConcurrentSkipListSet branch February 21, 2023 00:59
@lincolnthree
Copy link
Member

Hey @Isira-Seneviratne - Are you working on something new? :) Anything I should be aware of?

@Isira-Seneviratne Isira-Seneviratne restored the ConcurrentSkipListSet branch February 22, 2023 23:45
@Isira-Seneviratne
Copy link
Contributor Author

Hey @Isira-Seneviratne - Are you working on something new? :) Anything I should be aware of?

Yeah, I used ConcurrentSkipListSet in this PR instead of a volatile list.

@lincolnthree
Copy link
Member

Interesting. Can you tell me the reasons you went with this List implementation? I can guess, but I just want to make sure :)

@Isira-Seneviratne
Copy link
Contributor Author

Isira-Seneviratne commented Feb 27, 2023

Interesting. Can you tell me the reasons you went with this List implementation? I can guess, but I just want to make sure :)

It's actually a Set implementation, but it's designed for concurrent access, so a volatile variable is not needed.

Also, IntelliJ recommended using a thread-safe type instead of a volatile field.

@lincolnthree
Copy link
Member

lincolnthree commented Feb 28, 2023

Thanks, that helps me understand. There isn't technically anything wrong with volatile here. But I'm sure IntelliJ is scaring people away from it because it's not commonly used, and can be easy to get wrong by accessing non-threadsafe collections or objects across threads, or using the keyword when not necessary.

I would consider this...

Unfortunately, however, this PR breaks the tests (the ones that are passing) for TimeUnit configuration:
https://github.com/ocpsoft/prettytime/blob/master/core/src/test/java/org/ocpsoft/prettytime/PrettyTimeUnitConfigurationTest.java

Note that order of TimeUnits matters, and the default configuration does something a bit strange, providing the "JustNow" TimeUnit: https://github.com/ocpsoft/prettytime/blob/master/core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java#L1647

This TimeUnit is responsible for the default behavior (overriding Milliseconds and Seconds) by default so that anything less than a minute ago/from now shows 'just now' - a more human readable time measure for "short durations".

With your PR, that unit is sorted to somewhere near Minute, and loses priority to Milliseconds and Seconds when calculating non-precise durations - and that's not what the default behavior should be, since the unit needs to be in first place.

If you can figure out a way to do this without breaking the order of the default units, then let's do it. To be honest -- it's been a while since I coded that part of PrettyTime, so it might be worth re-visiting how unit order is preserved and set anyway.

@Isira-Seneviratne Isira-Seneviratne changed the title Use ConcurrentSkipListSet in PrettyTime. Use CopyOnWriteArrayList in PrettyTime. Mar 3, 2023
@lincolnthree
Copy link
Member

Sorry for the delay on this @Isira-Seneviratne - I will work on it ASAP. Life has gotten a bit unpredictable here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants