Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

[IGNORE] #39

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/main/java/ru/vk/itmo/podobaalexandr/InMemoryDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.vk.itmo.podobaalexandr;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class InMemoryDaoImpl implements Dao<MemorySegment, Entry<MemorySegment>> {
ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> memorySegmentEntryMap
= new ConcurrentSkipListMap<>(new MyComparator());

@Override
public Iterator<Entry<MemorySegment>> get(MemorySegment from, MemorySegment to) {
ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> innerMap = memorySegmentEntryMap;

Choose a reason for hiding this comment

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

почему ты хранишь в начале ключ, а потом ентри, где тоже содержится ключ


if (from != null && to == null) {
innerMap = innerMap.tailMap(from);
} else if (from == null && to != null) {
innerMap = innerMap.headMap(to);
} else if (from != null) {
innerMap = innerMap.subMap(from, to);
}

return innerMap.values().iterator();
Copy link

@coradead coradead Sep 26, 2023

Choose a reason for hiding this comment

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

Лишняя аллокация в этом методе. ищи где

Copy link

@coradead coradead Sep 27, 2023

Choose a reason for hiding this comment

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

А теперь очень сложные ифы (условно). Можно сделать в начале !=null && !=null. Но это вкусовщина, можешь забить.

Если исправил замечание - надо на коммент ответить в чем заключалась ошибка (чтобы я понял, правильно ли ты меня понял) и объяснить исправление.

Copy link
Author

Choose a reason for hiding this comment

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

Так как у нас два отдельных if, в которых создаётся новая map, то может быть случай, когда у нас выполняются два условия и будут две аллокации, а теперь только одна.

}

@Override
public void upsert(Entry<MemorySegment> entry) {
memorySegmentEntryMap.put(entry.key(), entry);
}

@Override
public Entry<MemorySegment> get(MemorySegment key) {
return memorySegmentEntryMap.get(key);
}

private static class MyComparator implements Comparator<MemorySegment> {

@Override
public int compare(MemorySegment o1, MemorySegment o2) {

int sizeDiff = Long.compare(o1.byteSize(), o2.byteSize());
long mismatch = o1.mismatch(o2);

if (o1.byteSize() != 0 && o2.byteSize() != 0) {
int diff = mismatch == -1
? 0
: o1.get(ValueLayout.JAVA_BYTE, mismatch) - o2.get(ValueLayout.JAVA_BYTE, mismatch);

Choose a reason for hiding this comment

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

тоже сложный иф тоже вкусовщина

return diff == 0 ? sizeDiff : diff;
}

return sizeDiff;
}
}

}
35 changes: 35 additions & 0 deletions src/main/java/ru/vk/itmo/test/podobaalexandr/FactoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.vk.itmo.test.podobaalexandr;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;
import ru.vk.itmo.podobaalexandr.InMemoryDaoImpl;
import ru.vk.itmo.test.DaoFactory;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.nio.charset.StandardCharsets;

@SuppressWarnings("unused")
@DaoFactory
public class FactoryImpl implements DaoFactory.Factory<MemorySegment, Entry<MemorySegment>> {
@Override
public Dao<MemorySegment, Entry<MemorySegment>> createDao() {
return new InMemoryDaoImpl();
}

@Override
public String toString(MemorySegment memorySegment) {
return new String((byte[]) memorySegment.heapBase()
.orElse(memorySegment.toArray(ValueLayout.JAVA_BYTE)), StandardCharsets.UTF_8);
}

@Override
public MemorySegment fromString(String data) {
return data == null ? null : MemorySegment.ofArray(data.getBytes(StandardCharsets.UTF_8));
}

@Override
public Entry<MemorySegment> fromBaseEntry(Entry<MemorySegment> baseEntry) {
return baseEntry;
}
}