Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@aviau
Copy link
Member

@aviau aviau commented Jan 17, 2021

pkg/schema: create CamliType type

Create a CamliType type in pkg/schema and use it in a couple of
packages.

It can be implemented in other packages as we go.

I was reading pkg/index and decided to implement @mpl's TODO:

// TODO(mpl): create consts somewhere for "claim" and "permanode" as camliTypes, and use them,		if meta.CamliType != string(schema.PermanodeCamliType) && meta.CamliType != string(schema.ClaimCamliType) {
// instead of hardcoding. Unless they already exist ? (didn't find them).

@googlebot googlebot added the cla: yes Author has submitted the Google CLA. label Jan 17, 2021
@aviau aviau added the needs review PRs that are ready to go, pending approval label Jan 17, 2021
Copy link
Contributor

@bradfitz bradfitz left a comment

Choose a reason for hiding this comment

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

There are a lot of string(foo) in this change, which suggests something isn't fully right yet. See comments.

@aviau aviau force-pushed the aviau/camlitypes branch 2 times, most recently from cb7c2d9 to 8718005 Compare January 17, 2021 05:03
@aviau aviau changed the title pkg/schema: create CamliType type [wip] pkg/schema: create CamliType type Jan 17, 2021
@aviau aviau changed the title [wip] pkg/schema: create CamliType type pkg/schema: create CamliType type Jan 17, 2021
@aviau
Copy link
Member Author

aviau commented Jan 17, 2021

@bradfitz Alright, done!

  • string -> CamliType everywhere it made sense
  • Renamed types to Type<...> as you suggested.

@aviau aviau requested a review from bradfitz January 17, 2021 05:14
panic("dup blob seen")
}
bm.CamliType = c.str(bm.CamliType)
bm.CamliType = schema.CamliType((c.str(string(bm.CamliType))))
Copy link
Member Author

@aviau aviau Jan 17, 2021

Choose a reason for hiding this comment

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

Not really sure if that works, can you confirm?

AFAIK this does not copy?

This is c.str():

// str returns s, interned.
func (c *Corpus) str(s string) string {
	if s == "" {
		return ""
	}
	if s, ok := c.strs[s]; ok {
		return s
	}
	if c.strs == nil {
		c.strs = make(map[string]string)
	}
	c.strs[s] = s
	return s
}

EDIT: This seems to indicate that it works, interning should work just fine:

package main

import (
	"fmt"
	"reflect"
	"strconv"
	"unsafe"
)

type CamliType string

const (
	Type12 CamliType = "12"
)

func stringptr(s string) uintptr {
	return (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
}

type stringInterner map[string]string

func (si stringInterner) Intern(s string) string {
	if interned, ok := si[s]; ok {
		return interned
	}
	si[s] = s
	return s
}

func main() {
	interner := stringInterner{}

	s1 := interner.Intern(string(Type12))
	s2 := interner.Intern(string(CamliType(strconv.Itoa(12))))

	fmt.Println(stringptr(s1) == stringptr(s2))
}

Copy link
Contributor

Choose a reason for hiding this comment

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

converting between two string-backed types is free. they'll share the same memory.

@aviau aviau force-pushed the aviau/camlitypes branch 5 times, most recently from f4fed2c to 76aeee6 Compare January 17, 2021 06:57
Create a CamliType type in pkg/schema and use it in a couple of
packages.

It can be implemented in other packages as we go.
@aviau aviau removed the needs review PRs that are ready to go, pending approval label Jan 19, 2021
@aviau aviau merged commit 231ba42 into master Jan 19, 2021
@aviau aviau deleted the aviau/camlitypes branch January 24, 2021 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Author has submitted the Google CLA.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants