apache / apache/dubbo

optimize the InternalThreadLocalMap.size method

Open
#6,875 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
41.6k
Forks
26.4k
Avg merge
15h 13m
Merged PRs (30d)
4

Description

- [ ] I have searched the [issues](https://github.com/apache/dubbo/issues) of this repository and believe that this is not a duplicate.
- [ ] I have checked the [FAQ](https://github.com/apache/dubbo/blob/master/FAQ.md) of this repository and believe that this is not a duplicate.

### Environment

* Dubbo version: 2.7.9
* Operating System version: macos
* Java version: 1.8

### Steps to reproduce this issue

I looked at the source code of InternalThreadLocalMap as follows:

```
public int size() {
int count = 0;
for (Object o : indexedVariables) {
if (o != UNSET) {
++count;
}
}

//the fist element in `indexedVariables` is a set to keep all the InternalThreadLocal to remove
//look at method `addToVariablesToRemove`
return count - 1;
}
```

I think we should add a size field to record how many elements have been stored in ThreadLocalMap, and then the size() method can directly return the size field we defined.

eg.

```
public int size() {
return size;
}
public boolean setIndexedVariable(int index, Object value) {
Object[] lookup = indexedVariables;
if (index < lookup.length) {
Object oldValue = lookup[index];
lookup[index] = value;
boolean result = oldValue == UNSET;
if(result){
size++;
}
return result;
} else {
expandIndexedVariableTableAndSet(index, value);
size++
return true;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.